veruniq
v1.1.4
Published
Form Validator declarative, rules sync/async, custom messages, nested path support. Tiny & extensible.
Maintainers
Readme
veruniq — Declarative Form Validator
A small, framework-agnostic JavaScript form validation library that supports declarative schemas, synchronous and asynchronous rules, nested path access, presets, and custom messages. Lightweight and easy to embed in Node or browser bundles.
Install
# from npm (if published)
npm install veruniq
# or use locally
npm installQuick Start
const { createValidator, registerPreset } = require('./index');
// optional: register a preset
registerPreset('strongPassword', [
'minLength:8',
{ rule: 'pattern:^(?=.*[A-Z])(?=.*\\d).+$', message: 'Password must include an uppercase letter and a digit' }
]);
const schema = {
'user.email': ['required', 'email'],
'password': ['required', 'minLength:6', 'preset:strongPassword'],
'age': [{ rule: 'min:18', message: 'You must be at least 18' }]
};
const validator = createValidator(schema, { abortEarly: true });
(async () => {
const { valid, errors } = await validator.validate({
user: { email: '[email protected]' },
password: 'Secret1',
age: 20
});
console.log(valid, errors);
// { valid: true, errors: {} }
})();API
- Entry point: index.js
- createValidator(schema, options) — main factory (see
createValidator)- options: { abortEarly: boolean (default true), messages: {} }
- returns an object with .validate(values) => Promise<{ valid, errors }>
Exports:
createValidatorregisterPreset- built-in
rules - default
messages - async helpers
asyncUtils
Core behavior:
- Supports rule formats: string shorthand (e.g.,
min:3), object ({ rule, params, message, when }), or custom function (sync or async). - Conditional rules via
whenordependsOn. - Presets allow composing multiple rules under a name (
preset:NAME).
Schema examples
- String shorthand:
"required","email","min:18","pattern:^\\d+$" - Object rule:
{ rule: 'minLength:6', message: 'Too short' }- Function rule (sync or Promise-returning):
async (value, ctx) => {
const ok = await checkUniqueEmail(value);
return ok || 'Email already taken';
}Files of interest
- Entry / public API: index.js
- Core validator: src/core.js — schema normalization and execution
- Built-in rule implementations: src/rules.js
- Messages map: src/messages.js
- Preset registry: src/presets.js
- Async helpers: src/asyncUtils.js
- Utility helpers: src/utils.js
Testing
Run provided tests (if dependencies installed):
npm testUnit tests and examples are located under the repository test/ and src/example/ (if present).
Contributing
- Fork the repository, create a branch, add tests for new behavior, and submit a PR.
- Follow existing code style and keep changes minimal and backwards compatible.
License
MIT — see package.json for license field.
