Schemas API Reference
This page covers the main API functions available in @resourge/schema
, including schema constructors, methods like compile
, validate
, and utilities.
object
Section titled “object”Creates a schema for validating JavaScript objects.
import { object, string, number } from '@resourge/schema';
const userSchema = object({ name: string().required("Name is required"), age: number().min(18, "Must be at least 18"),}).compile();
Parameters
Section titled “Parameters”schemas: Record<string, BaseSchema>
– Object shape describing each field’s validation.
string
Section titled “string”Creates a schema for validating string values.
import { string } from '@resourge/schema';
const nameSchema = string() .min(3, "Minimum 3 characters") .max(20, "Maximum 20 characters") .pattern(/^[a-zA-Z]+$/, "Only letters allowed") .required();
Available methods
Section titled “Available methods”.min(length: number, message?: string)
.max(length: number, message?: string)
.email(message?: string)
.pattern(regex: RegExp, message?: string)
.required(message?: string)
number
Section titled “number”Creates a schema for validating numeric values.
import { number } from '@resourge/schema';
const ageSchema = number() .min(0) .max(120) .integer("Must be an integer") .positive("Must be positive");
Available methods
Section titled “Available methods”.min(value: number, message?: string)
.max(value: number, message?: string)
.integer(message?: string)
.positive(message?: string)
.required(message?: string)
Creates a schema for validating arrays.
import { array, string } from '@resourge/schema';
const tagsSchema = array(string().min(2)).min(1).required();
Available methods
Section titled “Available methods”.min(length: number, message?: string)
.max(length: number, message?: string)
.required(message?: string)
.unique(message?: string)
Creates a schema for validating Date
objects.
import { date } from '@resourge/schema';
const dateSchema = date() .minDate(new Date("2024-01-01"), "Too early") .maxDate(new Date("2024-12-31"), "Too late") .required();
Available methods
Section titled “Available methods”.minDate(date: Date, message?: string)
.maxDate(date: Date, message?: string)
.required(message?: string)
boolean
Section titled “boolean”Creates a schema for validating boolean values.
import { boolean } from '@resourge/schema';
const boolSchema = boolean().required();
Available methods
Section titled “Available methods”.required(message?: string)
compile
Section titled “compile”Compiles the schema for optimized and consistent validation.
const schema = object({ name: string().min(2), age: number().min(18)}).compile();
Optional config parameters
Section titled “Optional config parameters”compile({ debug?: boolean, defaultNullable?: boolean, defaultOptional?: boolean, messages?: Record<string, string>, onlyOnTouch?: boolean});
validate
Section titled “validate”Validates a value against a compiled schema. Returns an array of validation errors.
const result = schema.validate(data);if (result.length > 0) { console.log("Validation failed:", result);}
Parameters
Section titled “Parameters”data
: The input to be validated.onlyOnTouch?
: Optional array of keys to validate “only on touch”.
isValid
Section titled “isValid”Returns a boolean indicating if the input passes validation.
const isValid = schema.isValid(data);
Parameters
Section titled “Parameters”data
: Input to validate.onlyOnTouch?
: Optional array of keys to validate only when changed.
setupDefaultMessage
Section titled “setupDefaultMessage”Sets global default validation messages.
import { setupDefaultMessage, string } from '@resourge/schema';
setupDefaultMessage({ string: { min: () => 'Too short!', required: () => 'Required field', }});
onlyOnTouch
Section titled “onlyOnTouch”Optional optimization to validate only values that have changed.
const errors = schema.validate(newData, ['name', 'email']);
Some useful types exported by the library:
SchemaError
– shape of a validation error.CompileConfig
– configuration object for.compile()
.OnlyOnTouch<T>
– helper type to specify touched keys.