<S extends Constraint, I>(schema: S, input: I): asserts input is I &
S["Type"]Creates an assertion function that throws an error if the input does not match the schema.
When to use
Use to validate unknown input at runtime while narrowing the value with a TypeScript assertion signature.
Details
The input is narrowed if the assertion succeeds. If schema validation fails,
the assertion throws an Error whose cause is SchemaIssue.Issue.
Gotchas
Causes that contain defects, interruptions, or other non-schema reasons throw
with the underlying Cause attached instead of being converted to schema
validation errors.
Example (Asserting and narrowing an input)
import { Schema } from "effect"
const input: unknown = "hello"
// This will pass silently (no return value) and narrow input to string
Schema.asserts(Schema.String, input)
console.log(input.toUpperCase())
// This will throw an error
try {
const invalid: unknown = 123
Schema.asserts(Schema.String, invalid)
} catch (error) {
console.log("Non-string assertion failed as expected")
}export const const asserts: <S extends Constraint, I>(
schema: S,
input: I
) => asserts input is I & S["Type"]
Creates an assertion function that throws an error if the input does not match
the schema.
When to use
Use to validate unknown input at runtime while narrowing the value with a
TypeScript assertion signature.
Details
The input is narrowed if the assertion succeeds. If schema validation fails,
the assertion throws an Error whose cause is SchemaIssue.Issue.
Gotchas
Causes that contain defects, interruptions, or other non-schema reasons throw
with the underlying Cause attached instead of being converted to schema
validation errors.
Example (Asserting and narrowing an input)
import { Schema } from "effect"
const input: unknown = "hello"
// This will pass silently (no return value) and narrow input to string
Schema.asserts(Schema.String, input)
console.log(input.toUpperCase())
// This will throw an error
try {
const invalid: unknown = 123
Schema.asserts(Schema.String, invalid)
} catch (error) {
console.log("Non-string assertion failed as expected")
}
asserts: <function (type parameter) S in <S extends Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S extends Constraint, function (type parameter) I in <S extends Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]I>(schema: S extends Constraintschema: function (type parameter) S in <S extends Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S, input: Iinput: function (type parameter) I in <S extends Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]I) => asserts input: Iinput is function (type parameter) I in <S extends Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]I & function (type parameter) S in <S extends Constraint, I>(schema: S, input: I): asserts input is I & S["Type"]S["Type"] =
import SchemaParserSchemaParser.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