Hyperlinkv0.8.0-beta.28

SchemaParser

SchemaParser.assertsfunctioneffect/SchemaParser.ts:225
<S extends Schema.Constraint, I>(
  schema: S,
  input: I
): asserts input is I & S["Type"]

Asserts that an input satisfies the schema's decoded type side.

When to use

Use to assert that an input satisfies the decoded side of a schema when schema validation failures should throw an Error whose cause is SchemaIssue.Issue.

Details

The assertion returns normally when validation succeeds. When the input does not satisfy the schema with a schema-only failure, it throws an Error with the SchemaIssue.Issue in its cause.

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.

Asserting
export function asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"] {
  const parser = asExit(run<S["Type"], never>(SchemaAST.toType(schema.ast)))
  const exit = parser(input, SchemaAST.defaultParseOptions)
  if (Exit.isFailure(exit)) {
    const issue = InternalSchemaCause.getSchemaIssueOrThrow(
      exit.cause,
      "Assertion adapter can only throw schema issues"
    )
    throw new Error(issue.toString(), { cause: issue })
  }
}
Referenced by 1 symbols