Import
What it does
ResponseValidator validates runtime data against a lightweight schema shape. Use it when you want built-in validation without depending on Zod, Valibot, or another schema library. It checks types, required fields, string/number constraints, patterns, enums, and custom predicates.
Static method
ResponseValidator.validate(data, schema, path?)
| Parameter | Type | Description |
|---|---|---|
data | unknown | The value to validate. |
schema | ValidationSchema | Schema definition describing the expected shape. |
path | string | (Optional) Dot-path prefix for error messages. Defaults to "root". |
ValidationError[] — an array of errors. Empty means valid.
ValidationError
| Property | Type | Description |
|---|---|---|
path | string | Dot-path to the invalid field (e.g. root.email). |
message | string | Human-readable error description. |
value | unknown | The actual value that failed validation. |
ValidationSchema
| Property | Type | Description |
|---|---|---|
type | "string" | "number" | "boolean" | "object" | "array" | Expected type. |
required | boolean | If true, rejects null and undefined. |
properties | Record<string, ValidationSchema> | Nested schemas for object properties. |
items | ValidationSchema | Schema applied to each element of an array. |
minLength | number | Minimum string length. |
maxLength | number | Maximum string length. |
minimum | number | Minimum number value. |
maximum | number | Maximum number value. |
pattern | RegExp | string | Regex pattern the string must match. |
enum | unknown[] | Allowed values. |
custom | (value: unknown) => boolean | string | Custom validator. Return true to pass, or a string error message to fail. |
Examples
Basic object validation
Array with item schema
Custom validator
Using with ApiClient
You can pass aValidationSchema directly to the validateResponse option on any ApiClient request:
When to use it
- You want a package-native validator with no extra dependencies
- You only need structural and constraint checks at runtime
- You want to keep your bundle lean