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
Transformations and Arbitrary Generation
The generation of arbitrary data requires a clear understanding of how transformations and filters are considered within a schema:
Illustrative Example
Explanation:
Schema 1: 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.
Schema 2: Adheres fully to all filters because they are correctly sequenced after transformations, preventing the generation of undesired data.
Best Practices
For effective and clear data generation, it’s advisable to organize transformations and filters methodically. A suggested pattern is:
Filters for the initial type (I).
Followed by transformations.
And then filters for the transformed type (A).
This setup ensures that each stage of data processing is precise and well-defined.
Illustrative Example
Avoid haphazard combinations of transformations and filters:
Prefer a structured approach by separating transformation steps from filter applications:
Customizing Arbitrary Data Generation
You can define how arbitrary data is generated by utilizing the arbitrary annotation in your schema definitions.
Example
The annotation allows access to any type parameters via the first argument (typeParameters) and 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.