ata-validator 1.34.0 → 1.36.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
@@ -240,20 +240,21 @@ is the most common way to get a misleading number out of this library.
240
240
 
241
241
  | | compiled with `ata build` | runtime `new Validator(schema)` |
242
242
  |---|---|---|
243
- | In a bundle, gzipped | **2.2 KB** | 92.3 KB |
244
- | Time to a served request | **3.5 ms** | 11.2 ms |
243
+ | In a bundle, gzipped | **2.2 KB** | 93.2 KB |
244
+ | Time to a served request | **3.5 ms** | 7.7 ms |
245
245
  | Schema known when | build time | any time |
246
246
 
247
- The bundle row is the ten-field user schema in `tests/fixtures/error-dx/user.schema.json`,
248
- every export of the compiled module against `new Validator(schema)`, built with
249
- `bun build --minify --target=browser` on ata 1.33.1. The startup row is a Hono route on
250
- Bun 1.4, best of seven, from `benchmark/bundle`, against 3.5 ms for the same app doing no
251
- validation at all, so the compiled path costs nothing measurable to start. The runtime
252
- figure is what it is because a schema that arrives at run time can use any keyword, so
253
- the whole engine has to be there. The compiled module imports nothing and contains only
254
- the checks your schema asks for.
255
-
256
- **On a server, use whichever fits your schemas.** 92 KB of JavaScript on a Node or Bun
247
+ The bundle row is the ten-field user schema in
248
+ `tests/fixtures/error-dx/user.schema.json`, every export of the compiled module against
249
+ `new Validator(schema)`, built with `bun build --minify --target=browser` on ata 1.35.0.
250
+ The startup row is a Hono route on Bun 1.4, best of seven, median of three rounds, from
251
+ `benchmark/bundle`, against 3.5 ms for the same app doing no validation at all, so the
252
+ compiled path costs nothing measurable to start. The runtime figure is what it is because
253
+ a schema that arrives at run time can use any keyword, so the whole engine has to be
254
+ there. The compiled module imports nothing and contains only the checks your schema asks
255
+ for.
256
+
257
+ **On a server, use whichever fits your schemas.** 93 KB of JavaScript on a Node or Bun
257
258
  process is not a cost anyone notices, and the runtime API is the simpler thing to reach
258
259
  for. Speed is the same either way once warm.
259
260
 
@@ -485,7 +486,7 @@ const v = new Validator(schema, {
485
486
 
486
487
  ### Build-time compile (`ata compile`)
487
488
 
488
- 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. For the ten-field user schema in `tests/fixtures/error-dx/user.schema.json` the module is 2.6 KB gzipped, full error detail included, against 91 KB for the runtime bundled for the browser.
489
+ 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. For the ten-field user schema in `tests/fixtures/error-dx/user.schema.json` the module is 2.2 KB gzipped, full error detail included, against 93.2 KB for the runtime bundled for the browser.
489
490
 
490
491
  ```bash
491
492
  npx ata compile schemas/user.json -o src/generated/user.validator.mjs
@@ -527,11 +528,11 @@ npx ata build 'schemas/*.json' --out-dir build/validators --check
527
528
  Run with `--watch` during development for incremental rebuilds.
528
529
 
529
530
  Bundle sizes for the 10-field user schema in `tests/fixtures/error-dx/user.schema.json`,
530
- minified and gzipped, measured with `bun build --minify --target=browser` on ata 1.33.1:
531
+ minified and gzipped, measured with `bun build --minify --target=browser` on ata 1.35.0:
531
532
 
532
533
  | What the app imports | Size | Notes |
533
534
  |---|---|---|
534
- | `Validator` from `ata-validator` | 92.3 KB | The compiler ships with it, because a runtime schema can use any keyword |
535
+ | `Validator` from `ata-validator` | 93.2 KB | The compiler ships with it, because a runtime schema can use any keyword |
535
536
  | `isValid` from the compiled module | **1.3 KB** | Nothing else is reachable, so the error collector is dropped |
536
537
  | `validate` from the compiled module | **2.0 KB** | Adds the detailed error collector |
537
538
 
package/build.d.ts CHANGED
@@ -155,3 +155,26 @@ export function schemaHash(schema: unknown): string;
155
155
  * schema, plus `parse` when {@link ToStandaloneModuleOptions.parse} is set
156
156
  * and `validateJSON` when {@link ToStandaloneModuleOptions.positions} is. */
157
157
  export function toStandaloneModule(schema: unknown, options?: ToStandaloneModuleOptions): string | null;
158
+
159
+ /**
160
+ * Whether `new Validator(schema)` with default options can be replaced by
161
+ * `fromCompiled()` from `ata-validator/compiled`. False for a schema with
162
+ * custom `errorMessage`s. A caller also needs {@link compiledModuleFor} to
163
+ * return a module.
164
+ */
165
+ export function compiledEligible(schema: unknown): boolean;
166
+
167
+ /**
168
+ * The schema a default `Validator` reads after normalization. Pass it to
169
+ * `fromCompiled()`, so defaults, error order and diagnostics follow the same
170
+ * document the runtime does.
171
+ */
172
+ export function compiledSchemaFor(schema: unknown): object;
173
+
174
+ /**
175
+ * The module that replaces `new Validator(schema)`, or null where the
176
+ * replacement would not answer as the runtime does: a schema
177
+ * {@link compiledEligible} declines, one the emitter cannot compile, and one
178
+ * whose detailed errors the generator cannot produce.
179
+ */
180
+ export function compiledModuleFor(schema: unknown, opts?: { format?: 'esm' | 'cjs' }): string | null;
package/build.mjs CHANGED
@@ -8,4 +8,8 @@ export const watch = mod.watch;
8
8
  export const bundleStandalone = mod.bundleStandalone;
9
9
  export const bundleCompact = mod.bundleCompact;
10
10
  export const toStandaloneModule = mod.toStandaloneModule;
11
+ export const schemaHash = mod.schemaHash;
12
+ export const compiledEligible = mod.compiledEligible;
13
+ export const compiledSchemaFor = mod.compiledSchemaFor;
14
+ export const compiledModuleFor = mod.compiledModuleFor;
11
15
  export default mod;
package/compiled.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ // ata-validator/compiled: the wrapper a bundler plugin puts in place of
2
+ // `new Validator(schema)` for a schema known at build time.
3
+ import type { ValidationResult } from './index.js';
4
+
5
+ export interface CompiledModule {
6
+ validate(data: unknown): { valid: boolean; errors: unknown[] };
7
+ isValid(data: unknown): boolean;
8
+ }
9
+
10
+ export interface CompiledValidator<T = unknown> {
11
+ validate(data: unknown): ValidationResult<T>;
12
+ isValidObject(data: unknown): data is T;
13
+ validateJSON(json: string): { valid: boolean; errors: unknown[] };
14
+ isValidJSON(json: string): boolean;
15
+ }
16
+
17
+ export function fromCompiled<T = unknown>(mod: CompiledModule, schema: object): CompiledValidator<T>;
package/compiled.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ // ata-validator/compiled: the wrapper a bundler plugin puts in place of
4
+ // `new Validator(schema)` when the schema is known at build time. It takes the
5
+ // module `compiledModuleFor(schema)` from ata-validator/build wrote, and answers
6
+ // as a default Validator does, without the runtime compiler in the bundle.
7
+ module.exports = require('./lib/compiled.js');
package/compiled.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import mod from './compiled.js';
2
+ export const { fromCompiled } = mod;
3
+ export default mod;
package/index.browser.mjs CHANGED
@@ -1,4 +1,5 @@
1
- // Browser ESM entry — same code, native addon stubbed out by bundler via "browser" field.
1
+ // Browser ESM entry: the same code, with the native addon stubbed out by the
2
+ // bundler through the package.json "browser" field.
2
3
  import mod from './index.js';
3
- export const { Validator, validate, validateAsync, parseAsync, version, createPaddedBuffer, SIMDJSON_PADDING, renderPretty, renderCompact, renderJSON, toTypeScript } = mod;
4
+ export const { Validator, compile, validate, validateAsync, parseAsync, version, createPaddedBuffer, SIMDJSON_PADDING, parseJSON, toTypeScript, defineSchema, renderPretty, renderCompact, toOutput, toRetryMessage, describeSchema, renderJSON } = mod;
4
5
  export default mod;
package/index.d.ts CHANGED
@@ -407,6 +407,12 @@ export interface ValidatorOptions {
407
407
  * `'log'` warns through `logger` (or the console) and continues.
408
408
  */
409
409
  strictSchema?: boolean | 'log';
410
+ /**
411
+ * The URI the schema was retrieved from. Relative references resolve against
412
+ * it when the schema declares no `$id` of its own, including a draft-07 root
413
+ * whose `$id` sits beside a `$ref` and is therefore ignored.
414
+ */
415
+ baseURI?: string;
410
416
  /** Receives `strictSchema: 'log'` warnings; `false` silences them. */
411
417
  logger?: { warn(...args: unknown[]): void } | false;
412
418
  /**