Validates and returns an array
The expected element type
The value to validate
The validated array
Validates and returns an object
The expected object type
The value to validate
The validated object
Recursively validates an object against a nested schema.
The expected structure definition.
The object to validate.
The current path for error reporting (internal use).
Whether to reject extra keys not in schema.
Expects a function to conform to specified input and output types.
The function to type-check.
The expected types for the function's parameters and return value.
Defines the expected input and output types for a function.
The expected type(s) of the function's parameters
The expected return type(s) of the function
A new function that type-checks its arguments and return value.
Checks if the provided value matches one or more specified types.
Overloads:
"number" narrows to number).T may be supplied.Returns true if the value matches any type, false otherwise.
Checks if the provided value matches one or more specified types.
Overloads:
"number" narrows to number).T may be supplied.The expected type for better TypeScript inference
The value to check.
One or more types to check against.
Returns true if the value matches any type, false otherwise.
Type-safe array validation
The expected element type
The value to check
Type guard for array
Checks if the provided parameter is an array of a specified type.
The expected element type
The type of elements that the array should contain.
The parameter to check.
Typed array of elements
Checks that the parameter is a syntactically valid Base64 string.
Supports both standard and URL-safe variants. Padding is required when
requirePadding is true (default).
The parameter to check
Optionalopts: { requirePadding?: boolean; urlSafe?: boolean } = {}Options
The validated Base64 string
Type-safe boolean validation
The value to check
Type guard for boolean
Checks if the provided parameter is a number within a specified range.
The minimum value.
The maximum value.
The parameter to check.
The validated number
Checks that the parameter is an instance of the given constructor.
Type-safe alternative to writing value instanceof MyClass everywhere.
The instance type produced by the constructor
The constructor to check against
The parameter to check
The validated instance
Checks that the parameter is a valid IPv6 address.
Uses the URL constructor as a permissive parser: any string accepted as
the host portion of http://[<addr>]/ is considered valid.
The parameter to check
The validated IPv6 address
Checks that the parameter is a valid ISO 8601 date string and returns
the parsed Date. Accepts the formats produced by Date#toISOString
plus reasonable variants (e.g. with timezone offsets).
The parameter to check
The parsed date (always valid)
Checks that the length of a string or array falls within the given bounds.
Either string or an array type
Inclusive length bounds
The parameter to check (string or array)
The validated value
Inverse of isEmpty: checks the parameter is a non-empty string, array,
Map, Set, or object.
Caller-supplied container type for narrower inference
The parameter to check
The validated non-empty value
Checks if the provided parameter is a non-empty array.
The expected element type
The parameter to check.
The validated non-empty array
Type-safe number validation
The value to check
Type guard for number
Type-safe object validation
The expected object type
The value to check
Type guard for object
Checks if the provided parameter is a valid phone number.
The parameter to check.
The validated phone number string
Checks that the parameter is a plain object (object literal or
Object.create(null)). Rejects class instances, arrays, dates, maps, etc.
The expected plain object shape
The parameter to check
The validated plain object
Checks that the parameter is a Promise (or a thenable).
The resolved promise type (caller-supplied)
The parameter to check
The validated promise
Type-safe string validation
The value to check
Type guard for string
Check if the parameter matches one of the specified types.
Overloads:
"string" → string).T, which falls
back to unknown.Returns the value cast to the expected type
Check if the parameter matches one of the specified types.
Overloads:
"string" → string).T, which falls
back to unknown.The expected type for better TypeScript inference
The types to check against.
The parameter to check.
Returns the value cast to the expected type
Universal "parse" entry point. Validates value against either:
"string", "number", ...),["string", "number"] → union),Validator<T> function,Returns the value typed correctly. Throws a TypeError on failure.
No as const is needed when calling with a literal schema thanks to
the <const S> parameter — the inferred type matches the schema.
Universal "parse" entry point. Validates value against either:
"string", "number", ...),["string", "number"] → union),Validator<T> function,Returns the value typed correctly. Throws a TypeError on failure.
No as const is needed when calling with a literal schema thanks to
the <const S> parameter — the inferred type matches the schema.
Universal "parse" entry point. Validates value against either:
"string", "number", ...),["string", "number"] → union),Validator<T> function,Returns the value typed correctly. Throws a TypeError on failure.
No as const is needed when calling with a literal schema thanks to
the <const S> parameter — the inferred type matches the schema.
Universal "parse" entry point. Validates value against either:
"string", "number", ...),["string", "number"] → union),Validator<T> function,Returns the value typed correctly. Throws a TypeError on failure.
No as const is needed when calling with a literal schema thanks to
the <const S> parameter — the inferred type matches the schema.
Register a new type in the typesMap.
The input type that the validator expects
The return type that the validator produces
// Register a positive number validator
typer.registerType<unknown, number>("positive", (value) => {
if (typeof value !== "number" || value <= 0) throw new TypeError("Must be positive");
return value;
});
// Register a string length validator
typer.registerType<unknown, string>("longString", (value) => {
if (typeof value !== "string" || value.length < 10) throw new TypeError("Must be long string");
return value;
});
Validates value without throwing. Same input shapes as parse.
Returns a discriminated union: { success: true, data } or
{ success: false, error }.
Validates value without throwing. Same input shapes as parse.
Returns a discriminated union: { success: true, data } or
{ success: false, error }.
Validates value without throwing. Same input shapes as parse.
Returns a discriminated union: { success: true, data } or
{ success: false, error }.
Validates value without throwing. Same input shapes as parse.
Returns a discriminated union: { success: true, data } or
{ success: false, error }.
Identity helper that preserves literal types of a schema declared as
a variable. Use it when you want to declare the schema once, derive
Infer<typeof schema>, and then call parse(schema, value) with
full type inference — without sprinkling as const.
Combines multiple validators into one that succeeds if any of them succeeds. The first matching validator's result is returned.
Tuple of types produced by each validator
A new validator that returns the first matching result
Validates an object against a schema.
The expected types for each key.
The object to validate.
Class representing a type checker. Version: 3.2.3
Author
Michael Lavigna - https://michaellavigna.com - michael.lavigna@hotmail.it
Since
3.2.3