Hyperlinkv0.8.0-beta.28

Brand

Brand.checkfunctioneffect/Brand.ts:274
<A extends Brand<any>>(checks_0: SchemaAST.Check<Brand<in out Keys extends string>.Unbranded<A>>, ...checks: Array<SchemaAST.Check<Brand.Unbranded<A>>>): Constructor<A>

Creates a branded type Constructor from one or more schema checks.

When to use

Use when you need a branded type constructor that performs runtime validation via schema checks.

Details

Calling the returned constructor validates the unbranded value and throws on failure. Use the returned option, result, or is methods for non-throwing validation.

constructorsnominalall
Source effect/Brand.ts:27416 lines
export function check<A extends Brand<any>>(
  ...checks: readonly [
    SchemaAST.Check<Brand.Unbranded<A>>,
    ...Array<SchemaAST.Check<Brand.Unbranded<A>>>
  ]
): Constructor<A> {
  const result = (input: Brand.Unbranded<A>): Result.Result<A, BrandError> => {
    return Result.mapError(SchemaAST.runChecks(checks, input), (issue) => new BrandError(issue)) as any
  }
  return Object.assign((input: Brand.Unbranded<A>) => Result.getOrThrow(result(input)), {
    option: (input: Brand.Unbranded<A>) => Option.getSuccess(result(input)),
    result,
    is: (input: Brand.Unbranded<A>): input is Brand.Unbranded<A> & A => Result.isSuccess(result(input)),
    checks
  })
}
Referenced by 2 symbols