ata-validator 0.4.13 → 0.4.14

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/lib/js-compiler.js +23 -11
  2. package/package.json +1 -1
@@ -523,6 +523,11 @@ function compileToJSCodegen(schema) {
523
523
  genCode(schema, 'd', lines, ctx)
524
524
  if (lines.length === 0) return () => true
525
525
 
526
+ // Append deferred checks (additionalProperties) at the end
527
+ if (ctx.deferredChecks) {
528
+ for (const dc of ctx.deferredChecks) lines.push(dc)
529
+ }
530
+
526
531
  const checkStr = lines.join('\n ')
527
532
 
528
533
  // Regex and helpers are passed as closure variables (not re-created per call)
@@ -634,7 +639,7 @@ function genCode(schema, v, lines, ctx, knownType) {
634
639
  case 'string': return `typeof ${v}==='string'`
635
640
  case 'number': return `(typeof ${v}==='number'&&isFinite(${v}))`
636
641
  case 'integer': return `Number.isInteger(${v})`
637
- case 'boolean': return `typeof ${v}==='boolean'`
642
+ case 'boolean': return `(${v}===true||${v}===false)`
638
643
  case 'null': return `${v}===null`
639
644
  default: return 'true'
640
645
  }
@@ -676,13 +681,18 @@ function genCode(schema, v, lines, ctx, knownType) {
676
681
  // Collect required keys so property checks can skip 'in' guard
677
682
  const requiredSet = new Set(schema.required || [])
678
683
 
679
- // required: direct property access (faster than destructuring in V8)
680
- const hoisted = {} // key -> access expression (no local vars)
684
+ // required: skip explicit check if property has a type constraint
685
+ // (type check on undefined returns false anyway: Number.isInteger(undefined) === false)
686
+ const hoisted = {} // key -> access expression
681
687
  if (schema.required && schema.properties && isObj) {
682
688
  const reqChecks = []
683
689
  for (const key of schema.required) {
684
690
  hoisted[key] = `${v}[${JSON.stringify(key)}]`
685
- reqChecks.push(`${v}[${JSON.stringify(key)}]===undefined`)
691
+ const prop = schema.properties[key]
692
+ const hasTypeCheck = prop && (prop.type || prop.enum || prop.const !== undefined)
693
+ if (!hasTypeCheck) {
694
+ reqChecks.push(`${v}[${JSON.stringify(key)}]===undefined`)
695
+ }
686
696
  }
687
697
  if (reqChecks.length > 0) {
688
698
  lines.push(`if(${reqChecks.join('||')})return false`)
@@ -745,18 +755,20 @@ function genCode(schema, v, lines, ctx, knownType) {
745
755
  lines.push(isArr ? `{${inner}}` : `if(Array.isArray(${v})){${inner}}`)
746
756
  }
747
757
 
748
- // additionalProperties
758
+ // additionalProperties -- deferred to end of function for better V8 optimization
759
+ // (type checks run first in hot path, expensive prop count check last)
749
760
  if (schema.additionalProperties === false && schema.properties) {
750
761
  const propCount = Object.keys(schema.properties).length
751
762
  if (!schema.patternProperties) {
752
- // Fast path: known property count, just check length
753
- const inner = `if(Object.getOwnPropertyNames(${v}).length!==${propCount})return false`
754
- lines.push(isObj ? inner : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
763
+ const inner = `var _n=0;for(var _k in ${v})_n++;if(_n!==${propCount})return false`
764
+ if (!ctx.deferredChecks) ctx.deferredChecks = []
765
+ ctx.deferredChecks.push(isObj ? inner : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
755
766
  } else {
756
767
  const allowed = Object.keys(schema.properties).map(k => `'${esc(k)}'`).join(',')
757
768
  const ci = ctx.varCounter++
758
769
  const inner = `const _k${ci}=Object.keys(${v});const _a${ci}=new Set([${allowed}]);for(let _i=0;_i<_k${ci}.length;_i++)if(!_a${ci}.has(_k${ci}[_i]))return false`
759
- lines.push(isObj ? `{${inner}}` : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
770
+ if (!ctx.deferredChecks) ctx.deferredChecks = []
771
+ ctx.deferredChecks.push(isObj ? `{${inner}}` : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
760
772
  }
761
773
  }
762
774
 
@@ -987,7 +999,7 @@ function genCodeE(schema, v, pathExpr, lines, ctx) {
987
999
  case 'string': return `typeof ${v}==='string'`
988
1000
  case 'number': return `(typeof ${v}==='number'&&isFinite(${v}))`
989
1001
  case 'integer': return `Number.isInteger(${v})`
990
- case 'boolean': return `typeof ${v}==='boolean'`
1002
+ case 'boolean': return `(${v}===true||${v}===false)`
991
1003
  case 'null': return `${v}===null`
992
1004
  default: return 'true'
993
1005
  }
@@ -1314,7 +1326,7 @@ function genCodeC(schema, v, pathExpr, lines, ctx) {
1314
1326
  case 'string': return `typeof ${v}==='string'`
1315
1327
  case 'number': return `(typeof ${v}==='number'&&isFinite(${v}))`
1316
1328
  case 'integer': return `Number.isInteger(${v})`
1317
- case 'boolean': return `typeof ${v}==='boolean'`
1329
+ case 'boolean': return `(${v}===true||${v}===false)`
1318
1330
  case 'null': return `${v}===null`
1319
1331
  default: return 'true'
1320
1332
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ata-validator",
3
- "version": "0.4.13",
3
+ "version": "0.4.14",
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",