<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.
export function function asserts<
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.
asserts<function (type parameter) S in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S extends import SchemaSchema.Constraint, function (type parameter) I in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]I>(schema: S extends Schema.Constraintschema: function (type parameter) S in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S, input: Iinput: function (type parameter) I in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]I): asserts input: Iinput is function (type parameter) I in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]I & function (type parameter) S in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S["Type"] {
const const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Exit.Exit<S["Type"], SchemaIssue.Issue>
parser = function asExit<T, E, R>(
parser: (
input: E,
options?: SchemaAST.ParseOptions
) => Effect.Effect<T, SchemaIssue.Issue, R>
): (
input: E,
options?: SchemaAST.ParseOptions
) => Exit.Exit<T, SchemaIssue.Issue>
asExit(function run<T, R>(
ast: SchemaAST.AST
): (
input: unknown,
options?: SchemaAST.ParseOptions
) => Effect.Effect<T, SchemaIssue.Issue, R>
run<function (type parameter) S in asserts<S extends Schema.Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S["Type"], never>(import SchemaASTSchemaAST.const toType: <SchemaAST.AST>(ast: SchemaAST.AST) => SchemaAST.ASTStrips all encoding transformations from an AST, returning the decoded
(type-level) representation.
Details
- Memoized: same input reference → same output reference.
- Recursively walks into composite nodes (
Arrays
,
Objects
,
Example (Getting the type AST)
import { Schema, SchemaAST } from "effect"
const schema = Schema.NumberFromString
const typeAst = SchemaAST.toType(schema.ast)
console.log(typeAst._tag) // "Number"
toType(schema: S extends Schema.Constraintschema.Constraint["ast"]: SchemaAST.ASTast)))
const const exit: Exit.Exit<
S["Type"],
SchemaIssue.Issue
>
exit = const parser: (
input: unknown,
options?: SchemaAST.ParseOptions
) => Exit.Exit<S["Type"], SchemaIssue.Issue>
parser(input: Iinput, import SchemaASTSchemaAST.const defaultParseOptions: ParseOptionsconst defaultParseOptions: {
errors: "first" | "all" | undefined;
onExcessProperty: "ignore" | "error" | "preserve" | undefined;
propertyOrder: "none" | "original" | undefined;
disableChecks: boolean | undefined;
concurrency: number | "unbounded" | undefined;
}
defaultParseOptions)
if (import ExitExit.const isFailure: <A, E>(
self: Exit<A, E>
) => self is Failure<A, E>
Checks whether an Exit is a Failure.
When to use
Use as a type guard to narrow Exit<A, E> to Failure<A, E> and access the
cause property.
Example (Narrowing to failure)
import { Exit } from "effect"
const exit = Exit.fail("error")
if (Exit.isFailure(exit)) {
console.log(exit.cause)
}
isFailure(const exit: Exit.Exit<
S["Type"],
SchemaIssue.Issue
>
exit)) {
const const issue: SchemaIssue.Issueissue = import InternalSchemaCauseInternalSchemaCause.function getSchemaIssueOrThrow(
cause: Cause.Cause<SchemaIssue.Issue>,
message: string
): SchemaIssue.Issue
getSchemaIssueOrThrow(
const exit: Exit.Failure<
S["Type"],
SchemaIssue.Issue
>
const exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
exit.Failure<S["Type"], Issue>.cause: Cause.Cause<E>(property) Failure<S["Type"], Issue>.cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause,
"Assertion adapter can only throw schema issues"
)
throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error(const issue: SchemaIssue.Issueissue.Base.toString(this: Issue): stringtoString(), { ErrorOptions.cause?: SchemaIssue.Issuecause: const issue: SchemaIssue.Issueissue })
}
}