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)
Transformations and Arbitrary Generation
When generating arbitrary data, it is important to understand how transformations and filters are handled within a schema:
Example (Filters and Transformations)
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.
Best Practices
To ensure consistent and valid arbitrary data generation, follow these guidelines:
Apply Filters First: Define filters for the initial type (I).
Apply Transformations: Add transformations to convert the data.
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:
Prefer a structured approach by separating transformation steps from filter applications:
Example (Preferred Structured Approach)
Customizing Arbitrary Data Generation
You can customize how arbitrary data is generated using the arbitrary annotation in schema definitions.
Example (Custom Arbitrary Generator)
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.