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.
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
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 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
Type-safe string validation
The value to check
Type guard for string
Check if the parameter matches one of the specified types.
The expected type for better TypeScript inference
The types to check against.
The parameter to check.
Returns the value cast to the expected type
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 an object against a schema.
The expected types for each key.
The object to validate.
Class representing a type checker. Version: 2.4.1
Author
Michael Lavigna
Since
2.4.1