Skip to content

Schema to Arbitrary

The Arbitrary.make function allows for the creation of random values that align with a specific Schema<A, I, R>. This function returns an Arbitrary<A> from the fast-check library, which is particularly useful for generating random test data that adheres to the defined schema constraints.

Example (Generating Arbitrary Data for a Schema)

import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Schema
Schema
} from "effect"
// Define a Person schema with constraints
const
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>(fields: {
name: typeof Schema.NonEmptyString;
age: Schema.filter<Schema.Schema<number, number, never>>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
age: Schema.filter<Schema.Schema<number, number, never>>
age
:
import Schema
Schema
.
class Int

@since3.10.0

Int
.
Pipeable.pipe<typeof Schema.Int, Schema.filter<Schema.Schema<number, number, never>>>(this: typeof Schema.Int, ab: (_: typeof Schema.Int) => Schema.filter<Schema.Schema<number, number, never>>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <number>(min: number, max: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <I, R>(self: Schema.Schema<number, I, R>) => Schema.filter<...>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 80))
})
// Create an Arbitrary based on the schema
const
const arb: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}>
arb
=
import Arbitrary
Arbitrary
.
const make: <{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>) => FastCheck.Arbitrary<...>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>
Person
)
// Generate random samples from the Arbitrary
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import FastCheck
FastCheck
.
sample<{
readonly name: string;
readonly age: number;
}>(generator: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}> | FastCheck.IRawProperty<{
readonly name: string;
readonly age: number;
}, boolean>, params?: number | ... 1 more ... | undefined): {
readonly name: string;
readonly age: number;
}[]
export sample

Generate an array containing all the values that would have been generated during

assert

or

check

@example

fc.sample(fc.nat(), 10); // extract 10 values from fc.nat() Arbitrary
fc.sample(fc.nat(), {seed: 42}); // extract values from fc.nat() as if we were running fc.assert with seed=42

@paramgenerator - IProperty or Arbitrary to extract the values from

@paramparams - Integer representing the number of values to generate or Parameters as in assert

@public

sample
(
const arb: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}>
arb
, 2))
/*
Example Output:
[ { name: 'q r', age: 3 }, { name: '&|', age: 6 } ]
*/

To make the output more realistic, see the Customizing Arbitrary Data Generation section.

When generating random values, Arbitrary tries to follow the schema’s constraints. It uses the most appropriate fast-check primitives and applies constraints if the primitive supports them.

For instance, if you define an age property as:

Schema.Int.pipe(Schema.between(1, 80))

the arbitrary generation will use:

fc.integer({ min: 1, max: 80 })

to produce values within that range.

To generate efficient arbitraries for strings that must match a certain pattern, use the Schema.pattern filter instead of writing a custom filter:

Example (Using Schema.pattern for Pattern Constraints)

import {
import Schema
Schema
} from "effect"
// ❌ Without using Schema.pattern (less efficient)
const
const Bad: Schema.filter<typeof Schema.String>
Bad
=
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.filter<typeof Schema.String>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.filter<typeof Schema.String>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
function filter<typeof Schema.String>(predicate: (a: string, options: ParseOptions, self: Refinement) => FilterReturnType, annotations?: Schema.Annotations.Filter<...> | undefined): (self: typeof Schema.String) => Schema.filter<...> (+2 overloads)

@since3.10.0

filter
((
s: string
s
) => /^[a-z]+$/.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
s: string
s
)))
// ✅ Using Schema.pattern (more efficient)
const
const Good: Schema.filter<Schema.Schema<string, string, never>>
Good
=
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.filter<Schema.Schema<string, string, never>>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.filter<Schema.Schema<string, string, never>>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const pattern: <string>(regex: RegExp, annotations?: Schema.Annotations.Filter<string, string> | undefined) => <I, R>(self: Schema.Schema<string, I, R>) => Schema.filter<Schema.Schema<...>>

@since3.10.0

pattern
(/^[a-z]+$/))

By using Schema.pattern, arbitrary generation will rely on fc.stringMatching(regexp), which is more efficient and directly aligned with the defined pattern.

When generating arbitrary data, it is important to understand how transformations and filters are handled within a schema:

Example (Filters and Transformations)

import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Schema
Schema
} from "effect"
// Schema with filters before the transformation
const
const schema1: Schema.filter<Schema.Schema<string, string, never>>
schema1
=
import Schema
Schema
.
const compose: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>) => Schema.SchemaClass<string, string, never> (+7 overloads)

@since3.10.0

compose
(
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
import Schema
Schema
.
class Trim

This schema allows removing whitespaces from the beginning and end of a string.

@since3.10.0

Trim
).
Pipeable.pipe<Schema.SchemaClass<string, string, never>, Schema.filter<Schema.Schema<string, string, never>>>(this: Schema.SchemaClass<...>, ab: (_: Schema.SchemaClass<string, string, never>) => Schema.filter<...>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const maxLength: <string>(maxLength: number, annotations?: Schema.Annotations.Filter<string, string> | undefined) => <I, R>(self: Schema.Schema<string, I, R>) => Schema.filter<...>

@since3.10.0

maxLength
(500)
)
// May produce empty strings due to ignored NonEmpty filter
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import FastCheck
FastCheck
.
sample<string>(generator: FastCheck.Arbitrary<string> | FastCheck.IRawProperty<string, boolean>, params?: number | FastCheck.Parameters<string> | undefined): string[]
export sample

Generate an array containing all the values that would have been generated during

assert

or

check

@example

fc.sample(fc.nat(), 10); // extract 10 values from fc.nat() Arbitrary
fc.sample(fc.nat(), {seed: 42}); // extract values from fc.nat() as if we were running fc.assert with seed=42

@paramgenerator - IProperty or Arbitrary to extract the values from

@paramparams - Integer representing the number of values to generate or Parameters as in assert

@public

sample
(
import Arbitrary
Arbitrary
.
const make: <string, string, never>(schema: Schema.Schema<string, string, never>) => FastCheck.Arbitrary<string>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const schema1: Schema.filter<Schema.Schema<string, string, never>>
schema1
), 2))
/*
Example Output:
[ '', '"Ry' ]
*/
// Schema with filters applied after transformations
const
const schema2: Schema.filter<Schema.Schema<string, string, never>>
schema2
=
import Schema
Schema
.
class Trim

This schema allows removing whitespaces from the beginning and end of a string.

@since3.10.0

Trim
.
Pipeable.pipe<typeof Schema.Trim, Schema.filter<Schema.Schema<string, string, never>>, Schema.filter<Schema.Schema<string, string, never>>>(this: typeof Schema.Trim, ab: (_: typeof Schema.Trim) => Schema.filter<...>, bc: (_: Schema.filter<...>) => Schema.filter<...>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const nonEmptyString: <string>(annotations?: Schema.Annotations.Filter<string, string> | undefined) => <I, R>(self: Schema.Schema<string, I, R>) => Schema.filter<Schema.Schema<string, I, R>>

@since3.10.0

nonEmptyString
(),
import Schema
Schema
.
const maxLength: <string>(maxLength: number, annotations?: Schema.Annotations.Filter<string, string> | undefined) => <I, R>(self: Schema.Schema<string, I, R>) => Schema.filter<...>

@since3.10.0

maxLength
(500)
)
// Adheres to all filters, avoiding empty strings
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import FastCheck
FastCheck
.
sample<string>(generator: FastCheck.Arbitrary<string> | FastCheck.IRawProperty<string, boolean>, params?: number | FastCheck.Parameters<string> | undefined): string[]
export sample

Generate an array containing all the values that would have been generated during

assert

or

check

@example

fc.sample(fc.nat(), 10); // extract 10 values from fc.nat() Arbitrary
fc.sample(fc.nat(), {seed: 42}); // extract values from fc.nat() as if we were running fc.assert with seed=42

@paramgenerator - IProperty or Arbitrary to extract the values from

@paramparams - Integer representing the number of values to generate or Parameters as in assert

@public

sample
(
import Arbitrary
Arbitrary
.
const make: <string, string, never>(schema: Schema.Schema<string, string, never>) => FastCheck.Arbitrary<string>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const schema2: Schema.filter<Schema.Schema<string, string, never>>
schema2
), 2))
/*
Example Output:
[ ']H+MPXgZKz', 'SNS|waP~\\' ]
*/

Explanation:

  • schema1: Takes into account Schema.maxLength(500) since it is applied after the Schema.Trim transformation, but ignores the Schema.NonEmptyString as it precedes the transformations.
  • schema2: Adheres fully to all filters because they are correctly sequenced after transformations, preventing the generation of undesired data.

To ensure consistent and valid arbitrary data generation, follow these guidelines:

  1. Apply Filters First: Define filters for the initial type (I).
  2. Apply Transformations: Add transformations to convert the data.
  3. Apply Final Filters: Use filters for the transformed type (A).

This setup ensures that each stage of data processing is precise and well-defined.

Example (Avoid Mixed Filters and Transformations)

Avoid haphazard combinations of transformations and filters:

import {
import Schema
Schema
} from "effect"
// Less optimal approach: Mixing transformations and filters
const
const problematic: Schema.SchemaClass<string, string, never>
problematic
=
import Schema
Schema
.
const compose: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>) => Schema.SchemaClass<string, string, never> (+7 overloads)

@since3.10.0

compose
(
import Schema
Schema
.
class Lowercase

This schema converts a string to lowercase.

@since3.10.0

Lowercase
,
import Schema
Schema
.
class Trim

This schema allows removing whitespaces from the beginning and end of a string.

@since3.10.0

Trim
)

Prefer a structured approach by separating transformation steps from filter applications:

Example (Preferred Structured Approach)

import {
import Schema
Schema
} from "effect"
// Recommended: Separate transformations and filters
const
const improved: Schema.transform<typeof Schema.String, Schema.filter<Schema.Schema<string, string, never>>>
improved
=
import Schema
Schema
.
const transform: <Schema.filter<Schema.Schema<string, string, never>>, typeof Schema.String>(from: typeof Schema.String, to: Schema.filter<Schema.Schema<string, string, never>>, options: {
...;
} | {
...;
}) => Schema.transform<...> (+1 overload)

Create a new Schema by transforming the input and output of an existing Schema using the provided mapping functions.

@since3.10.0

transform
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.filter<Schema.Schema<string, string, never>>, Schema.filter<Schema.Schema<string, string, never>>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.filter<...>, bc: (_: Schema.filter<...>) => Schema.filter<...>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const trimmed: <string>(annotations?: Schema.Annotations.Filter<string, string> | undefined) => <I, R>(self: Schema.Schema<string, I, R>) => Schema.filter<Schema.Schema<string, I, R>>

Verifies that a string contains no leading or trailing whitespaces.

Note. This combinator does not make any transformations, it only validates. If what you were looking for was a combinator to trim strings, then check out the trim combinator.

@since3.10.0

trimmed
(),
import Schema
Schema
.
const lowercased: <string>(annotations?: Schema.Annotations.Filter<string, string> | undefined) => <I, R>(self: Schema.Schema<string, I, R>) => Schema.filter<Schema.Schema<string, I, R>>

Verifies that a string is lowercased.

@since3.10.0

lowercased
()),
{
strict?: true
strict
: true,
decode: (fromA: string, fromI: string) => string
decode
: (
s: string
s
) =>
s: string
s
.
String.trim(): string

Removes the leading and trailing white space and line terminator characters from a string.

trim
().
String.toLowerCase(): string

Converts all the alphabetic characters in a string to lowercase.

toLowerCase
(),
encode: (toI: string, toA: string) => string
encode
: (
s: string
s
) =>
s: string
s
}
)

You can customize how arbitrary data is generated using the arbitrary annotation in schema definitions.

Example (Custom Arbitrary Generator)

import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Schema
Schema
} from "effect"
const
const Name: Schema.refine<string, Schema.Schema<string, string, never>>
Name
=
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
.
Annotable<refine<string, Schema<string, string, never>>, string, string, never>.annotations(annotations: Schema.Annotations.GenericSchema<string>): Schema.refine<string, Schema.Schema<string, string, never>>

Merges a set of new annotations with existing ones, potentially overwriting any duplicates.

annotations
({
Annotations.GenericSchema<string>.arbitrary?: (..._: any) => Arbitrary.LazyArbitrary<string>
arbitrary
: () => (
fc: typeof FastCheck
fc
) =>
fc: typeof FastCheck
fc
.
constantFrom<string>(...values: string[]): FastCheck.Arbitrary<string> (+1 overload)
export constantFrom

For one ...values values - all equiprobable

WARNING: It expects at least one value, otherwise it should throw

@paramvalues - Constant values to be produced (all values shrink to the first one)

@public

constantFrom
("Alice Johnson", "Dante Howell", "Marta Reyes")
})
const
const Age: Schema.filter<Schema.Schema<number, number, never>>
Age
=
import Schema
Schema
.
class Int

@since3.10.0

Int
.
Pipeable.pipe<typeof Schema.Int, Schema.filter<Schema.Schema<number, number, never>>>(this: typeof Schema.Int, ab: (_: typeof Schema.Int) => Schema.filter<Schema.Schema<number, number, never>>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <number>(min: number, max: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <I, R>(self: Schema.Schema<number, I, R>) => Schema.filter<...>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 80))
const
const Person: Schema.Struct<{
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>(fields: {
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
name: Schema.refine<string, Schema.Schema<string, string, never>>
name
:
const Name: Schema.refine<string, Schema.Schema<string, string, never>>
Name
,
age: Schema.filter<Schema.Schema<number, number, never>>
age
:
const Age: Schema.filter<Schema.Schema<number, number, never>>
Age
})
const
const arb: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}>
arb
=
import Arbitrary
Arbitrary
.
const make: <{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>) => FastCheck.Arbitrary<...>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const Person: Schema.Struct<{
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>
Person
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import FastCheck
FastCheck
.
sample<{
readonly name: string;
readonly age: number;
}>(generator: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}> | FastCheck.IRawProperty<{
readonly name: string;
readonly age: number;
}, boolean>, params?: number | ... 1 more ... | undefined): {
readonly name: string;
readonly age: number;
}[]
export sample

Generate an array containing all the values that would have been generated during

assert

or

check

@example

fc.sample(fc.nat(), 10); // extract 10 values from fc.nat() Arbitrary
fc.sample(fc.nat(), {seed: 42}); // extract values from fc.nat() as if we were running fc.assert with seed=42

@paramgenerator - IProperty or Arbitrary to extract the values from

@paramparams - Integer representing the number of values to generate or Parameters as in assert

@public

sample
(
const arb: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}>
arb
, 2))
/*
Example Output:
[ { name: 'Dante Howell', age: 6 }, { name: 'Marta Reyes', age: 53 } ]
*/

The annotation allows access the complete export of the fast-check library (fc). This setup enables you to return an Arbitrary that precisely generates the type of data desired.

When using mocking libraries like @faker-js/faker, you can combine them with fast-check to generate realistic data for testing purposes.

Example (Integrating with Faker)

import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Schema
Schema
} from "effect"
import {
const faker: Faker

The faker instance for the en locale.

  • Language: English
  • Endonym: English

This instance uses the following locales internally (in descending precedence):

  • en
  • base

faker
} from "@faker-js/faker"
const
const Name: Schema.refine<string, Schema.Schema<string, string, never>>
Name
=
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
.
Annotable<refine<string, Schema<string, string, never>>, string, string, never>.annotations(annotations: Schema.Annotations.GenericSchema<string>): Schema.refine<string, Schema.Schema<string, string, never>>

Merges a set of new annotations with existing ones, potentially overwriting any duplicates.

annotations
({
Annotations.GenericSchema<string>.arbitrary?: (..._: any) => Arbitrary.LazyArbitrary<string>
arbitrary
: () => (
fc: typeof FastCheck
fc
) =>
fc: typeof FastCheck
fc
.
constant<null>(value: null): FastCheck.Arbitrary<null>
export constant

For value

@paramvalue - The value to produce

@public

constant
(null).
Arbitrary<null>.map<string>(mapper: (t: null) => string, unmapper?: ((possiblyU: unknown) => null) | undefined): FastCheck.Arbitrary<string>

Create another arbitrary by mapping all produced values using the provided mapper Values produced by the new arbitrary are the result of applying mapper value by value

@example

const rgbChannels: Arbitrary<{r:number,g:number,b:number}> = ...;
const color: Arbitrary<string> = rgbChannels.map(ch => `#${(ch.r*65536 + ch.g*256 + ch.b).toString(16).padStart(6, '0')}`);
// transform an Arbitrary producing {r,g,b} integers into an Arbitrary of '#rrggbb'

@parammapper - Map function, to produce a new element based on an old one

@paramunmapper - Optional unmap function, it will never be used except when shrinking user defined values. Must throw if value is not compatible (since 3.0.0)

@returnsNew arbitrary with mapped elements

map
(() => {
// Each time the arbitrary is sampled, faker generates a new name
return
const faker: Faker

The faker instance for the en locale.

  • Language: English
  • Endonym: English

This instance uses the following locales internally (in descending precedence):

  • en
  • base

faker
.
Faker.person: PersonModule
person
.
PersonModule.fullName(options?: {
firstName?: string;
lastName?: string;
sex?: SexType;
}): string

Generates a random full name.

@paramoptions An options object.

@paramoptions.firstName The optional first name to use. If not specified a random one will be chosen.

@paramoptions.lastName The optional last name to use. If not specified a random one will be chosen.

@paramoptions.sex The optional sex to use. Can be either 'female' or 'male'.

@example faker.person.fullName() // 'Allen Brown' faker.person.fullName({ firstName: 'Joann' }) // 'Joann Osinski' faker.person.fullName({ firstName: 'Marcella', sex: 'female' }) // 'Mrs. Marcella Huels' faker.person.fullName({ lastName: 'Beer' }) // 'Mr. Alfonso Beer' faker.person.fullName({ sex: 'male' }) // 'Fernando Schaefer'

@since8.0.0

fullName
()
})
})
const
const Age: Schema.filter<Schema.Schema<number, number, never>>
Age
=
import Schema
Schema
.
class Int

@since3.10.0

Int
.
Pipeable.pipe<typeof Schema.Int, Schema.filter<Schema.Schema<number, number, never>>>(this: typeof Schema.Int, ab: (_: typeof Schema.Int) => Schema.filter<Schema.Schema<number, number, never>>): Schema.filter<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <number>(min: number, max: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <I, R>(self: Schema.Schema<number, I, R>) => Schema.filter<...>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 80))
const
const Person: Schema.Struct<{
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>(fields: {
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
name: Schema.refine<string, Schema.Schema<string, string, never>>
name
:
const Name: Schema.refine<string, Schema.Schema<string, string, never>>
Name
,
age: Schema.filter<Schema.Schema<number, number, never>>
age
:
const Age: Schema.filter<Schema.Schema<number, number, never>>
Age
})
const
const arb: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}>
arb
=
import Arbitrary
Arbitrary
.
const make: <{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>) => FastCheck.Arbitrary<...>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const Person: Schema.Struct<{
name: Schema.refine<string, Schema.Schema<string, string, never>>;
age: Schema.filter<Schema.Schema<number, number, never>>;
}>
Person
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import FastCheck
FastCheck
.
sample<{
readonly name: string;
readonly age: number;
}>(generator: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}> | FastCheck.IRawProperty<{
readonly name: string;
readonly age: number;
}, boolean>, params?: number | ... 1 more ... | undefined): {
readonly name: string;
readonly age: number;
}[]
export sample

Generate an array containing all the values that would have been generated during

assert

or

check

@example

fc.sample(fc.nat(), 10); // extract 10 values from fc.nat() Arbitrary
fc.sample(fc.nat(), {seed: 42}); // extract values from fc.nat() as if we were running fc.assert with seed=42

@paramgenerator - IProperty or Arbitrary to extract the values from

@paramparams - Integer representing the number of values to generate or Parameters as in assert

@public

sample
(
const arb: FastCheck.Arbitrary<{
readonly name: string;
readonly age: number;
}>
arb
, 2))
/*
Example Output:
[
{ name: 'Henry Dietrich', age: 68 },
{ name: 'Lucas Haag', age: 52 }
]
*/