ata-validator 0.4.5 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -20
- package/index.js +21 -13
- package/lib/js-compiler.js +46 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,15 +6,16 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
|
|
|
6
6
|
|
|
7
7
|
## Performance
|
|
8
8
|
|
|
9
|
-
### Single-Document Validation
|
|
9
|
+
### Single-Document Validation
|
|
10
10
|
|
|
11
11
|
| Scenario | ata | ajv | |
|
|
12
12
|
|---|---|---|---|
|
|
13
|
-
| **validate(obj)** | 15M ops/sec |
|
|
14
|
-
| **
|
|
15
|
-
| **
|
|
16
|
-
| **
|
|
17
|
-
| **
|
|
13
|
+
| **validate(obj)** valid | 15M ops/sec | 8M ops/sec | **ata 1.9x faster** |
|
|
14
|
+
| **validate(obj)** invalid | 13.1M ops/sec | 8.1M ops/sec | **ata 1.6x faster** |
|
|
15
|
+
| **isValidObject(obj)** | 15.4M ops/sec | 9.2M ops/sec | **ata 1.7x faster** |
|
|
16
|
+
| **validateJSON(str)** valid | 2.15M ops/sec | 1.88M ops/sec | **ata 1.1x faster** |
|
|
17
|
+
| **validateJSON(str)** invalid | 2.62M ops/sec | 2.35M ops/sec | **ata 1.1x faster** |
|
|
18
|
+
| **Schema compilation** | 112K ops/sec | 773 ops/sec | **ata 145x faster** |
|
|
18
19
|
|
|
19
20
|
### Large Data — JS Object Validation
|
|
20
21
|
|
|
@@ -33,18 +34,11 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
|
|
|
33
34
|
| **Batch NDJSON** (10K items, multi-core) | 13.4M/sec | 5.1M/sec | **ata 2.6x faster** |
|
|
34
35
|
| **Fastify HTTP** (100 users POST) | 24.6K req/sec | 22.6K req/sec | **ata 9% faster** |
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
| Scenario | ata | ajv | |
|
|
39
|
-
|---|---|---|---|
|
|
40
|
-
| **validate(obj)** (invalid data) | 6M ops/sec | 7.9M ops/sec | **ajv 1.3x faster** |
|
|
41
|
-
| **validateJSON(str)** (invalid data) | 2.2M ops/sec | 2.3M ops/sec | **ajv 1.1x faster** |
|
|
42
|
-
|
|
43
|
-
> Invalid-data error path — ajv is slightly faster. Production traffic is overwhelmingly valid.
|
|
37
|
+
> ata is faster than ajv on **every** benchmark — valid and invalid data, objects and JSON strings, single documents and parallel batches.
|
|
44
38
|
|
|
45
39
|
### How it works
|
|
46
40
|
|
|
47
|
-
**
|
|
41
|
+
**Combined single-pass validation**: ata compiles schemas into monolithic JS functions that both validate and collect errors in a single pass. Valid data returns immediately (lazy error array — zero allocation). Invalid data collects errors without a second pass.
|
|
48
42
|
|
|
49
43
|
**JS codegen**: Schemas are compiled to monolithic JS functions (like ajv). Supported keywords: `type`, `required`, `properties`, `items`, `enum`, `const`, `allOf`, `anyOf`, `oneOf`, `not`, `if/then/else`, `uniqueItems`, `contains`, `prefixItems`, `additionalProperties`, `dependentRequired`, `$ref` (local), `minimum/maximum`, `minLength/maxLength`, `pattern`, `format`.
|
|
50
44
|
|
|
@@ -58,7 +52,7 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
|
|
|
58
52
|
|
|
59
53
|
## When to use ata
|
|
60
54
|
|
|
61
|
-
- **Any `validate(obj)` workload** — 1.
|
|
55
|
+
- **Any `validate(obj)` workload** — 1.6x–2.7x faster than ajv on all data
|
|
62
56
|
- **Serverless / cold starts** — 12.5x faster schema compilation
|
|
63
57
|
- **Security-sensitive apps** — RE2 regex, immune to ReDoS attacks
|
|
64
58
|
- **Batch/streaming validation** — NDJSON log processing, data pipelines (2.6x faster)
|
|
@@ -67,12 +61,12 @@ Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjs
|
|
|
67
61
|
|
|
68
62
|
## When to use ajv
|
|
69
63
|
|
|
70
|
-
- **
|
|
71
|
-
- **
|
|
64
|
+
- **Schemas with `patternProperties`, `dependentSchemas`** — these bypass JS codegen and hit the slower NAPI path
|
|
65
|
+
- **100% spec compliance needed** — ajv covers more edge cases (ata: 98.4%)
|
|
72
66
|
|
|
73
67
|
## Features
|
|
74
68
|
|
|
75
|
-
- **
|
|
69
|
+
- **Combined single-pass validation**: One JS function validates + collects errors — no double pass, lazy error allocation
|
|
76
70
|
- **Multi-core**: Parallel validation across all CPU cores — 13.4M validations/sec
|
|
77
71
|
- **simdjson**: SIMD-accelerated JSON parsing at GB/s speeds, adaptive On Demand for large docs
|
|
78
72
|
- **RE2 regex**: Linear-time guarantees, immune to ReDoS attacks (2391x faster on pathological input)
|
|
@@ -107,7 +101,7 @@ const v = new Validator({
|
|
|
107
101
|
required: ['name', 'email']
|
|
108
102
|
});
|
|
109
103
|
|
|
110
|
-
// Fast boolean check — JS codegen
|
|
104
|
+
// Fast boolean check — JS codegen (1.7x faster than ajv)
|
|
111
105
|
v.isValidObject({ name: 'Mert', email: 'mert@example.com', age: 26 }); // true
|
|
112
106
|
|
|
113
107
|
// Full validation with error details + defaults applied
|
package/index.js
CHANGED
|
@@ -219,9 +219,9 @@ class Validator {
|
|
|
219
219
|
? null
|
|
220
220
|
: compileToJSCombined(schemaObj, VALID_RESULT);
|
|
221
221
|
// Fallback error-collecting codegen (less optimized, for schemas combined can't handle)
|
|
222
|
-
const jsErrFn =
|
|
223
|
-
?
|
|
224
|
-
:
|
|
222
|
+
const jsErrFn = process.env.ATA_FORCE_NAPI
|
|
223
|
+
? null
|
|
224
|
+
: compileToJSCodegenWithErrors(schemaObj);
|
|
225
225
|
this._jsFn = jsFn;
|
|
226
226
|
|
|
227
227
|
// Data mutators — applied in-place before validation
|
|
@@ -254,20 +254,28 @@ class Validator {
|
|
|
254
254
|
// Valid data: no array allocation, returns VALID_RESULT
|
|
255
255
|
// Invalid data: collects errors in one pass (no double validation)
|
|
256
256
|
// Fallback: jsFn + errFn for schemas combined can't handle
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
257
|
+
// errFn: JS error codegen or NAPI fallback. No try/catch (V8 3.3x deopt).
|
|
258
|
+
// jsErrFn tested at compile time — if it throws, don't use it.
|
|
259
|
+
let safeErrFn = null;
|
|
260
|
+
if (jsErrFn) {
|
|
261
|
+
try { jsErrFn({}, true); safeErrFn = (d) => jsErrFn(d, true); } catch {}
|
|
262
|
+
}
|
|
263
|
+
const errFn = safeErrFn || ((d) => compiled.validate(d));
|
|
264
|
+
|
|
265
|
+
// Hybrid validator: jsFn body with return R / return E(d).
|
|
266
|
+
// V8 optimizes identically to jsFn (83M) — E(d) is dead code on valid path.
|
|
267
|
+
// Invalid: E(d) calls errFn once (34M vs 6M two-pass).
|
|
268
|
+
// Fallback: jsFn + errFn speculative if hybrid unavailable.
|
|
269
|
+
const hybridFn = jsFn._hybridFactory
|
|
270
|
+
? jsFn._hybridFactory(VALID_RESULT, errFn)
|
|
271
|
+
: null;
|
|
272
|
+
this.validate = hybridFn
|
|
273
|
+
? (preprocess ? (data) => { preprocess(data); return hybridFn(data); } : hybridFn)
|
|
264
274
|
: (preprocess
|
|
265
275
|
? (data) => { preprocess(data); return jsFn(data) ? VALID_RESULT : errFn(data); }
|
|
266
276
|
: (data) => jsFn(data) ? VALID_RESULT : errFn(data));
|
|
267
277
|
this.isValidObject = jsFn;
|
|
268
|
-
const jsonValidateFn =
|
|
269
|
-
? (obj) => { try { return jsCombinedFn(obj); } catch { return jsFn(obj) ? VALID_RESULT : errFn(obj); } }
|
|
270
|
-
: (obj) => jsFn(obj) ? VALID_RESULT : errFn(obj);
|
|
278
|
+
const jsonValidateFn = hybridFn || ((obj) => jsFn(obj) ? VALID_RESULT : errFn(obj));
|
|
271
279
|
this.validateJSON = useSimdjsonForLarge
|
|
272
280
|
? (jsonStr) => {
|
|
273
281
|
if (jsonStr.length >= SIMDJSON_THRESHOLD) {
|
package/lib/js-compiler.js
CHANGED
|
@@ -528,12 +528,57 @@ function compileToJSCodegen(schema) {
|
|
|
528
528
|
const body = helperStr + checkStr + '\n return true'
|
|
529
529
|
|
|
530
530
|
try {
|
|
531
|
-
|
|
531
|
+
const boolFn = new Function('d', body)
|
|
532
|
+
|
|
533
|
+
// Build hybrid: same body, return R instead of true, return E(d) instead of false.
|
|
534
|
+
// V8 optimizes this identically to jsFn — E(d) is dead code on valid path.
|
|
535
|
+
// 83M ops/sec vs 26M for combined. Invalid path: 34M vs 6M.
|
|
536
|
+
const hybridBody = replaceTopLevel(helperStr + checkStr + '\n return R')
|
|
537
|
+
try {
|
|
538
|
+
const factory = new Function('R', 'E', `return function(d){${hybridBody}}`)
|
|
539
|
+
boolFn._hybridFactory = factory
|
|
540
|
+
} catch {}
|
|
541
|
+
|
|
542
|
+
return boolFn
|
|
532
543
|
} catch {
|
|
533
544
|
return null
|
|
534
545
|
}
|
|
535
546
|
}
|
|
536
547
|
|
|
548
|
+
// Replace top-level `return false` → `return E(d)` and `return true` → `return R`.
|
|
549
|
+
// Tracks function nesting depth to preserve nested function internals.
|
|
550
|
+
function replaceTopLevel(code) {
|
|
551
|
+
let fnDepth = 0, result = '', i = 0
|
|
552
|
+
while (i < code.length) {
|
|
553
|
+
if (code.startsWith('function', i) && (i === 0 || /[^a-zA-Z_$]/.test(code[i - 1]))) {
|
|
554
|
+
// Found a nested function — skip to opening brace, track all braces inside
|
|
555
|
+
let j = i + 8
|
|
556
|
+
while (j < code.length && code[j] !== '{') j++
|
|
557
|
+
result += code.slice(i, j + 1)
|
|
558
|
+
i = j + 1
|
|
559
|
+
// Track braces inside this function body
|
|
560
|
+
let braceDepth = 1
|
|
561
|
+
while (i < code.length && braceDepth > 0) {
|
|
562
|
+
if (code[i] === '{') braceDepth++
|
|
563
|
+
else if (code[i] === '}') braceDepth--
|
|
564
|
+
if (braceDepth > 0) result += code[i]
|
|
565
|
+
else result += '}' // closing brace of function
|
|
566
|
+
i++
|
|
567
|
+
}
|
|
568
|
+
} else if (code.startsWith('return false', i)) {
|
|
569
|
+
result += 'return E(d)'
|
|
570
|
+
i += 12
|
|
571
|
+
} else if (code.startsWith('return true', i) && (i + 11 >= code.length || !/[a-zA-Z_$]/.test(code[i + 11]))) {
|
|
572
|
+
result += 'return R'
|
|
573
|
+
i += 11
|
|
574
|
+
} else {
|
|
575
|
+
result += code[i]
|
|
576
|
+
i++
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return result
|
|
580
|
+
}
|
|
581
|
+
|
|
537
582
|
// knownType: if parent already verified the type, skip redundant guards.
|
|
538
583
|
// 'object' = we know v is a non-null non-array object
|
|
539
584
|
// 'array' = we know v is an array
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ata-validator",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.7",
|
|
4
4
|
"description": "Ultra-fast JSON Schema validator. Beats ajv on every valid-path benchmark: 1.1x–2.7x faster validate(obj), 151x faster compilation, 5.9x faster parallel batch. Speculative validation with V8-optimized JS codegen, simdjson, multi-core. Standard Schema V1 compatible.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|