ata-validator 0.13.4 → 0.14.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/CHANGELOG.md +15 -0
- package/README.md +33 -0
- package/index.d.ts +16 -19
- package/package.json +7 -3
- package/prebuilds/ata-darwin-arm64/node-napi-v10.node +0 -0
- package/prebuilds/ata-linux-arm64/node-napi-v10.node +0 -0
- package/prebuilds/ata-linux-arm64-musl/node-napi-v10.node +0 -0
- package/prebuilds/ata-linux-x64/node-napi-v10.node +0 -0
- package/prebuilds/ata-linux-x64-musl/node-napi-v10.node +0 -0
- package/prebuilds/ata-win32-x64/node-napi-v10.node +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to ata-validator are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/), and this project adheres to semantic versioning.
|
|
4
4
|
|
|
5
|
+
## 0.14.0 - 2026-05-16
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Generic `Validator<T>` with type predicate `isValidObject(data): data is T`. Pairs naturally with TypeBox, Zod-from-JSON-Schema, Valibot, or hand-written types over JSON Schema literals.
|
|
10
|
+
- `ValidationResult<T>` and `ValidateAndParseResult<T>` are discriminated unions. On the `valid: true` branch the parsed data is typed; on `valid: false` the `errors` array carries the diagnostic information.
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Type-level only: accessing `result.data` (or `result.value` on `validateAndParse`) without first checking `result.valid` is now a TypeScript compile error. Runtime behavior is unchanged. The previous shape returned `undefined` in that position, so this surfaces an existing latent bug at compile time.
|
|
15
|
+
|
|
16
|
+
### Notes
|
|
17
|
+
|
|
18
|
+
- Pure `.d.ts` change. No JS, C++, AOT, or CLI behavior is affected. Bundle size unchanged. Runtime performance unchanged.
|
|
19
|
+
|
|
5
20
|
## 0.13.4 — 2026-05-14
|
|
6
21
|
|
|
7
22
|
### Fixed
|
package/README.md
CHANGED
|
@@ -90,6 +90,39 @@ v.isValidParallel(ndjson); // bool[]
|
|
|
90
90
|
v.countValid(ndjson); // number
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
+
### Type-safe schemas
|
|
94
|
+
|
|
95
|
+
`Validator` is generic. Pair it with any schema authoring tool, or a hand-written type, to get TypeScript narrowing in your handler code.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { Type, type Static } from '@sinclair/typebox'
|
|
99
|
+
import { Validator } from 'ata-validator'
|
|
100
|
+
|
|
101
|
+
const UserSchema = Type.Object({
|
|
102
|
+
id: Type.Integer({ minimum: 1 }),
|
|
103
|
+
name: Type.String({ minLength: 1 }),
|
|
104
|
+
email: Type.String({ format: 'email' }),
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
type User = Static<typeof UserSchema>
|
|
108
|
+
|
|
109
|
+
const v = new Validator<User>(UserSchema)
|
|
110
|
+
|
|
111
|
+
if (v.isValidObject(data)) {
|
|
112
|
+
// data is narrowed to User, no cast needed
|
|
113
|
+
console.log(data.name)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const result = v.validate(data)
|
|
117
|
+
if (result.valid) {
|
|
118
|
+
// result.data is User
|
|
119
|
+
} else {
|
|
120
|
+
// result.errors: ValidationError[]
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The same pattern works with Zod-from-JSON-Schema, Valibot, or a hand-written `type User = {...}` alongside a JSON Schema literal. `Validator<T>` makes no library-specific assumption.
|
|
125
|
+
|
|
93
126
|
### Cross-Schema `$ref`
|
|
94
127
|
|
|
95
128
|
```javascript
|
package/index.d.ts
CHANGED
|
@@ -15,16 +15,13 @@ export interface ValidationError {
|
|
|
15
15
|
/** A user-supplied format checker. Receives the candidate value, returns true if valid. */
|
|
16
16
|
export type FormatChecker = (value: string) => boolean;
|
|
17
17
|
|
|
18
|
-
export
|
|
19
|
-
valid:
|
|
20
|
-
errors: ValidationError[];
|
|
21
|
-
}
|
|
18
|
+
export type ValidationResult<T = unknown> =
|
|
19
|
+
| { valid: true; data: T; errors: ValidationError[] }
|
|
20
|
+
| { valid: false; data?: never; errors: ValidationError[] };
|
|
22
21
|
|
|
23
|
-
export
|
|
24
|
-
valid:
|
|
25
|
-
value: unknown;
|
|
26
|
-
errors: ValidationError[];
|
|
27
|
-
}
|
|
22
|
+
export type ValidateAndParseResult<T = unknown> =
|
|
23
|
+
| { valid: true; value: T; errors: ValidationError[] }
|
|
24
|
+
| { valid: false; value: unknown; errors: ValidationError[] };
|
|
28
25
|
|
|
29
26
|
export interface ValidatorOptions {
|
|
30
27
|
coerceTypes?: boolean;
|
|
@@ -74,26 +71,26 @@ export interface StandaloneModule {
|
|
|
74
71
|
errFn: ((data: unknown, allErrors?: boolean) => ValidationResult) | null;
|
|
75
72
|
}
|
|
76
73
|
|
|
77
|
-
export class Validator {
|
|
74
|
+
export class Validator<T = unknown> {
|
|
78
75
|
constructor(schema: object | string, options?: ValidatorOptions);
|
|
79
76
|
|
|
80
77
|
/** Add a schema to the registry for cross-schema $ref resolution */
|
|
81
78
|
addSchema(schema: object): void;
|
|
82
79
|
|
|
83
80
|
/** Validate data, returns result with errors. Applies defaults, coerceTypes, removeAdditional. */
|
|
84
|
-
validate(data: unknown): ValidationResult
|
|
81
|
+
validate(data: unknown): ValidationResult<T>;
|
|
85
82
|
|
|
86
83
|
/** Fast boolean check via JS codegen or tier 0 interpreter. No error collection. */
|
|
87
|
-
isValidObject(data: unknown):
|
|
84
|
+
isValidObject(data: unknown): data is T;
|
|
88
85
|
|
|
89
86
|
/** Validate a JSON string. Uses simdjson fast path for large documents. */
|
|
90
|
-
validateJSON(jsonString: string): ValidationResult
|
|
87
|
+
validateJSON(jsonString: string): ValidationResult<T>;
|
|
91
88
|
|
|
92
89
|
/** Fast boolean check for a JSON string */
|
|
93
90
|
isValidJSON(jsonString: string): boolean;
|
|
94
91
|
|
|
95
92
|
/** Parse JSON with simdjson + validate against schema. Returns parsed value and validation result. Requires native addon. */
|
|
96
|
-
validateAndParse(jsonString: string | Buffer): ValidateAndParseResult
|
|
93
|
+
validateAndParse(jsonString: string | Buffer): ValidateAndParseResult<T>;
|
|
97
94
|
|
|
98
95
|
/** Ultra-fast buffer validation via native addon */
|
|
99
96
|
isValid(input: Buffer | Uint8Array | string): boolean;
|
|
@@ -128,7 +125,7 @@ export class Validator {
|
|
|
128
125
|
toStandaloneModule(options?: { format?: 'esm' | 'cjs'; abortEarly?: boolean }): string | null;
|
|
129
126
|
|
|
130
127
|
/** Load a pre-compiled standalone module. Zero schema compilation at startup. */
|
|
131
|
-
static fromStandalone(mod: StandaloneModule, schema: object | string, options?: ValidatorOptions): Validator
|
|
128
|
+
static fromStandalone<T = unknown>(mod: StandaloneModule, schema: object | string, options?: ValidatorOptions): Validator<T>;
|
|
132
129
|
|
|
133
130
|
/** Bundle multiple schemas into a single JS module string. Load with Validator.loadBundle(). */
|
|
134
131
|
static bundle(schemas: object[], options?: ValidatorOptions): string;
|
|
@@ -155,16 +152,16 @@ export class Validator {
|
|
|
155
152
|
}
|
|
156
153
|
|
|
157
154
|
/** One-shot validate: creates a Validator, validates data, returns result. */
|
|
158
|
-
export function validate(
|
|
155
|
+
export function validate<T = unknown>(
|
|
159
156
|
schema: object | string,
|
|
160
157
|
data: unknown
|
|
161
|
-
): ValidationResult
|
|
158
|
+
): ValidationResult<T>;
|
|
162
159
|
|
|
163
160
|
/** Fast compile: returns a validate function directly. WeakMap cached, second call with same schema is near-zero cost. */
|
|
164
|
-
export function compile(
|
|
161
|
+
export function compile<T = unknown>(
|
|
165
162
|
schema: object | string,
|
|
166
163
|
options?: ValidatorOptions
|
|
167
|
-
): (data: unknown) => ValidationResult
|
|
164
|
+
): (data: unknown) => ValidationResult<T>;
|
|
168
165
|
|
|
169
166
|
/** Parse JSON using simdjson (native addon) or JSON.parse (fallback). */
|
|
170
167
|
export function parseJSON(jsonString: string | Buffer): unknown;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ata-validator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Ultra-fast JSON Schema validator. 5x faster validation, 159,000x 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",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"rebuild": "cmake-js rebuild --target ata",
|
|
42
42
|
"prebuild": "pkg-prebuilds-copy --baseDir build/Release --source ata.node --name=ata --strip --napi_version=10",
|
|
43
43
|
"prebuild-all": "npm run prebuild -- --arch x64 && npm run prebuild -- --arch arm64",
|
|
44
|
-
"test": "node test.js && node tests/test_no_native.js && node tests/test_aot_build.js && node tests/test_aot_differential.js && node tests/test_aot_cli_build.js && node tests/test_aot_cli_smoke.js && node tests/test_bundle_standalone.js",
|
|
44
|
+
"test": "node test.js && node tests/test_no_native.js && node tests/test_aot_build.js && node tests/test_aot_differential.js && node tests/test_aot_cli_build.js && node tests/test_aot_cli_smoke.js && node tests/test_bundle_standalone.js && node tests/test_typed_validator_runner.js",
|
|
45
45
|
"test:suite": "node tests/run_suite.js",
|
|
46
46
|
"test:compat": "node tests/test_compat.js",
|
|
47
47
|
"test:standard-schema": "node tests/test_standard_schema.js",
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"test:ts": "node tests/test_ts_gen.js",
|
|
50
50
|
"test:ts-corpus": "node tests/test_ts_corpus.js",
|
|
51
51
|
"test:ts-differential": "node tests/test_ts_differential.js",
|
|
52
|
+
"test:typed": "node tests/test_typed_validator_runner.js",
|
|
52
53
|
"bench": "node benchmark/bench_large.js",
|
|
53
54
|
"fuzz": "node tests/fuzz_differential.js",
|
|
54
55
|
"fuzz:long": "FUZZ_ITERATIONS=100000 node tests/fuzz_differential.js",
|
|
@@ -119,10 +120,13 @@
|
|
|
119
120
|
"yaml": "^2.0.0"
|
|
120
121
|
},
|
|
121
122
|
"peerDependenciesMeta": {
|
|
122
|
-
"yaml": {
|
|
123
|
+
"yaml": {
|
|
124
|
+
"optional": true
|
|
125
|
+
}
|
|
123
126
|
},
|
|
124
127
|
"devDependencies": {
|
|
125
128
|
"@sinclair/typebox": "^0.34.49",
|
|
129
|
+
"@types/node": "^25.8.0",
|
|
126
130
|
"cmake-js": "^8.0.0",
|
|
127
131
|
"mitata": "^1.0.34",
|
|
128
132
|
"typebox": "^1.1.7",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
Binary file
|