API Reference Overview
Complete reference for all ValidlyJS classes, methods, interfaces, and types.
Overview
This reference covers all public APIs available in ValidlyJS, including the main Validator class, fluent builders, utility functions, and TypeScript interfaces.
Validator Class
The main validation class that handles schema-based validation.
Note: The Validator class is the core of ValidlyJS, providing methods for synchronous and asynchronous validation, custom rule extension, and configuration.
Constructor
constructor(schema: ValidationSchema, options?: ValidatorOptions)
Creates a new validator instance with the specified schema and options.
Parameters
schema
- Validation schema defining rules for each fieldoptions
- Optional configuration options
import { Validator } from 'validlyjs';
const validator = new Validator({
name: 'required|string|min:2',
email: 'required|email',
age: 'required|integer|min:18'
}, {
responseType: 'laravel',
language: 'en',
stopOnFirstError: false
});
validate(data)
async validate(data: Record): Promise
Validates data asynchronously against the schema. Supports both sync and async rules.
Parameters
data
- Object containing data to validate
Returns
Promise
- Validation result with errors and validity status
const result = await validator.validate({
name: 'John Doe',
email: 'john@example.com',
age: 25
});
console.log(result.isValid); // true
console.log(result.errors); // {}
console.log(result.data); // Original data
validateSync(data)
validateSync(data: Record): ValidationResult
Validates data synchronously. Only executes synchronous rules.
Parameters
data
- Object containing data to validate
Returns
ValidationResult
- Validation result (sync only)
const result = validator.validateSync({
name: 'John',
email: 'invalid-email',
age: 17
});
console.log(result.isValid); // false
console.log(result.errors); // { email: [...], age: [...] }
extend(name, rule)
extend(name: string, rule: CustomRuleDefinition): void
Adds a custom validation rule to this validator instance.
Parameters
name
- Name of the custom rulerule
- Rule definition object
validator.extend('strongPassword', {
validate: (value) => {
return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/.test(value);
},
message: 'Password must be at least 8 characters with uppercase, lowercase, number and special character.'
});
setLanguage(language)
setLanguage(language: string): void
Changes the language for error messages.
Parameters
language
- Language code (e.g., 'en', 'es', 'fr')
validator.setLanguage('es'); // Switch to Spanish