ata-validator 0.11.0 → 0.12.0

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 CHANGED
@@ -1,8 +1,12 @@
1
+ <p align="center">
2
+ <img src="./assets/ata-validator.svg" alt="ata-validator" width="640" />
3
+ </p>
4
+
1
5
  # ata-validator
2
6
 
3
7
  Ultra-fast JSON Schema validator powered by [simdjson](https://github.com/simdjson/simdjson). Multi-core parallel validation, RE2 regex, codegen bytecode engine. Standard Schema V1 compatible.
4
8
 
5
- **[ata-validator.com](https://ata-validator.com)** | **[API Docs](docs/API.md)** | **[Contributing](CONTRIBUTING.md)**
9
+ **[ata-validator.com](https://ata-validator.com)** | **[API Docs](docs/API.md)** | **[Migrate from ajv](docs/migration-from-ajv.md)** | **[Framework integrations](docs/integrations/)** | **[Contributing](CONTRIBUTING.md)**
6
10
 
7
11
  ## Performance
8
12
 
@@ -54,8 +58,8 @@ Three-tier hybrid codegen: static schemas compile to zero-overhead key checks, d
54
58
  |---|---|---|---|---|---|
55
59
  | **validate (valid)** | **9ns** | 38ns | 50ns | 334ns | 326ns |
56
60
  | **validate (invalid)** | **37ns** | 103ns | 4ns | 11.8μs | 842ns |
57
- | **compilation** | **453ns** | 1.24ms | 52μs | | |
58
- | **first validation** | **2.1μs** | 1.11ms | 54μs | | |
61
+ | **compilation** | **453ns** | 1.24ms | 52μs | n/a | n/a |
62
+ | **first validation** | **2.1μs** | 1.11ms | 54μs | n/a | n/a |
59
63
 
60
64
  > Different categories: ata/ajv/typebox are JSON Schema validators, zod/valibot are schema-builder DSLs. [Benchmark code](benchmark/bench_all_mitata.mjs)
61
65
 
@@ -219,7 +223,7 @@ const v = new Validator(schema, {
219
223
 
220
224
  ### Build-time compile (`ata compile`)
221
225
 
222
- The `ata` CLI turns a JSON Schema file into a self-contained JavaScript module. No runtime dependency on `ata-validator`, so only the generated validator ships to the browser typical output is ~1 KB gzipped compared to ~27 KB for the full runtime.
226
+ The `ata` CLI turns a JSON Schema file into a self-contained JavaScript module. No runtime dependency on `ata-validator`, so only the generated validator ships to the browser. Typical output is ~1 KB gzipped compared to ~27 KB for the full runtime.
223
227
 
224
228
  ```bash
225
229
  npx ata compile schemas/user.json -o src/generated/user.validator.mjs
@@ -314,6 +318,24 @@ auto result = ata::validate(schema, R"({"name": "Mert"})");
314
318
  // result.valid == true
315
319
  ```
316
320
 
321
+ ## Framework integrations
322
+
323
+ Copy-paste recipes for the common frameworks. Most need 10-20 lines of glue. See [docs/integrations](docs/integrations/) for the full set.
324
+
325
+ | Framework | Pattern | Recipe |
326
+ |---|---|---|
327
+ | Fastify | dedicated plugin | [`fastify-ata`](https://github.com/ata-core/fastify-ata) |
328
+ | Vite (build-time compile) | dedicated plugin | [`ata-vite`](https://github.com/ata-core/ata-vite) |
329
+ | Hono | async middleware | [docs/integrations/hono.md](docs/integrations/hono.md) |
330
+ | Elysia | direct handler check | [docs/integrations/elysia.md](docs/integrations/elysia.md) |
331
+ | tRPC | Standard Schema V1 input | [docs/integrations/trpc.md](docs/integrations/trpc.md) |
332
+ | TanStack Form | Standard Schema V1 validator | [docs/integrations/tanstack-form.md](docs/integrations/tanstack-form.md) |
333
+ | Express | sync middleware | [docs/integrations/express.md](docs/integrations/express.md) |
334
+ | Koa | async ctx middleware | [docs/integrations/koa.md](docs/integrations/koa.md) |
335
+ | NestJS | validation pipe | [docs/integrations/nestjs.md](docs/integrations/nestjs.md) |
336
+ | SvelteKit | form action, API route | [docs/integrations/sveltekit.md](docs/integrations/sveltekit.md) |
337
+ | Astro | API route, server action | [docs/integrations/astro.md](docs/integrations/astro.md) |
338
+
317
339
  ## Supported Keywords
318
340
 
319
341
  | Category | Keywords |
package/index.d.ts CHANGED
@@ -123,3 +123,11 @@ export function createPaddedBuffer(jsonStr: string): { buffer: Buffer; length: n
123
123
 
124
124
  /** Required padding size for simdjson buffers. */
125
125
  export const SIMDJSON_PADDING: number;
126
+
127
+ /**
128
+ * Generate a TypeScript declaration (.d.ts source) for the given JSON Schema.
129
+ * Returns a string containing the generated type plus `isValid` / `validate`
130
+ * signatures. Used internally by the `ata compile` CLI and exposed for
131
+ * build-time integrations (Vite plugin, custom build steps).
132
+ */
133
+ export function toTypeScript(schema: object, options?: { name?: string }): string;
package/index.js CHANGED
@@ -1216,6 +1216,8 @@ function compile(schema, opts) {
1216
1216
  return fn;
1217
1217
  }
1218
1218
 
1219
+ const { toTypeScript } = require("./lib/ts-gen");
1220
+
1219
1221
  module.exports = {
1220
1222
  Validator,
1221
1223
  compile,
@@ -1224,4 +1226,5 @@ module.exports = {
1224
1226
  createPaddedBuffer,
1225
1227
  SIMDJSON_PADDING,
1226
1228
  parseJSON,
1229
+ toTypeScript,
1227
1230
  };
@@ -675,8 +675,14 @@ function codegenSafe(schema, schemaMap) {
675
675
  if (siblings.length > 0 && schema.unevaluatedProperties === undefined && schema.unevaluatedItems === undefined) return false
676
676
  }
677
677
 
678
- // additionalProperties as schema — bail entirely, too many edge cases with allOf interaction
679
- if (typeof schema.additionalProperties === 'object') return false
678
+ // additionalProperties as schema — supported when no composition (allOf/oneOf/anyOf)
679
+ // is in play, and no patternProperties (unified loop does not emit the
680
+ // per-key sub-schema walk yet). Closure path handles those cases.
681
+ if (typeof schema.additionalProperties === 'object' && schema.additionalProperties !== null) {
682
+ if (schema.allOf || schema.oneOf || schema.anyOf) return false
683
+ if (schema.patternProperties) return false
684
+ if (!codegenSafe(schema.additionalProperties, schemaMap)) return false
685
+ }
680
686
  if (schema.additionalProperties === false && !schema.properties) return false
681
687
 
682
688
  // propertyNames: false — codegen doesn't handle this
@@ -729,6 +735,37 @@ function codegenSafe(schema, schemaMap) {
729
735
  return true
730
736
  }
731
737
 
738
+ // Returns true if the schema (or any nested sub-schema reachable through the
739
+ // usual keywords) has additionalProperties as a schema value. Used by the
740
+ // combined-codegen bail since that path does not emit the per-key loop yet.
741
+ function hasAdditionalPropertiesSchema(schema) {
742
+ if (typeof schema !== 'object' || schema === null) return false
743
+ if (typeof schema.additionalProperties === 'object' && schema.additionalProperties !== null) return true
744
+ const objSubs = ['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas']
745
+ for (const key of objSubs) {
746
+ if (schema[key] && typeof schema[key] === 'object') {
747
+ for (const v of Object.values(schema[key])) {
748
+ if (hasAdditionalPropertiesSchema(v)) return true
749
+ }
750
+ }
751
+ }
752
+ const arrSubs = ['allOf', 'anyOf', 'oneOf', 'prefixItems']
753
+ for (const key of arrSubs) {
754
+ if (Array.isArray(schema[key])) {
755
+ for (const s of schema[key]) {
756
+ if (hasAdditionalPropertiesSchema(s)) return true
757
+ }
758
+ }
759
+ }
760
+ const singleSubs = ['items', 'contains', 'not', 'if', 'then', 'else', 'propertyNames']
761
+ for (const key of singleSubs) {
762
+ if (typeof schema[key] === 'object' && schema[key] !== null) {
763
+ if (hasAdditionalPropertiesSchema(schema[key])) return true
764
+ }
765
+ }
766
+ return false
767
+ }
768
+
732
769
  // --- Codegen mode: generates a single Function (NOT CSP-safe) ---
733
770
  // This matches ajv's approach: one monolithic function, V8 JIT fully inlines it
734
771
  function compileToJSCodegen(schema, schemaMap) {
@@ -1274,6 +1311,25 @@ function genCode(schema, v, lines, ctx, knownType) {
1274
1311
  _deferOrInline(ctx, lines, v, isObj ? inner : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${inner}}`)
1275
1312
  }
1276
1313
 
1314
+ // additionalProperties as a schema: validate every non-declared property
1315
+ // against that sub-schema. Skip if patternProperties is present (handled by
1316
+ // the unified loop). Composition cases are filtered out by codegenSafe.
1317
+ if (typeof schema.additionalProperties === 'object' && schema.additionalProperties !== null && !schema.patternProperties) {
1318
+ const declared = schema.properties ? Object.keys(schema.properties) : []
1319
+ const skipCheck = declared.length === 0
1320
+ ? null
1321
+ : declared.map(k => `_k===${JSON.stringify(k)}`).join('||')
1322
+ const subLines = []
1323
+ genCode(schema.additionalProperties, '_av', subLines, ctx)
1324
+ if (subLines.length > 0) {
1325
+ const body = subLines.join(';')
1326
+ const loop = skipCheck
1327
+ ? `for(var _k in ${v}){if(${skipCheck})continue;const _av=${v}[_k];${body}}`
1328
+ : `for(var _k in ${v}){const _av=${v}[_k];${body}}`
1329
+ _deferOrInline(ctx, lines, v, isObj ? loop : `if(typeof ${v}==='object'&&${v}!==null&&!Array.isArray(${v})){${loop}}`)
1330
+ }
1331
+ }
1332
+
1277
1333
  // dependentRequired
1278
1334
  if (schema.dependentRequired) {
1279
1335
  for (const [key, deps] of Object.entries(schema.dependentRequired)) {
@@ -2251,6 +2307,9 @@ function compileToJSCodegenWithErrors(schema, schemaMap) {
2251
2307
  if (s.includes('unevaluatedProperties') || s.includes('unevaluatedItems')) return null
2252
2308
  // Bail on self-referencing schemas — error codegen doesn't support recursion
2253
2309
  if (s.includes('"$ref":"#"')) return null
2310
+ // Bail on additionalProperties as a schema — genCodeE does not emit the
2311
+ // per-key sub-schema loop. Let the closure path handle detailed errors.
2312
+ if (hasAdditionalPropertiesSchema(schema)) return null
2254
2313
  }
2255
2314
  if (typeof schema === 'boolean') {
2256
2315
  return schema
@@ -2752,6 +2811,9 @@ function compileToJSCombined(schema, VALID_RESULT, schemaMap) {
2752
2811
  if (s.includes('unevaluatedProperties') || s.includes('unevaluatedItems')) return null
2753
2812
  // Bail on self-referencing schemas — combined codegen doesn't support recursion
2754
2813
  if (s.includes('"$ref":"#"')) return null
2814
+ // Bail on additionalProperties as a schema — combined path doesn't emit the
2815
+ // per-key sub-schema loop yet. The jsFn + errFn fallback handles it correctly.
2816
+ if (hasAdditionalPropertiesSchema(schema)) return null
2755
2817
  }
2756
2818
  if (typeof schema === 'boolean') {
2757
2819
  return schema
package/lib/ts-gen.js CHANGED
@@ -56,6 +56,28 @@ function renderValueType(schema, defs, depth = 0) {
56
56
 
57
57
  if (t === 'array') {
58
58
  const items = schema.items;
59
+ const prefix = Array.isArray(schema.prefixItems) ? schema.prefixItems : null;
60
+
61
+ if (prefix) {
62
+ const prefixTypes = prefix.map((s) => renderValueType(s, defs, depth + 1));
63
+ const minItems = typeof schema.minItems === 'number' ? schema.minItems : 0;
64
+ // Elements before minItems are required; the remainder are optional
65
+ // because JSON Schema does not require prefixItems to be present.
66
+ const elements = prefixTypes.map((t, i) => (i < minItems ? t : `${t}?`));
67
+ if (items === false) {
68
+ return `[${elements.join(', ')}]`;
69
+ }
70
+ if (items === undefined || items === true) {
71
+ return `[${elements.join(', ')}, ...unknown[]]`;
72
+ }
73
+ if (typeof items === 'object' && items !== null) {
74
+ const rest = renderValueType(items, defs, depth + 1);
75
+ const restType = rest.includes(' | ') ? `(${rest})` : rest;
76
+ return `[${elements.join(', ')}, ...${restType}[]]`;
77
+ }
78
+ }
79
+
80
+ if (items === false) return 'never[]';
59
81
  if (items === undefined || items === true) return 'unknown[]';
60
82
  const inner = renderValueType(items, defs, depth + 1);
61
83
  return inner.includes(' | ') ? `Array<${inner}>` : `${inner}[]`;
@@ -84,15 +106,30 @@ function renderObject(schema, defs, depth) {
84
106
  const t = renderValueType(props[k], defs, depth + 1);
85
107
  const opt = required.has(k) ? '' : '?';
86
108
  const safeKey = /^[A-Za-z_$][\w$]*$/.test(k) ? k : JSON.stringify(k);
87
- const desc = typeof props[k] === 'object' && props[k] && typeof props[k].description === 'string'
88
- ? ` /** ${props[k].description.replace(/\*\//g, '* /')} */\n`
89
- : '';
90
- return `${desc} ${safeKey}${opt}: ${t};`;
109
+ const doc = renderJsDoc(props[k], ' ');
110
+ return `${doc} ${safeKey}${opt}: ${t};`;
91
111
  });
92
112
  // extra keys when additionalProperties is present as a schema or true
93
113
  const extra = schema.additionalProperties;
94
114
  if (extra && typeof extra === 'object') {
95
- lines.push(` [key: string]: ${renderValueType(extra, defs, depth + 1)};`);
115
+ // TypeScript requires the index signature to be a supertype of every
116
+ // named property's emitted type. Widen to a union covering each property
117
+ // type, plus undefined when any property is optional.
118
+ const widen = new Set();
119
+ widen.add(renderValueType(extra, defs, depth + 1));
120
+ let hasOptional = false;
121
+ for (const k of keys) {
122
+ widen.add(renderValueType(props[k], defs, depth + 1));
123
+ if (!required.has(k)) hasOptional = true;
124
+ }
125
+ if (hasOptional) widen.add('undefined');
126
+ const indexType = widen.has('unknown') ? 'unknown' : Array.from(widen).join(' | ');
127
+ lines.push(` [key: string]: ${indexType};`);
128
+ } else if (extra !== false) {
129
+ // JSON Schema accepts extra keys by default. Emit a permissive index
130
+ // signature so tsc does not reject excess properties that the runtime
131
+ // would consider valid.
132
+ lines.push(` [key: string]: unknown;`);
96
133
  }
97
134
  return `{\n${lines.join('\n')}\n}`;
98
135
  }
@@ -106,10 +143,54 @@ function renderLiteral(v) {
106
143
 
107
144
  function toTypeName(name) {
108
145
  const cleaned = String(name).replace(/[^A-Za-z0-9_]/g, '_');
146
+ if (cleaned === '') return '_Anon';
109
147
  if (/^[0-9]/.test(cleaned)) return `_${cleaned}`;
110
148
  return cleaned.charAt(0).toUpperCase() + cleaned.slice(1);
111
149
  }
112
150
 
151
+ // Build a JSDoc block that captures the description plus any runtime-only
152
+ // constraints the TypeScript type cannot express (minLength, format, range,
153
+ // etc.). Editors and TypeDoc surface these on hover, so authors can see what
154
+ // the schema requires even though tsc does not enforce it.
155
+ function renderJsDoc(schema, indent) {
156
+ if (!schema || typeof schema !== 'object') return '';
157
+
158
+ let description = '';
159
+ if (typeof schema.description === 'string' && schema.description.length > 0) {
160
+ description = schema.description.replace(/\*\//g, '* /');
161
+ }
162
+
163
+ const tags = [];
164
+ const numKeys = ['minLength', 'maxLength', 'minItems', 'maxItems', 'minProperties', 'maxProperties',
165
+ 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum', 'multipleOf'];
166
+ for (const k of numKeys) {
167
+ if (typeof schema[k] === 'number') tags.push(`@${k} ${schema[k]}`);
168
+ }
169
+ if (typeof schema.pattern === 'string') tags.push(`@pattern ${schema.pattern}`);
170
+ if (typeof schema.format === 'string') tags.push(`@format ${schema.format}`);
171
+ if (schema.uniqueItems === true) tags.push('@uniqueItems');
172
+ if (schema.deprecated === true) tags.push('@deprecated');
173
+ if (schema.default !== undefined) {
174
+ try { tags.push(`@default ${JSON.stringify(schema.default)}`); } catch (_) {}
175
+ }
176
+ if (Array.isArray(schema.examples) && schema.examples.length > 0) {
177
+ try { tags.push(`@example ${JSON.stringify(schema.examples[0])}`); } catch (_) {}
178
+ }
179
+
180
+ if (description === '' && tags.length === 0) return '';
181
+
182
+ if (description !== '' && tags.length === 0) {
183
+ return `${indent}/** ${description} */\n`;
184
+ }
185
+
186
+ const lines = [`${indent}/**`];
187
+ if (description !== '') lines.push(`${indent} * ${description}`);
188
+ if (description !== '' && tags.length > 0) lines.push(`${indent} *`);
189
+ for (const t of tags) lines.push(`${indent} * ${t}`);
190
+ lines.push(`${indent} */`);
191
+ return lines.join('\n') + '\n';
192
+ }
193
+
113
194
  // Public: given a schema and optional type name, return a .d.ts source.
114
195
  function toTypeScript(schema, opts) {
115
196
  const options = opts || {};
@@ -125,10 +206,14 @@ function toTypeScript(schema, opts) {
125
206
  }
126
207
 
127
208
  const rootType = renderValueType(schema, defs, 0);
128
- const isObjectShape = rootType.startsWith('{') || rootType.startsWith('Record<');
129
- const rootDecl = isObjectShape && !rootType.includes(' | ')
130
- ? `export interface ${rootName} ${rootType}`
131
- : `export type ${rootName} = ${rootType};`;
209
+ const rootDoc = renderJsDoc(schema, '');
210
+ // Use `interface` only for a pure object literal; otherwise fall back to
211
+ // `type`. Catches cases like `{...}[]` (array of object) and `Record<...>`
212
+ // which are valid TS but cannot be expressed as an interface body.
213
+ const isPureObjectLiteral = rootType.startsWith('{') && rootType.endsWith('}') && !rootType.includes(' | ');
214
+ const rootDecl = isPureObjectLiteral
215
+ ? `${rootDoc}export interface ${rootName} ${rootType}`
216
+ : `${rootDoc}export type ${rootName} = ${rootType};`;
132
217
 
133
218
  return `// Auto-generated by ata-validator — do not edit.
134
219
  ${defLines.length ? defLines.join('\n\n') + '\n\n' : ''}${rootDecl}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ata-validator",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Ultra-fast JSON Schema validator. 4.7x faster validation, 1,800x faster compilation. Works without native addon. Cross-schema $ref, Draft 2020-12 + Draft 7, V8-optimized JS codegen, simdjson, RE2, multi-core. Standard Schema V1 compatible.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -40,6 +40,9 @@
40
40
  "test:compat": "node tests/test_compat.js",
41
41
  "test:standard-schema": "node tests/test_standard_schema.js",
42
42
  "test:browser": "node tests/test_browser.js",
43
+ "test:ts": "node tests/test_ts_gen.js",
44
+ "test:ts-corpus": "node tests/test_ts_corpus.js",
45
+ "test:ts-differential": "node tests/test_ts_differential.js",
43
46
  "bench": "node benchmark/bench_large.js",
44
47
  "fuzz": "node tests/fuzz_differential.js",
45
48
  "fuzz:long": "FUZZ_ITERATIONS=100000 node tests/fuzz_differential.js",
@@ -60,7 +63,7 @@
60
63
  "standard-schema",
61
64
  "fastify"
62
65
  ],
63
- "author": "Mert Can Altin <mertcanaltin01@gmail.com>",
66
+ "author": "Mert Can Altin <mertgold60@gmail.com>",
64
67
  "license": "MIT",
65
68
  "repository": {
66
69
  "type": "git",
@@ -103,6 +106,7 @@
103
106
  "cmake-js": "^8.0.0",
104
107
  "mitata": "^1.0.34",
105
108
  "typebox": "^1.1.7",
109
+ "typescript": "^6.0.3",
106
110
  "valibot": "^1.3.1",
107
111
  "zod": "^4.3.6"
108
112
  },