Core Quick Start
Model
interface AccountModel {
profile: {
name: string;
email: string;
};
seatCount: number | '';
}
Policy
import type { ValidationPolicy, Validator, ValidatorHelper } from '@validation-rules-engine/core';
class AccountPolicy implements ValidationPolicy {
addValidations(helper: ValidatorHelper): Validator[] {
return [
helper.validateFor('profile.name').isRequired('Name is required'),
helper.validateFor('profile.email')
.isRequired('Email is required')
.isEmail('Enter a valid email address'),
helper.validateFor('seatCount')
.isRequired('Seat count is required')
.isNumber('Seat count must be numeric')
.range('Seat count must be between 1 and 500', 1, 500, 'number')
];
}
}
Register through an adapter or Core-only
Angular:
validationProvider.register('Account', new AccountPolicy());
React:
const policies = [{ name: 'account', policy: new AccountPolicy() }];
const form = useValidationForm({ initialModel, policies, policyNames: ['account'] });
Vanilla / Core-only:
Build validators from the policy and evaluate them in application code, or open the live Vanilla Showcase to see DOM wiring for the same Core contracts.
const helper = new ValidatorHelper();
const validators = new AccountPolicy().addValidations(helper);
Read results
const firstError = model.validationResults?.[0];
console.log(firstError?.propertyName);
console.log(firstError?.error.message);
Adapters expose the same result shape through directives, hooks, summaries, and group status helpers.