Built-in Extensions

▶ Open in Playground

The default import from @cleverbrush/schema includes a pre-applied extension pack with common validators. You get these methods automatically — no extra setup required:

String Extensions

MethodDescription
.email(errorMessage?)Validates email format
.url(opts?, errorMessage?)Validates URL format. opts.protocols narrows allowed schemes
.uuid(errorMessage?)Validates UUID (versions 1–5) format
.ip(opts?, errorMessage?)Validates IPv4 or IPv6. opts.version narrows to 'v4' or 'v6'
.trim()Preprocessor — trims whitespace before validation
.toLowerCase()Preprocessor — lowercases value before validation
.nonempty(errorMessage?)Rejects empty strings

Number Extensions

MethodDescription
.positive(errorMessage?)Value must be > 0
.negative(errorMessage?)Value must be < 0
.finite(errorMessage?)Value must be finite
.multipleOf(n, errorMessage?)Value must be an exact multiple of n (float-safe)

Array Extensions

MethodDescription
.nonempty(errorMessage?)Array must have at least one element
.unique(keyFn?, errorMessage?)All elements must be unique. Optional keyFn extracts comparison key

All validator extensions accept an optional error message as the last parameter — either a string or a function (matching the same ValidationErrorMessageProvider pattern used by built-in constraints like .minLength()):

import { string, number, array } from '@cleverbrush/schema';

// String error messages
const email = string().email('Please enter a valid email');
const age = number().positive('Age must be positive');
const tags = array().of(string()).nonempty('At least one tag required');

// Function error messages — receive the invalid value
const name = string().nonempty((val) => `"${val}" is not allowed`);
const score = number().multipleOf(5, (val) => `${val} is not a multiple of 5`);

The /core Sub-path

If you need bare builders without the built-in extensions (e.g. to apply only your own custom extensions), import from the /core sub-path:

// Bare builders — no built-in extensions
import { string, number, array, withExtensions } from '@cleverbrush/schema/core';

// Apply only your own extensions
const s = withExtensions(myCustomExtension);

The default import re-exports everything from /core and overrides the nine factory functions with pre-extended versions. The extension descriptors themselves (stringExtensions, numberExtensions, arrayExtensions) are also exported so you can compose them with your own.