Skip to content

API Reference

The @hedystia/validations package provides a robust schema builder h to create type-safe schemas for your API.

Core Schemas

These are the starting points for any validation schema.

MethodReturn TypeDescription
h.string()StringSchemaTypeValidates that a value is a string.
h.number()NumberSchemaTypeValidates that a value is a number (excluding NaN).
h.boolean()BooleanSchemaTypeValidates that a value is a boolean (true/false).
h.any()AnySchemaTypeAccepts any value without validation.
h.null()NullSchemaTypeValidates that a value is exactly null.
h.literal(value)LiteralSchemaValidates an exact match for a string, number, or boolean.
h.object(definition)ObjectSchemaTypeValidates an object against a key-value schema definition.
h.array(schema)ArraySchemaValidates an array where every item matches the provided schema.
h.enum(values)UnionSchemaValidates that a value matches one of the provided literal values in the array.
h.options(...schemas)UnionSchemaValidates a union of multiple schemas (similar to Zod's union).
h.instanceOf(class)InstanceOfSchemaValidates that a value is an instance of the specified class.
h.date()StringSchemaTypeAlias for h.string().date().
h.uuid()StringSchemaTypeAlias for h.string().uuid().
h.email()StringSchemaTypeAlias for h.string().email().
h.phone()StringSchemaTypeAlias for h.string().phone().
h.domain()StringSchemaTypeAlias for h.string().domain().

String Modifiers

Methods available on h.string().

MethodDescription
.minLength(n)Ensures the string has at least n characters.
.maxLength(n)Ensures the string has at most n characters.
.email()Validates common email format.
.uuid()Validates UUID v4 format.
.regex(ptrn)Matches the string against a custom Regular Expression.
.phone()Validates international phone formats.
.domain(http?)Validates domain/URL format. If true (default), requires http or https.
.date()Validates that the string is a valid parseable ISO date.
.coerce()Converts incoming non-string values to string automatically.

Number Modifiers

Methods available on h.number().

MethodDescription
.min(n)Ensures the number is greater than or equal to n.
.max(n)Ensures the number is less than or equal to n.
.coerce()Converts strings or numeric types to number automatically.

General Modifiers

Methods available on most schemas to change their behavior.

MethodDescription
.optional()Allows the value to be undefined. Maps to `T
.null()Allows the value to be null. Maps to `T
.nullable()Alias for .null(). Allows the value to be null. Maps to `T
.array()Transforms the schema into an array of that type: T[].
.coerce()Enables type coercion for primitive types (string, number, boolean).

Type Utilities

HelperDescription
Infer<typeof T>TypeScript utility to extract the output type of a schema.