effect-ts-laws
    Preparing search index...

    Function schemaLaws

    • Build the @effect/schema laws from a schema.

      Type Parameters

      • A

        Decoded type.

      • I

        Encoded type.

      Parameters

      • schema: Schema<A, I>

        The schema under test.

      Returns LawSet

      The schema laws for the given schema.

      // Test schema laws are respected in the “Person” schema.
      import {checkLaws, schemaLaws} from 'effect-ts-laws'
      import {Schema, Equivalence as EQ, pipe} from 'effect'

      // String equality that understand integer equivalences such as
      // ('+01' === ' 1.0 '), ('NaN' === 'NaN'.), and ('1e1' === '0xA').
      const equivalence: EQ.Equivalence<string> = (self, that) =>
      self === that ||
      parseInt(self) === parseInt(that) ||
      parseFloat(self) === parseFloat(that)

      const Age: Schema.Schema<number, string> = pipe(
      Schema.String.annotations({equivalence: () => equivalence}),
      Schema.parseNumber,
      Schema.int(),
      Schema.positive(),
      )

      // Here “age” is of type number.
      interface Person extends Schema.Schema.Type<typeof _Person> {}

      // But here “age” is of type string.
      interface PersonEncoded extends Schema.Schema.Encoded<typeof _Person> {}

      const _Person = Schema.Struct({name: Schema.String, age: Age})

      const Person: Schema.Schema<Person, PersonEncoded> = _Person

      const laws = schemaLaws(Person)

      console.table(checkLaws(laws))