Hyperlinkv0.8.0-beta.28

SchemaAST

SchemaAST.isPatternfunctioneffect/SchemaAST.ts:3048
(
  regExp: globalThis.RegExp,
  annotations?: Schema.Annotations.Filter
): Filter<string>

Creates a Filter that validates strings by running RegExp.test.

When to use

Use when string validation should be represented as a schema Filter backed by a regular expression.

Details

The filter can be used with Schema.filter or attached directly to a String AST node through checks. The regular expression source is stored in annotations for serialization and arbitrary generation.

Gotchas

Use a non-global, non-sticky regular expression, or reset lastIndex yourself, because RegExp.test is stateful for expressions with the g or y flag.

Example (Validating an email pattern)

import { SchemaAST } from "effect"

const emailFilter = SchemaAST.isPattern(/^[^@]+@[^@]+$/)
constructorsFilter
export function isPattern(regExp: globalThis.RegExp, annotations?: Schema.Annotations.Filter) {
  const source = regExp.source
  return makeFilter(
    (s: string) => regExp.test(s),
    {
      expected: `a string matching the RegExp ${source}`,
      meta: {
        _tag: "isPattern",
        regExp
      },
      arbitrary: {
        constraint: {
          patterns: [regExp.source]
        }
      },
      ...annotations
    }
  )
}
Referenced by 1 symbols