Built-in validation support for all common data types including strings, numbers, arrays, objects, and specialized formats like email and URL.
All supported validation types for the type property in rules.
type RuleType =
| "string" // Text validation with length and pattern support
| "number" // Numeric validation with range support
| "boolean" // Boolean type validation
| "method" // Function type validation
| "regexp" // RegExp object validation
| "integer" // Integer number validation
| "float" // Float number validation
| "array" // Array validation with length support
| "object" // Object type validation
| "enum" // Enumeration validation
| "date" // Date object validation
| "url" // URL format validation
| "hex" // Hexadecimal format validation
| "email" // Email format validation
| "pattern" // Pattern matching validation
| "any"; // Accept any typeText validation with comprehensive length, pattern, and whitespace checking.
Configuration Options:
min - Minimum string lengthmax - Maximum string lengthlen - Exact string lengthpattern - Regular expression patternwhitespace - Reject whitespace-only stringsUsage Examples:
const rules = {
// Basic string validation
title: { type: "string", required: true },
// Length constraints
username: { type: "string", min: 3, max: 20 },
// Exact length
zipCode: { type: "string", len: 5 },
// Pattern matching
slug: {
type: "string",
pattern: /^[a-z0-9-]+$/
},
// Whitespace validation
comment: {
type: "string",
whitespace: true, // Reject empty or whitespace-only
min: 1
}
};Numeric validation with range checking and type coercion.
Configuration Options:
min - Minimum numeric valuemax - Maximum numeric valuelen - Exact numeric value (equality check)Usage Examples:
const rules = {
// Basic number validation
age: { type: "number", required: true },
// Range constraints
score: { type: "number", min: 0, max: 100 },
// Exact value
magicNumber: { type: "number", len: 42 },
// Decimal numbers
price: { type: "number", min: 0.01 }
};Validates that numbers are integers (whole numbers).
Usage Examples:
const rules = {
// Basic integer validation
count: { type: "integer", min: 0 },
// Integer range
rating: { type: "integer", min: 1, max: 5 }
};Validates floating-point numbers.
Usage Examples:
const rules = {
// Float validation
temperature: { type: "float" },
// Float range
percentage: { type: "float", min: 0.0, max: 100.0 }
};Validates boolean values (true/false).
Usage Examples:
const rules = {
// Boolean validation
isActive: { type: "boolean", required: true },
// Optional boolean
newsletter: { type: "boolean" }
};Array validation with length constraints and element validation.
Configuration Options:
min - Minimum array lengthmax - Maximum array lengthlen - Exact array lengthdefaultField - Rule applied to all array elementsUsage Examples:
const rules = {
// Basic array validation
tags: { type: "array", required: true },
// Length constraints
items: { type: "array", min: 1, max: 10 },
// Exact length
coordinates: { type: "array", len: 2 },
// Element validation
emails: {
type: "array",
defaultField: { type: "email" },
min: 1
},
// Complex element validation
users: {
type: "array",
defaultField: {
type: "object",
fields: {
name: { type: "string", required: true },
age: { type: "number", min: 0 }
}
}
}
};Object validation with nested field validation.
Configuration Options:
fields - Specific validation rules for object propertiesdefaultField - Rule applied to all object propertiesUsage Examples:
const rules = {
// Basic object validation
profile: { type: "object", required: true },
// Nested field validation
user: {
type: "object",
fields: {
name: { type: "string", required: true },
email: { type: "email", required: true },
settings: {
type: "object",
fields: {
theme: { type: "string", enum: ["light", "dark"] },
notifications: { type: "boolean" }
}
}
}
},
// Dynamic object properties
metadata: {
type: "object",
defaultField: { type: "string" }
}
};Validates that values match one of the specified allowed values.
Configuration Options:
enum - Array of allowed valuesUsage Examples:
const rules = {
// String enumeration
status: {
type: "enum",
enum: ["active", "inactive", "pending"]
},
// Number enumeration
priority: {
type: "enum",
enum: [1, 2, 3, 4, 5]
},
// Mixed type enumeration
value: {
type: "enum",
enum: ["yes", "no", true, false, null]
}
};Validates Date objects and date strings.
Usage Examples:
const rules = {
// Date object validation
createdAt: { type: "date", required: true },
// Date string validation (will be parsed)
birthday: { type: "date" },
// Date with custom validation
eventDate: {
type: "date",
validator: (rule, value, callback) => {
const date = new Date(value);
const now = new Date();
if (date < now) {
callback("Event date must be in the future");
} else {
callback();
}
}
}
};Validates email address format using built-in email pattern.
Usage Examples:
const rules = {
// Basic email validation
email: { type: "email", required: true },
// Email with additional constraints
workEmail: {
type: "email",
pattern: /@company\.com$/,
message: "Must be a company email address"
}
};Validates URL format.
Usage Examples:
const rules = {
// Basic URL validation
website: { type: "url" },
// Required URL
homepage: { type: "url", required: true },
// URL with pattern constraint
apiEndpoint: {
type: "url",
pattern: /^https:\/\//,
message: "Must be a secure HTTPS URL"
}
};Validates hexadecimal format (e.g., color codes).
Usage Examples:
const rules = {
// Hex color validation
color: { type: "hex" },
// Required hex value
backgroundColor: { type: "hex", required: true },
// Hex with length constraint
shortHex: {
type: "hex",
len: 3 // #RGB format
}
};Validates that values are RegExp objects.
Usage Examples:
const rules = {
// RegExp validation
pattern: { type: "regexp", required: true },
// Validate user-provided regex
searchPattern: { type: "regexp" }
};Validates that values are functions.
Usage Examples:
const rules = {
// Function validation
callback: { type: "method", required: true },
// Optional function
transformer: { type: "method" }
};Special pattern-based validation that automatically sets type to "pattern" when a RegExp pattern is provided without an explicit type.
Usage Examples:
const rules = {
// Pattern validation (type automatically set to "pattern")
code: { pattern: /^[A-Z]{2}\d{4}$/ },
// Explicit pattern type
identifier: {
type: "pattern",
pattern: /^[a-f0-9-]{36}$/ // UUID format
}
};Accepts any value type without type checking.
Usage Examples:
const rules = {
// Accept any type
dynamicValue: { type: "any" },
// Any type with other constraints
flexibleField: {
type: "any",
required: true,
validator: (rule, value, callback) => {
// Custom validation logic
if (value === undefined || value === null) {
callback("Value cannot be null or undefined");
} else {
callback();
}
}
}
};Use arrays of rules to apply different type validations.
Usage Example:
const rules = {
// Accept string or number
mixedField: [
{ type: "string", min: 1 },
{ type: "number", min: 0 }
],
// Complex multi-type validation
flexibleId: [
{ type: "string", pattern: /^[a-z0-9-]+$/ },
{ type: "number", min: 1 }
]
};Register custom types for reuse across schemas.
Usage Example:
// Register custom phone type
Schema.register("phone", (rule, value, callback) => {
const phoneRegex = /^\+?[\d\s\-\(\)]{10,}$/;
if (!phoneRegex.test(value)) {
callback("Invalid phone number format");
} else {
callback();
}
});
// Use custom type
const rules = {
phone: { type: "phone", required: true }
};