const { Validator } = require('ata-validator');Creates a validator instance. Does not compile anything until first use (lazy compilation).
const v = new Validator({
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer', minimum: 0 }
},
required: ['name']
});Options:
| Option | Type | Default | Description |
|---|---|---|---|
coerceTypes |
boolean | false | Convert types in-place. "42" becomes 42 for integer fields. |
removeAdditional |
boolean | false | Remove properties not defined in schema. |
Validates data. Applies defaults, coercion, and removal if configured. Returns a result object.
const result = v.validate({ name: 'Mert', age: 26 });
// Valid:
// { valid: true, errors: [] }
// Invalid:
// {
// valid: false,
// errors: [
// { code: 'type_mismatch', path: '/age', message: 'expected integer' },
// { code: 'required_missing', path: '/name', message: 'missing: name' }
// ]
// }Error codes:
type_mismatch- wrong typerequired_missing- required property missingminimum_violation/maximum_violation- number out of rangemin_length_violation/max_length_violation- string length out of rangepattern_mismatch- string doesn't match patternformat_mismatch- string doesn't match format (email, date, etc.)enum_mismatch- value not in enumconst_mismatch- value doesn't match constunique_items_violation- duplicate items in arrayadditional_property- property not defined in schemamin_items_violation/max_items_violation- array length out of range
Boolean-only check. Fastest path, no error details, no defaults/coercion.
v.isValidObject({ name: 'Mert', age: 26 }); // true
v.isValidObject({ name: 123 }); // falseValidates a JSON string. Uses simdjson for large documents (>8KB).
const result = v.validateJSON('{"name": "Mert", "age": 26}');
// { valid: true, errors: [] }Boolean-only check on JSON string.
v.isValidJSON('{"name": "Mert"}'); // trueRaw NAPI path for Buffer/Uint8Array input. No JS parsing, goes straight to C++.
v.isValid(Buffer.from('{"name": "Mert"}')); // trueMulti-core parallel validation of NDJSON (newline-delimited JSON). Returns array of booleans.
const ndjson = Buffer.from('{"name":"a"}\n{"name":"b"}\n{"bad":1}');
v.isValidParallel(ndjson); // [true, true, false]Counts valid documents in NDJSON buffer. Single number return, fastest batch path.
v.countValid(ndjson); // 2Single-thread NDJSON batch validation. Returns array of booleans.
Zero-copy path for pre-padded buffers (simdjson requires 64 bytes padding).
Skip compilation at startup by saving compiled validators to disk.
Returns JS module source string.
const source = v.toStandalone();
fs.writeFileSync('./compiled.js', source);Loads a pre-compiled validator. No native addon needed.
const v = Validator.fromStandalone(require('./compiled.js'), schema);Bundles multiple schemas into a single JS file.
const bundle = Validator.bundle([schema1, schema2, schema3]);
fs.writeFileSync('./validators.js', bundle);Deduplicated bundle. Identical function bodies are shared.
const bundle = Validator.bundleCompact(schemas);
// 5 schema types across 500 routes -> 17KB vs 3.6MB without dedupLoads a bundle.
const validators = Validator.loadBundle(require('./validators.js'), schemas);
validators[0].validate(data);ata-validator implements the Standard Schema interface.
const v = new Validator(schema);
const result = v['~standard'].validate(data);
// Valid: { value: data }
// Invalid: { issues: [{ message: 'expected string', path: [{ key: 'name' }] }] }Works with Fastify v5, tRPC, TanStack Form, Drizzle ORM.
One-shot validation without creating a Validator instance. Uses native C++ path.
const { validate } = require('ata-validator');
const result = validate({ type: 'string' }, 'hello');Creates a simdjson-compatible padded buffer.
const { createPaddedBuffer, SIMDJSON_PADDING } = require('ata-validator');
const { buffer, length } = createPaddedBuffer('{"name": "Mert"}');
v.isValidPrepadded(buffer, length);ata uses three-tier lazy compilation:
- Constructor - stores schema only, no compilation (~0.5us)
- First validate() - compiles codegen + combined validator
- First isValidObject() - compiles only boolean codegen (faster)
Same schema string reuses compiled functions from cache. Second new Validator(sameSchema) skips compilation entirely.
Hand-written parsers, no regex:
| Format | Example |
|---|---|
email |
user@example.com |
date |
2026-03-28 |
date-time |
2026-03-28T12:00:00Z |
time |
12:00:00 |
uri |
https://example.com |
uri-reference |
/path/to/resource |
ipv4 |
192.168.1.1 |
ipv6 |
::1 |
uuid |
550e8400-e29b-41d4-a716-446655440000 |
hostname |
example.com |