Hyperlinkv0.8.0-beta.28

Schema

Schema.toArbitraryfunctioneffect/Schema.ts:13079
<S extends Constraint>(schema: S): FastCheck.Arbitrary<S["Type"]>
<S extends Constraint>(
  schema: S,
  options: { readonly report: true }
): Annotations.ToArbitrary.WithReport<FastCheck.Arbitrary<S["Type"]>>

Derives a fast-check Arbitrary from a schema for property-based testing. The derived arbitrary generates values that satisfy the schema.

Details

Constraints refine base generators; candidates add weighted sources while filters still validate every value. { report: true } returns warnings such as OpaqueFilter, while derivation errors remain fail-fast. Recursive schemas use terminal branches and fail when no finite terminal path exists.

Example (Generating arbitrary values)

import { Schema } from "effect"
import * as FastCheck from "fast-check"

const PersonArb = Schema.toArbitrary(
  Schema.Struct({ name: Schema.String, age: Schema.Number })
)

// Sample a random value
const sample = FastCheck.sample(PersonArb, 1)[0]
console.log(typeof sample.name) // "string"
Arbitrary
Source effect/Schema.ts:1307920 lines
export function toArbitrary<S extends Constraint>(schema: S): FastCheck.Arbitrary<S["Type"]>
export function toArbitrary<S extends Constraint>(
  schema: S,
  options: { readonly report: true }
): Annotations.ToArbitrary.WithReport<FastCheck.Arbitrary<S["Type"]>>
export function toArbitrary<S extends Constraint>(
  schema: S,
  options?: { readonly report?: boolean }
): FastCheck.Arbitrary<S["Type"]> | Annotations.ToArbitrary.WithReport<FastCheck.Arbitrary<S["Type"]>> {
  if (options?.report === true) {
    const lawc = InternalArbitrary.memoized(schema.ast)
    const report = InternalArbitrary.makeReport()
    InternalArbitrary.collectReport(schema.ast, report)
    return {
      value: lawc(FastCheck, {}),
      report: InternalArbitrary.toReport(report)
    }
  }
  return toArbitraryLazy(schema)(FastCheck)
}