Skip to content

Schemas API Reference

This page covers the main API functions available in @resourge/schema, including schema constructors, methods like compile, validate, and utilities.


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();
  • schemas: Record<string, BaseSchema> – Object shape describing each field’s validation.

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();
  • .min(length: number, message?: string)
  • .max(length: number, message?: string)
  • .email(message?: string)
  • .pattern(regex: RegExp, message?: string)
  • .required(message?: string)

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");
  • .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();
  • .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();
  • .minDate(date: Date, message?: string)
  • .maxDate(date: Date, message?: string)
  • .required(message?: string)

Creates a schema for validating boolean values.

import { boolean } from '@resourge/schema';
const boolSchema = boolean().required();
  • .required(message?: string)

Compiles the schema for optimized and consistent validation.

const schema = object({
name: string().min(2),
age: number().min(18)
}).compile();
compile({
debug?: boolean,
defaultNullable?: boolean,
defaultOptional?: boolean,
messages?: Record<string, string>,
onlyOnTouch?: boolean
});

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);
}
  • data: The input to be validated.
  • onlyOnTouch?: Optional array of keys to validate “only on touch”.

Returns a boolean indicating if the input passes validation.

const isValid = schema.isValid(data);
  • data: Input to validate.
  • onlyOnTouch?: Optional array of keys to validate only when changed.

Sets global default validation messages.

import { setupDefaultMessage, string } from '@resourge/schema';
setupDefaultMessage({
string: {
min: () => 'Too short!',
required: () => 'Required field',
}
});

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.