Skip to content

Filters

Filters validate values after the underlying schema accepts them, without changing the decoded TypeScript type. Use synchronous predicates for local checks and effectful getters when validation needs services. Plain builders follow the call rules.

Predicate checks

const ShelfCode = Schema.String.check(
  Schema.makeFilter((code) => code.startsWith("S-") || "Use an S- shelf code")
)
 
effect function readShelf(input: unknown) {
  return try Schema.decodeUnknownEffect(ShelfCode)(input)
}
// Run in an effect function:
//   try Console.log(try readShelf("S-12"))
//   try Effect.catch(readShelf("A-12"), (error) => Console.log(error.message))
S-12
Use an S- shelf code

A predicate receives (value, ast, options):

  • Return true or undefined to accept.
  • Return false to reject with the default message, or a string for a custom message.
  • Return a SchemaIssue.Issue, a { path, issue } object, or an array of issues for structured diagnostics.

Filters preserve the underlying static type; add a brand when validated values need a distinct type. Plain predicates must be synchronous and pure—use SchemaGetter.checkEffect for service calls or typed failures.

Annotating checks

const FourDigits = Schema.String.check(
  Schema.makeFilter((text) => /^\d{4}$/.test(text), {
    expected: "four decimal digits",
    description: "The depot's four-digit routing code",
    toJsonSchema: () => ({ pattern: "^[0-9]{4}$" })
  })
)
// Run in an effect function:
//   try Console.log(Schema.is(FourDigits)("1234"))
//   try Effect.catch(Schema.decodeUnknownEffect(FourDigits)("abc"), (error) => Console.log(error.message))
true
Expected four decimal digits
  • expected feeds the default formatter (Expected ...); message overrides the whole message.
  • toJsonSchema exports the constraint to JSON Schema.
  • arbitraryConstraint guides Arbitrary generators when an opaque predicate alone cannot construct valid samples.

Issue paths and multiple issues

const DispatchWindow = Schema.Struct({
  start: Schema.Int,
  end: Schema.Int,
  destination: Schema.String
}).check(
  Schema.makeFilter((window) => {
    const issues: Array<Schema.FilterIssue> = []
    if (window.end <= window.start) {
      issues.push({ path: ["end"], issue: "End must follow start" })
    }
    if (window.destination.length === 0) {
      issues.push({ path: ["destination"], issue: "Choose a destination" })
    }
    return issues
  })
)
 
effect function validateWindow(input: unknown) {
  return try Schema.decodeUnknownEffect(DispatchWindow)(input, { errors: "all" })
}
// Run in an effect function: try Effect.catch(validateWindow({ start: 2, end: 1, destination: "" }), (error) => Console.log(error.message))
End must follow start
  at ["end"]
Choose a destination
  at ["destination"]

Paths are relative to the checked value (property keys or array indices). An empty array accepts the value. Cross-field struct checks run only after every field passes its own schema; errors: "all" collects independent issues across the struct.

Built-in constraints

const Label = Schema.String.check(
  Schema.isMinLength(2), Schema.isMaxLength(40), Schema.isTrimmed()
)
const Count = Schema.Finite.check(
  Schema.isInt(), Schema.isGreaterThanOrEqualTo(0), Schema.isMultipleOf(2)
)
const Ratio = Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 1 }))
const Batch = Schema.Array(Schema.String).check(Schema.isBetweenLength(1, 8))
const Serial = Schema.BigInt.check(Schema.isGreaterThanBigInt(0n))
// Run in an effect function: try Console.log(Schema.is(Label)(" ok"), Schema.is(Count)(4), Schema.is(Count)(3), Schema.is(Ratio)(2), Schema.is(Batch)([]), Schema.is(Serial)(1n))
false true false false false true

Checks validate without transforming—they never trim, round, or change case:

  • Strings: isNonEmpty, isPattern, isStartingWith, isEndingWith, isIncluding, isTrimmed, isLowercased, isUppercased, isCapitalized, isUncapitalized (plus the prebuilt Schema.NonEmptyString).
  • Lengths (strings and arrays): isMinLength, isMaxLength, isBetweenLength (isBetweenLength(n, n) for exact length).
  • Numbers & BigInts: strict and inclusive bounds, isBetween, isInt, isMultipleOf, and the BigInt-suffixed equivalents (isGreaterThanBigInt, isBetweenBigInt, etc.).
const ShipDate = Schema.Date.check(
  Schema.isBetweenDate({
    minimum: new Date("2026-01-01T00:00:00.000Z"),
    maximum: new Date("2026-12-31T23:59:59.999Z")
  })
)
 
function boundedPriceSchema() {
  const zero = BigDecimal.make(0n, 0)
  return Schema.BigDecimal.check(Schema.isGreaterThanOrEqualToBigDecimal(zero))
}
 
const HoldDuration = Schema.Duration.check(
  Schema.makeFilter((duration) => duration.between({
    minimum: Duration.seconds(1), maximum: Duration.seconds(30)
  }))
)
// Run in an effect function:
//   try Console.log(Schema.is(ShipDate)(new Date("2026-06-01T00:00:00Z")), Schema.is(ShipDate)(new Date("invalid")))
//   try Console.log(Schema.is(boundedPriceSchema())(BigDecimal.make(125n, 2)))
//   try Console.log(Schema.is(HoldDuration)(Duration.seconds(5)), Schema.is(HoldDuration)(Duration.seconds(31)))
true false
true
true false

Schema.Date rejects invalid Date instances before bounds run. Date and BigDecimal checks follow the Date and BigDecimal suffixes; durations use custom filters with Duration.isGreaterThan, isLessThan, their inclusive variants, or between. For bounds relative to the current time, read Clock inside an effect function and build the schema there.


The matching Effect page: effect.website/docs/v4/schema/filters