Hyperlinkv0.8.0-beta.28

SchemaParser

SchemaParser.makefunctioneffect/SchemaParser.ts:135
<S extends Schema.Constraint>(schema: S): (
  input: S["~type.make.in"],
  options?: Schema.MakeOptions
) => S["Type"]

Creates a synchronous maker for the schema's decoded type side.

When to use

Use to construct decoded schema values synchronously when invalid input should throw an Error whose cause is SchemaIssue.Issue.

Details

The returned function constructs a value from constructor input and throws an Error with the SchemaIssue.Issue in its cause when construction fails.

Gotchas

Causes that contain defects, interruptions, or asynchronous work at this synchronous boundary throw an Error whose cause is the underlying Cause, instead of being converted to a schema validation error.

constructors
export function make<S extends Schema.Constraint>(schema: S) {
  const parser = makeEffect(schema)
  return (input: S["~type.make.in"], options?: Schema.MakeOptions): S["Type"] => {
    const exit = Effect.runSyncExit(parser(input, options))
    if (Exit.isSuccess(exit)) {
      return exit.value
    }
    const issue = InternalSchemaCause.getSchemaIssueOrThrow(
      exit.cause,
      "Constructor adapter can only throw schema issues"
    )
    throw new Error(issue.toString(), { cause: issue })
  }
}