ata-validator 0.4.11 → 0.4.12

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.
Files changed (2) hide show
  1. package/index.js +55 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -360,25 +360,65 @@ class Validator {
360
360
  return this._compiled.validate(d);
361
361
  });
362
362
 
363
- const hybridFn = jsFn._hybridFactory
364
- ? jsFn._hybridFactory(VALID_RESULT, errFn)
365
- : null;
366
- this.validate = hybridFn
367
- ? preprocess
368
- ? (data) => {
369
- preprocess(data);
370
- return hybridFn(data);
371
- }
372
- : hybridFn
373
- : preprocess
363
+ // Best path: combined validator (single pass, validates + collects errors)
364
+ // Valid data: returns VALID_RESULT, no allocation
365
+ // Invalid data: collects errors in one pass (no double validation)
366
+ // Fallback: hybridFn or jsFn + errFn for schemas combined can't handle
367
+ // Test combined at compile time -- some schemas produce broken combined code
368
+ // Test combined at compile time -- some schemas (e.g. if/then/else)
369
+ // produce broken combined code that crashes on certain inputs.
370
+ // We probe with diverse data; if any throws, fall back to hybrid.
371
+ let safeCombinedFn = null;
372
+ if (jsCombinedFn) {
373
+ try {
374
+ const probe = {};
375
+ // Populate probe with one key per known property to trigger nested paths
376
+ if (schemaObj && schemaObj.properties) {
377
+ for (const k of Object.keys(schemaObj.properties)) probe[k] = "";
378
+ }
379
+ if (schemaObj && schemaObj.if && schemaObj.if.properties) {
380
+ for (const k of Object.keys(schemaObj.if.properties)) probe[k] = "";
381
+ }
382
+ jsCombinedFn(probe);
383
+ jsCombinedFn({});
384
+ jsCombinedFn(null);
385
+ jsCombinedFn(0);
386
+ safeCombinedFn = jsCombinedFn;
387
+ } catch {}
388
+ }
389
+
390
+ if (safeCombinedFn) {
391
+ this.validate = preprocess
374
392
  ? (data) => {
375
393
  preprocess(data);
376
- return jsFn(data) ? VALID_RESULT : errFn(data);
394
+ return safeCombinedFn(data);
377
395
  }
378
- : (data) => (jsFn(data) ? VALID_RESULT : errFn(data));
396
+ : safeCombinedFn;
397
+ } else {
398
+ const hybridFn = jsFn._hybridFactory
399
+ ? jsFn._hybridFactory(VALID_RESULT, errFn)
400
+ : null;
401
+ this.validate = hybridFn
402
+ ? preprocess
403
+ ? (data) => {
404
+ preprocess(data);
405
+ return hybridFn(data);
406
+ }
407
+ : hybridFn
408
+ : preprocess
409
+ ? (data) => {
410
+ preprocess(data);
411
+ return jsFn(data) ? VALID_RESULT : errFn(data);
412
+ }
413
+ : (data) => (jsFn(data) ? VALID_RESULT : errFn(data));
414
+ }
379
415
  this.isValidObject = jsFn;
380
- const jsonValidateFn =
381
- hybridFn || ((obj) => (jsFn(obj) ? VALID_RESULT : errFn(obj)));
416
+ const hybridFn = jsFn._hybridFactory
417
+ ? jsFn._hybridFactory(VALID_RESULT, errFn)
418
+ : null;
419
+ const jsonValidateFn = safeCombinedFn
420
+ || hybridFn
421
+ || ((obj) => (jsFn(obj) ? VALID_RESULT : errFn(obj)));
382
422
  this.validateJSON = useSimdjsonForLarge
383
423
  ? (jsonStr) => {
384
424
  if (jsonStr.length >= SIMDJSON_THRESHOLD) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ata-validator",
3
- "version": "0.4.11",
3
+ "version": "0.4.12",
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",