ata-validator 0.4.0 → 0.4.1

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/include/ata.h CHANGED
@@ -12,10 +12,10 @@ namespace ata {
12
12
 
13
13
  inline constexpr uint32_t VERSION_MAJOR = 0;
14
14
  inline constexpr uint32_t VERSION_MINOR = 4;
15
- inline constexpr uint32_t VERSION_REVISION = 0;
15
+ inline constexpr uint32_t VERSION_REVISION = 1;
16
16
 
17
17
  inline constexpr std::string_view version() noexcept {
18
- return "0.4.0";
18
+ return "0.4.1";
19
19
  }
20
20
 
21
21
  enum class error_code : uint8_t {
package/index.js CHANGED
@@ -1,6 +1,54 @@
1
1
  const native = require("node-gyp-build")(__dirname);
2
2
  const { compileToJS, compileToJSCodegen } = require("./lib/js-compiler");
3
3
 
4
+ // Extract default values from a schema tree. Returns a function that applies
5
+ // defaults to an object in-place (mutates), or null if no defaults exist.
6
+ function buildDefaultsApplier(schema) {
7
+ if (typeof schema !== 'object' || schema === null) return null;
8
+ const actions = [];
9
+ collectDefaults(schema, actions);
10
+ if (actions.length === 0) return null;
11
+ return (data) => {
12
+ for (let i = 0; i < actions.length; i++) actions[i](data);
13
+ };
14
+ }
15
+
16
+ function collectDefaults(schema, actions, path) {
17
+ if (typeof schema !== 'object' || schema === null) return;
18
+ const props = schema.properties;
19
+ if (!props) return;
20
+ for (const [key, prop] of Object.entries(props)) {
21
+ if (prop && typeof prop === 'object' && prop.default !== undefined) {
22
+ const defaultVal = prop.default;
23
+ if (!path) {
24
+ actions.push((data) => {
25
+ if (typeof data === 'object' && data !== null && !(key in data)) {
26
+ data[key] = typeof defaultVal === 'object' && defaultVal !== null
27
+ ? JSON.parse(JSON.stringify(defaultVal)) : defaultVal;
28
+ }
29
+ });
30
+ } else {
31
+ const parentPath = path;
32
+ actions.push((data) => {
33
+ let target = data;
34
+ for (let j = 0; j < parentPath.length; j++) {
35
+ if (typeof target !== 'object' || target === null) return;
36
+ target = target[parentPath[j]];
37
+ }
38
+ if (typeof target === 'object' && target !== null && !(key in target)) {
39
+ target[key] = typeof defaultVal === 'object' && defaultVal !== null
40
+ ? JSON.parse(JSON.stringify(defaultVal)) : defaultVal;
41
+ }
42
+ });
43
+ }
44
+ }
45
+ // Recurse into nested object schemas
46
+ if (prop && typeof prop === 'object' && prop.properties) {
47
+ collectDefaults(prop, actions, (path || []).concat(key));
48
+ }
49
+ }
50
+ }
51
+
4
52
  const SIMDJSON_PADDING = 64;
5
53
  const VALID_RESULT = Object.freeze({ valid: true, errors: Object.freeze([]) });
6
54
 
@@ -42,6 +90,10 @@ class Validator {
42
90
  : (compileToJSCodegen(schemaObj) || compileToJS(schemaObj));
43
91
  this._jsFn = jsFn;
44
92
 
93
+ // Default value applier — applies schema defaults to objects in-place
94
+ const applyDefaults = buildDefaultsApplier(schemaObj);
95
+ this._applyDefaults = applyDefaults;
96
+
45
97
  // Closure-capture: avoid `this` property lookup on every call.
46
98
  // V8 keeps closure vars in registers — no hidden class traversal.
47
99
  const fastSlot = this._fastSlot;
@@ -55,7 +107,9 @@ class Validator {
55
107
  const useSimdjsonForLarge = !hasArrayTraversal;
56
108
 
57
109
  if (jsFn) {
58
- this.validate = (data) => jsFn(data) ? VALID_RESULT : compiled.validate(data);
110
+ this.validate = applyDefaults
111
+ ? (data) => { applyDefaults(data); return jsFn(data) ? VALID_RESULT : compiled.validate(data); }
112
+ : (data) => jsFn(data) ? VALID_RESULT : compiled.validate(data);
59
113
  this.isValidObject = jsFn;
60
114
  this.validateJSON = useSimdjsonForLarge
61
115
  ? (jsonStr) => {
@@ -123,6 +177,7 @@ class Validator {
123
177
 
124
178
  // Fallback methods — only used when JS codegen is unavailable
125
179
  validate(data) {
180
+ if (this._applyDefaults) this._applyDefaults(data);
126
181
  return this._compiled.validate(data);
127
182
  }
128
183
 
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  // Compile a JSON Schema into a pure JS validator function.
4
- // No eval, no new Function, CSP-safe.
4
+ // Closure-based validator no new Function() or eval().
5
5
  // Returns null if the schema is too complex for JS compilation.
6
6
 
7
7
  function compileToJS(schema, defs) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ata-validator",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
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",