@jarenjs/validate 0.8.4 → 0.9.2

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 (48) hide show
  1. package/ARCHITECTURE.md +1067 -0
  2. package/LICENSE +21 -0
  3. package/README.md +339 -2
  4. package/dist/types/array.d.ts +2 -0
  5. package/dist/types/bigint.d.ts +1 -0
  6. package/dist/types/combine.d.ts +1 -0
  7. package/dist/types/condition.d.ts +1 -0
  8. package/dist/types/content.d.ts +3 -0
  9. package/dist/types/data.d.ts +7 -0
  10. package/dist/types/dollar-data.d.ts +20 -0
  11. package/dist/types/dynamic-ref.d.ts +44 -0
  12. package/dist/types/enum.d.ts +1 -0
  13. package/dist/types/format.d.ts +21 -0
  14. package/dist/types/index.d.ts +874 -0
  15. package/dist/types/number.d.ts +1 -0
  16. package/dist/types/object.d.ts +3 -0
  17. package/dist/types/query-keyword.d.ts +19 -0
  18. package/dist/types/query.d.ts +29 -0
  19. package/dist/types/schema.d.ts +1 -0
  20. package/dist/types/string.d.ts +1 -0
  21. package/dist/types/tools.d.ts +51 -0
  22. package/dist/types/traverse.d.ts +32 -0
  23. package/dist/types/unevaluated.d.ts +12 -0
  24. package/package.json +32 -7
  25. package/src/array.js +565 -0
  26. package/src/bigint.js +97 -0
  27. package/src/combine.js +226 -0
  28. package/src/condition.js +109 -0
  29. package/src/content.js +83 -0
  30. package/src/data.js +477 -0
  31. package/src/dollar-data.js +629 -0
  32. package/src/dynamic-ref.js +121 -0
  33. package/src/enum.js +148 -0
  34. package/src/format.js +66 -0
  35. package/src/index.js +1854 -0
  36. package/src/number.js +159 -0
  37. package/src/object.js +755 -0
  38. package/src/query-keyword.js +99 -0
  39. package/src/query.js +59 -0
  40. package/src/schema.js +645 -0
  41. package/src/string.js +152 -0
  42. package/src/tools.js +205 -0
  43. package/src/traverse.js +433 -0
  44. package/src/unevaluated.js +151 -0
  45. package/dist/index.js +0 -1998
  46. package/dist/index.js.map +0 -7
  47. package/dist/index.min.js +0 -2
  48. package/dist/index.min.js.map +0 -7
@@ -0,0 +1,874 @@
1
+ import { TraverseOptions } from './traverse.js';
2
+ import { EvalLog } from './tools.js';
3
+ export { registerFormatCompilers } from './format.js';
4
+ export { TraverseOptions };
5
+ export type DollarDataRef = {
6
+ /**
7
+ * - Relative JSON Pointer resolved from the current data location
8
+ */
9
+ $data: string;
10
+ };
11
+ export type DataKeywordSchema = {
12
+ /**
13
+ * - JSON Pointer to the minimum value
14
+ */
15
+ minimum?: string;
16
+ /**
17
+ * - JSON Pointer to the maximum value
18
+ */
19
+ maximum?: string;
20
+ /**
21
+ * - JSON Pointer to the exclusive minimum value
22
+ */
23
+ exclusiveMinimum?: string;
24
+ /**
25
+ * - JSON Pointer to the exclusive maximum value
26
+ */
27
+ exclusiveMaximum?: string;
28
+ /**
29
+ * - JSON Pointer to the multipleOf value
30
+ */
31
+ multipleOf?: string;
32
+ /**
33
+ * - JSON Pointer to the minLength value
34
+ */
35
+ minLength?: string;
36
+ /**
37
+ * - JSON Pointer to the maxLength value
38
+ */
39
+ maxLength?: string;
40
+ /**
41
+ * - JSON Pointer to the pattern string
42
+ */
43
+ pattern?: string;
44
+ /**
45
+ * - JSON Pointer to the format name
46
+ */
47
+ format?: string;
48
+ /**
49
+ * - JSON Pointer to an array of valid values
50
+ */
51
+ enum?: string;
52
+ /**
53
+ * - JSON Pointer to the constant value
54
+ */
55
+ const?: string;
56
+ /**
57
+ * - JSON Pointer to the minItems value
58
+ */
59
+ minItems?: string;
60
+ /**
61
+ * - JSON Pointer to the maxItems value
62
+ */
63
+ maxItems?: string;
64
+ /**
65
+ * - JSON Pointer to the minProperties value
66
+ */
67
+ minProperties?: string;
68
+ /**
69
+ * - JSON Pointer to the maxProperties value
70
+ */
71
+ maxProperties?: string;
72
+ };
73
+ export type JSONSchemaKeywords = {
74
+ /**
75
+ * - Schema resource identifier (URI)
76
+ */
77
+ $id?: string;
78
+ /**
79
+ * - Meta-schema URI declaring the draft dialect
80
+ */
81
+ $schema?: string;
82
+ /**
83
+ * - Reference to another schema (URI reference)
84
+ */
85
+ $ref?: string;
86
+ /**
87
+ * - Plain-name fragment identifier (2019-09+)
88
+ */
89
+ $anchor?: string;
90
+ /**
91
+ * - Dynamic reference (2020-12)
92
+ */
93
+ $dynamicRef?: string;
94
+ /**
95
+ * - Dynamic anchor (2020-12)
96
+ */
97
+ $dynamicAnchor?: string;
98
+ /**
99
+ * - Vocabulary declarations of a meta-schema
100
+ */
101
+ $vocabulary?: Record<string, boolean>;
102
+ /**
103
+ * - Comment for schema maintainers; not used in validation
104
+ */
105
+ $comment?: string;
106
+ /**
107
+ * - Reusable subschema definitions (2019-09+)
108
+ */
109
+ $defs?: Record<string, JSONSchema>;
110
+ /**
111
+ * - Reusable subschema definitions (draft-07 and earlier)
112
+ */
113
+ definitions?: Record<string, JSONSchema>;
114
+ /**
115
+ * - Expected JSON type(s): 'null', 'boolean', 'object', 'array', 'number', 'string' or 'integer'
116
+ */
117
+ type?: string | string[];
118
+ /**
119
+ * - Exhaustive list of valid values
120
+ */
121
+ enum?: unknown[] | DollarDataRef;
122
+ /**
123
+ * - Single valid value
124
+ */
125
+ const?: unknown | DollarDataRef;
126
+ /**
127
+ * - Minimum string length (in graphemes by default)
128
+ */
129
+ minLength?: number | DollarDataRef;
130
+ /**
131
+ * - Maximum string length (in graphemes by default)
132
+ */
133
+ maxLength?: number | DollarDataRef;
134
+ /**
135
+ * - ECMA-262 regular expression the string must match
136
+ */
137
+ pattern?: string | DollarDataRef;
138
+ /**
139
+ * - Encoding of a string-embedded document (e.g. 'base64')
140
+ */
141
+ contentEncoding?: string;
142
+ /**
143
+ * - Media type of a string-embedded document
144
+ */
145
+ contentMediaType?: string;
146
+ /**
147
+ * - Schema for the decoded string-embedded document
148
+ */
149
+ contentSchema?: JSONSchema;
150
+ /**
151
+ * - Number must be a multiple of this value
152
+ */
153
+ multipleOf?: number | DollarDataRef;
154
+ /**
155
+ * - Inclusive lower bound
156
+ */
157
+ minimum?: number | DollarDataRef;
158
+ /**
159
+ * - Inclusive upper bound
160
+ */
161
+ maximum?: number | DollarDataRef;
162
+ /**
163
+ * - Exclusive lower bound (boolean form in draft-04 style schemas)
164
+ */
165
+ exclusiveMinimum?: number | boolean | DollarDataRef;
166
+ /**
167
+ * - Exclusive upper bound (boolean form in draft-04 style schemas)
168
+ */
169
+ exclusiveMaximum?: number | boolean | DollarDataRef;
170
+ /**
171
+ * - Schemas for named object members
172
+ */
173
+ properties?: Record<string, JSONSchema>;
174
+ /**
175
+ * - Schemas for members whose name matches a regular expression
176
+ */
177
+ patternProperties?: Record<string, JSONSchema>;
178
+ /**
179
+ * - Schema for members not matched by properties/patternProperties
180
+ */
181
+ additionalProperties?: boolean | JSONSchema;
182
+ /**
183
+ * - Schema for members not evaluated by any subschema (2019-09+)
184
+ */
185
+ unevaluatedProperties?: boolean | JSONSchema;
186
+ /**
187
+ * - Member names that must be present
188
+ */
189
+ required?: string[] | DollarDataRef;
190
+ /**
191
+ * - Schema every member name must validate against
192
+ */
193
+ propertyNames?: JSONSchema;
194
+ /**
195
+ * - Minimum number of members
196
+ */
197
+ minProperties?: number | DollarDataRef;
198
+ /**
199
+ * - Maximum number of members
200
+ */
201
+ maxProperties?: number | DollarDataRef;
202
+ /**
203
+ * - Schema for array elements (array form is the draft-07 tuple syntax)
204
+ */
205
+ items?: JSONSchema | JSONSchema[];
206
+ /**
207
+ * - Tuple element schemas (2020-12)
208
+ */
209
+ prefixItems?: JSONSchema[];
210
+ /**
211
+ * - Schema for elements beyond the tuple prefix (draft-07 and earlier)
212
+ */
213
+ additionalItems?: boolean | JSONSchema;
214
+ /**
215
+ * - Schema for elements not evaluated by any subschema (2019-09+)
216
+ */
217
+ unevaluatedItems?: boolean | JSONSchema;
218
+ /**
219
+ * - At least one element must validate against this schema
220
+ */
221
+ contains?: JSONSchema;
222
+ /**
223
+ * - Minimum number of elements
224
+ */
225
+ minItems?: number | DollarDataRef;
226
+ /**
227
+ * - Maximum number of elements
228
+ */
229
+ maxItems?: number | DollarDataRef;
230
+ /**
231
+ * - Whether all elements must be unique
232
+ */
233
+ uniqueItems?: boolean | DollarDataRef;
234
+ /**
235
+ * - Minimum number of elements matching 'contains' (2019-09+)
236
+ */
237
+ minContains?: number;
238
+ /**
239
+ * - Maximum number of elements matching 'contains' (2019-09+)
240
+ */
241
+ maxContains?: number;
242
+ /**
243
+ * - Value must validate against all of these schemas
244
+ */
245
+ allOf?: JSONSchema[];
246
+ /**
247
+ * - Value must validate against at least one of these schemas
248
+ */
249
+ anyOf?: JSONSchema[];
250
+ /**
251
+ * - Value must validate against exactly one of these schemas
252
+ */
253
+ oneOf?: JSONSchema[];
254
+ /**
255
+ * - Value must NOT validate against this schema
256
+ */
257
+ not?: JSONSchema;
258
+ /**
259
+ * - Condition schema selecting between 'then' and 'else'
260
+ */
261
+ if?: JSONSchema;
262
+ /**
263
+ * - Applied when 'if' validates
264
+ */
265
+ then?: JSONSchema;
266
+ /**
267
+ * - Applied when 'if' does not validate
268
+ */
269
+ else?: JSONSchema;
270
+ /**
271
+ * - Schemas applied when a member is present (2019-09+)
272
+ */
273
+ dependentSchemas?: Record<string, JSONSchema>;
274
+ /**
275
+ * - Members required when a member is present (2019-09+)
276
+ */
277
+ dependentRequired?: Record<string, string[]>;
278
+ /**
279
+ * - Short descriptive title
280
+ */
281
+ title?: string;
282
+ /**
283
+ * - Explanation of the schema's purpose
284
+ */
285
+ description?: string;
286
+ /**
287
+ * - Default value annotation
288
+ */
289
+ default?: unknown;
290
+ /**
291
+ * - Example values annotation
292
+ */
293
+ examples?: unknown[];
294
+ /**
295
+ * - Value is managed by the receiving authority
296
+ */
297
+ readOnly?: boolean;
298
+ /**
299
+ * - Value is never returned by the receiving authority
300
+ */
301
+ writeOnly?: boolean;
302
+ /**
303
+ * - Value is deprecated
304
+ */
305
+ deprecated?: boolean;
306
+ /**
307
+ * - Named semantic format (e.g. 'email', 'uri', 'date-time')
308
+ */
309
+ format?: string | DollarDataRef;
310
+ /**
311
+ * - Format-aware inclusive lower bound (non-standard, Ajv-style)
312
+ */
313
+ formatMinimum?: string;
314
+ /**
315
+ * - Format-aware inclusive upper bound (non-standard, Ajv-style)
316
+ */
317
+ formatMaximum?: string;
318
+ /**
319
+ * - Format-aware exclusive lower bound (non-standard, Ajv-style)
320
+ */
321
+ formatExclusiveMinimum?: string;
322
+ /**
323
+ * - Format-aware exclusive upper bound (non-standard, Ajv-style)
324
+ */
325
+ formatExclusiveMaximum?: string;
326
+ /**
327
+ * - Data keyword referencing instance data (json-everything style)
328
+ */
329
+ data?: DataKeywordSchema;
330
+ };
331
+ export type JSONSchema = JSONSchemaKeywords & Record<string, unknown>;
332
+ export type FormatCompiler = (schemaObj: ValidationObject, jsonSchema: JSONSchema & {
333
+ format?: string;
334
+ }) => ((data: unknown, dataPath?: string) => boolean) | undefined;
335
+ /**
336
+ * Ajv-style $data reference object.
337
+ * The value is a Relative JSON Pointer that resolves from the current data location.
338
+ * Format: `<non-negative-integer>("#"|<json-pointer>)`
339
+ * - "0" - The current value itself
340
+ * - "0#" - The property name/index of the current value
341
+ * - "0/foo" - The "foo" property of the current value
342
+ * - "1" - The parent value
343
+ * - "1/foo" - The "foo" property of the parent value
344
+ * @see https://github.com/ajv-validator/ajv/tree/master/spec/extras/%24data
345
+ * @typedef {Object} DollarDataRef
346
+ * @property {string} $data - Relative JSON Pointer resolved from the current data location
347
+ */
348
+ /**
349
+ * Data keyword schema for referencing instance data (json-everything style).
350
+ * Allows constraints to reference values from other parts of the instance;
351
+ * every property value is a (relative) JSON Pointer to the constraint's value.
352
+ * @see https://docs.json-everything.net/schema/examples/data-ref/
353
+ * @typedef {Object} DataKeywordSchema
354
+ * @property {string} [minimum] - JSON Pointer to the minimum value
355
+ * @property {string} [maximum] - JSON Pointer to the maximum value
356
+ * @property {string} [exclusiveMinimum] - JSON Pointer to the exclusive minimum value
357
+ * @property {string} [exclusiveMaximum] - JSON Pointer to the exclusive maximum value
358
+ * @property {string} [multipleOf] - JSON Pointer to the multipleOf value
359
+ * @property {string} [minLength] - JSON Pointer to the minLength value
360
+ * @property {string} [maxLength] - JSON Pointer to the maxLength value
361
+ * @property {string} [pattern] - JSON Pointer to the pattern string
362
+ * @property {string} [format] - JSON Pointer to the format name
363
+ * @property {string} [enum] - JSON Pointer to an array of valid values
364
+ * @property {string} [const] - JSON Pointer to the constant value
365
+ * @property {string} [minItems] - JSON Pointer to the minItems value
366
+ * @property {string} [maxItems] - JSON Pointer to the maxItems value
367
+ * @property {string} [minProperties] - JSON Pointer to the minProperties value
368
+ * @property {string} [maxProperties] - JSON Pointer to the maxProperties value
369
+ */
370
+ /**
371
+ * The standard JSON Schema keywords understood by Jaren
372
+ * (draft-06 through draft 2020-12). See {@link JSONSchema} for the full
373
+ * schema object type that also permits custom keywords.
374
+ * @typedef {Object} JSONSchemaKeywords
375
+ * @property {string} [$id] - Schema resource identifier (URI)
376
+ * @property {string} [$schema] - Meta-schema URI declaring the draft dialect
377
+ * @property {string} [$ref] - Reference to another schema (URI reference)
378
+ * @property {string} [$anchor] - Plain-name fragment identifier (2019-09+)
379
+ * @property {string} [$dynamicRef] - Dynamic reference (2020-12)
380
+ * @property {string} [$dynamicAnchor] - Dynamic anchor (2020-12)
381
+ * @property {Record<string, boolean>} [$vocabulary] - Vocabulary declarations of a meta-schema
382
+ * @property {string} [$comment] - Comment for schema maintainers; not used in validation
383
+ * @property {Record<string, JSONSchema>} [$defs] - Reusable subschema definitions (2019-09+)
384
+ * @property {Record<string, JSONSchema>} [definitions] - Reusable subschema definitions (draft-07 and earlier)
385
+ * @property {string | string[]} [type] - Expected JSON type(s): 'null', 'boolean', 'object', 'array', 'number', 'string' or 'integer'
386
+ * @property {unknown[] | DollarDataRef} [enum] - Exhaustive list of valid values
387
+ * @property {unknown | DollarDataRef} [const] - Single valid value
388
+ * @property {number | DollarDataRef} [minLength] - Minimum string length (in graphemes by default)
389
+ * @property {number | DollarDataRef} [maxLength] - Maximum string length (in graphemes by default)
390
+ * @property {string | DollarDataRef} [pattern] - ECMA-262 regular expression the string must match
391
+ * @property {string} [contentEncoding] - Encoding of a string-embedded document (e.g. 'base64')
392
+ * @property {string} [contentMediaType] - Media type of a string-embedded document
393
+ * @property {JSONSchema} [contentSchema] - Schema for the decoded string-embedded document
394
+ * @property {number | DollarDataRef} [multipleOf] - Number must be a multiple of this value
395
+ * @property {number | DollarDataRef} [minimum] - Inclusive lower bound
396
+ * @property {number | DollarDataRef} [maximum] - Inclusive upper bound
397
+ * @property {number | boolean | DollarDataRef} [exclusiveMinimum] - Exclusive lower bound (boolean form in draft-04 style schemas)
398
+ * @property {number | boolean | DollarDataRef} [exclusiveMaximum] - Exclusive upper bound (boolean form in draft-04 style schemas)
399
+ * @property {Record<string, JSONSchema>} [properties] - Schemas for named object members
400
+ * @property {Record<string, JSONSchema>} [patternProperties] - Schemas for members whose name matches a regular expression
401
+ * @property {boolean | JSONSchema} [additionalProperties] - Schema for members not matched by properties/patternProperties
402
+ * @property {boolean | JSONSchema} [unevaluatedProperties] - Schema for members not evaluated by any subschema (2019-09+)
403
+ * @property {string[] | DollarDataRef} [required] - Member names that must be present
404
+ * @property {JSONSchema} [propertyNames] - Schema every member name must validate against
405
+ * @property {number | DollarDataRef} [minProperties] - Minimum number of members
406
+ * @property {number | DollarDataRef} [maxProperties] - Maximum number of members
407
+ * @property {JSONSchema | JSONSchema[]} [items] - Schema for array elements (array form is the draft-07 tuple syntax)
408
+ * @property {JSONSchema[]} [prefixItems] - Tuple element schemas (2020-12)
409
+ * @property {boolean | JSONSchema} [additionalItems] - Schema for elements beyond the tuple prefix (draft-07 and earlier)
410
+ * @property {boolean | JSONSchema} [unevaluatedItems] - Schema for elements not evaluated by any subschema (2019-09+)
411
+ * @property {JSONSchema} [contains] - At least one element must validate against this schema
412
+ * @property {number | DollarDataRef} [minItems] - Minimum number of elements
413
+ * @property {number | DollarDataRef} [maxItems] - Maximum number of elements
414
+ * @property {boolean | DollarDataRef} [uniqueItems] - Whether all elements must be unique
415
+ * @property {number} [minContains] - Minimum number of elements matching 'contains' (2019-09+)
416
+ * @property {number} [maxContains] - Maximum number of elements matching 'contains' (2019-09+)
417
+ * @property {JSONSchema[]} [allOf] - Value must validate against all of these schemas
418
+ * @property {JSONSchema[]} [anyOf] - Value must validate against at least one of these schemas
419
+ * @property {JSONSchema[]} [oneOf] - Value must validate against exactly one of these schemas
420
+ * @property {JSONSchema} [not] - Value must NOT validate against this schema
421
+ * @property {JSONSchema} [if] - Condition schema selecting between 'then' and 'else'
422
+ * @property {JSONSchema} [then] - Applied when 'if' validates
423
+ * @property {JSONSchema} [else] - Applied when 'if' does not validate
424
+ * @property {Record<string, JSONSchema>} [dependentSchemas] - Schemas applied when a member is present (2019-09+)
425
+ * @property {Record<string, string[]>} [dependentRequired] - Members required when a member is present (2019-09+)
426
+ * @property {string} [title] - Short descriptive title
427
+ * @property {string} [description] - Explanation of the schema's purpose
428
+ * @property {unknown} [default] - Default value annotation
429
+ * @property {unknown[]} [examples] - Example values annotation
430
+ * @property {boolean} [readOnly] - Value is managed by the receiving authority
431
+ * @property {boolean} [writeOnly] - Value is never returned by the receiving authority
432
+ * @property {boolean} [deprecated] - Value is deprecated
433
+ * @property {string | DollarDataRef} [format] - Named semantic format (e.g. 'email', 'uri', 'date-time')
434
+ * @property {string} [formatMinimum] - Format-aware inclusive lower bound (non-standard, Ajv-style)
435
+ * @property {string} [formatMaximum] - Format-aware inclusive upper bound (non-standard, Ajv-style)
436
+ * @property {string} [formatExclusiveMinimum] - Format-aware exclusive lower bound (non-standard, Ajv-style)
437
+ * @property {string} [formatExclusiveMaximum] - Format-aware exclusive upper bound (non-standard, Ajv-style)
438
+ * @property {DataKeywordSchema} [data] - Data keyword referencing instance data (json-everything style)
439
+ */
440
+ /**
441
+ * Represents a JSON Schema object.
442
+ * Covers the standard keywords of drafts 06, 07, 2019-09 and 2020-12
443
+ * (see {@link JSONSchemaKeywords}) while remaining open for custom
444
+ * keywords: any property outside the standard set is permitted.
445
+ * Note that a complete schema is `JSONSchema | boolean` - the boolean
446
+ * forms accept everything (`true`) or nothing (`false`).
447
+ * @typedef {JSONSchemaKeywords & Record<string, unknown>} JSONSchema
448
+ */
449
+ /**
450
+ * A format compiler function.
451
+ * Called once per schema location at compile time with the compiling
452
+ * ValidationObject and the schema that declares the format; returns the
453
+ * format validator that is invoked for each instance value, or undefined
454
+ * when the format does not apply to the schema location. Compilers are
455
+ * only invoked for schemas whose `format` member is a plain string.
456
+ * @typedef {(schemaObj: ValidationObject, jsonSchema: JSONSchema & {format?: string}) => ((data: unknown, dataPath?: string) => boolean) | undefined} FormatCompiler
457
+ */
458
+ export declare const DEFAULT_SCHEMA_DRAFT = "http://json-schema.org/draft-06/schema#";
459
+ /**
460
+ * Detects the JSON Schema draft version from the schema's $schema property
461
+ * @param {object} schema - The JSON schema
462
+ * @returns {number} - The draft version (6, 7, 2019, or 2020)
463
+ */
464
+ export declare function detectSchemaDraft(schema: object): number;
465
+ declare class InternalValidationError {
466
+ timeStamp: any;
467
+ object: any;
468
+ key: any;
469
+ expected: any;
470
+ dataKey: any;
471
+ value: any;
472
+ rest: any;
473
+ constructor(obj: any, key: any, expected: any, dataKey: any, value: any, rest: any);
474
+ }
475
+ /**
476
+ * JSON Schema Validation Error
477
+ * Represents a validation error according to the JSON Schema specification.
478
+ * @see https://json-schema.org/draft/2020-12/json-schema-core.html#output
479
+ */
480
+ export declare class ValidationError {
481
+ keyword: string;
482
+ instancePath: string;
483
+ schemaPath: string;
484
+ params: object;
485
+ message: string;
486
+ /**
487
+ * @param {object} options - Error options
488
+ * @param {string} options.keyword - The keyword that failed validation
489
+ * @param {string} options.instancePath - JSON Pointer to the data location
490
+ * @param {string} options.schemaPath - JSON Pointer to the schema location
491
+ * @param {object} options.params - Keyword-specific parameters
492
+ * @param {string} [options.message] - Human-readable error message
493
+ */
494
+ constructor(options: {
495
+ keyword: string;
496
+ instancePath: string;
497
+ schemaPath: string;
498
+ params: object;
499
+ message?: string;
500
+ });
501
+ /**
502
+ * Convert error to a plain object
503
+ * @returns {object} Plain object representation
504
+ */
505
+ toJSON(): object;
506
+ }
507
+ /**
508
+ * ValidationOptions configures the behavior of the validation process.
509
+ * @class
510
+ */
511
+ export declare class ValidationOptions {
512
+ /** @type {boolean} Whether to stop at first error or continue */
513
+ skipErrors: boolean;
514
+ /** @type {boolean} Whether to use grapheme cluster counting for string length */
515
+ useGrapheme: boolean;
516
+ /** @type {boolean} Whether to collect and return detailed errors */
517
+ collectErrors: boolean;
518
+ /** @type {boolean|null} Whether to validate contentEncoding/contentMediaType (null = auto based on draft) */
519
+ contentValidation: boolean | null;
520
+ /** @type {number} The JSON Schema draft version (6, 7, 2019, or 2020) */
521
+ draftVersion: number;
522
+ /** @type {boolean} Whether validation vocabulary keywords (type, minimum, ...) are asserted */
523
+ vocabValidation: boolean;
524
+ /** @type {boolean|null} Whether the format keyword asserts (null = auto by draft) */
525
+ formatAssertion: boolean | null;
526
+ /**
527
+ * Creates validation options.
528
+ * @param {boolean} [skipErrors=true] - Whether to stop at first error or continue
529
+ * @param {boolean} [useGrapheme=true] - Whether to use grapheme cluster counting for strings
530
+ * @param {boolean} [collectErrors=false] - Whether to collect all errors or just return boolean
531
+ * @param {boolean|null} [contentValidation=null] - Whether to validate contentEncoding/contentMediaType (null = auto based on draft)
532
+ * @param {number} [draftVersion=7] - The JSON Schema draft version (6, 7, 2019, or 2020)
533
+ * @param {boolean} [vocabValidation=true] - Whether the validation vocabulary is enabled (false when the schema's metaschema omits it via $vocabulary)
534
+ * @param {boolean|null} [formatAssertion=null] - Whether format asserts (null = auto: asserts below draft 2020-12, annotation-only from 2020-12 on)
535
+ */
536
+ constructor(skipErrors?: boolean, useGrapheme?: boolean, collectErrors?: boolean, contentValidation?: boolean | null, draftVersion?: number, vocabValidation?: boolean, formatAssertion?: boolean | null);
537
+ }
538
+ /**
539
+ * ValidationRoot manages the compilation and validation context for a schema.
540
+ * It holds references to all schemas, formats, options, and compiled ValidationObjects.
541
+ * @class
542
+ */
543
+ export declare class ValidationRoot {
544
+ #private;
545
+ /**
546
+ * Creates a new ValidationRoot.
547
+ * @param {string} origin - The root schema origin/URI
548
+ * @param {Map} schemas - Map of schema paths to schema objects
549
+ * @param {Record<string, FormatCompiler>} formats - Registered format validators
550
+ * @param {ValidationOptions} [opts] - Validation options
551
+ * @param {TraverseOptions} [traverse] - Schema traversal options
552
+ * @param {object|null} [owner] - The owning JarenValidator instance; extension
553
+ * keywords ('$query') compile embedded schema literals against it so their
554
+ * `$ref`s resolve to the owner's `addSchema` registrations
555
+ */
556
+ constructor(origin: string, schemas: Map<any, any>, formats: Record<string, FormatCompiler>, opts?: ValidationOptions, traverse?: TraverseOptions, owner?: object | null);
557
+ /** @returns {string} The root schema origin/URI */
558
+ get rootOrigin(): string;
559
+ /** @returns {TraverseOptions} Schema traversal options */
560
+ get traverse(): TraverseOptions;
561
+ /** @returns {ValidationOptions} Validation options */
562
+ get options(): ValidationOptions;
563
+ /** @returns {object} Registered format validators */
564
+ get formats(): object;
565
+ /** @returns {Array} Array of validation errors */
566
+ get errors(): any[];
567
+ /** @returns {ValidationObject} The root schema's ValidationObject */
568
+ get firstSchema(): ValidationObject;
569
+ /** @returns {boolean} Whether any schema in this compilation contains a $data reference */
570
+ get usesDollarData(): boolean;
571
+ /** @returns {boolean} Whether any schema in this compilation contains unevaluatedProperties/unevaluatedItems */
572
+ get usesUnevaluated(): boolean;
573
+ /** @returns {EvalLog} The evaluation log for unevaluated* annotation tracking */
574
+ get evalLog(): EvalLog;
575
+ /** @returns {object|null} The owning JarenValidator instance, or null when constructed standalone */
576
+ get owner(): object | null;
577
+ /**
578
+ * Creates a new ValidationObject for the given path and schema.
579
+ * @param {string} path - The URI path for this schema object
580
+ * @param {object|boolean} schema - The JSON schema
581
+ * @param {string} baseUri - The base URI for resolving relative refs
582
+ * @returns {ValidationObject} The created ValidationObject
583
+ */
584
+ createObject(path: string, schema: object | boolean, baseUri: string, parentDeclaredDraft?: null): ValidationObject;
585
+ /**
586
+ * Checks if an object exists at the given path without creating it.
587
+ * @param {string} path - The URI path to check
588
+ * @returns {ValidationObject|null|undefined} The existing object, null if marked unresolved, or undefined if not known
589
+ */
590
+ unresolvedObject(path: string): ValidationObject | null | undefined;
591
+ /**
592
+ * Gets the raw schema object for a given reference without compiling it.
593
+ * Used to check schema properties (like $recursiveAnchor) at compile time.
594
+ * @param {string} ref - The reference URI to resolve
595
+ * @param {string} path - The current path (for error messages)
596
+ * @param {object} schema - The schema containing the $ref
597
+ * @returns {{id: string, schema: object}|null} The resolved schema info or null
598
+ */
599
+ getRawSchema(ref: string, path: string, schema: object): {
600
+ id: string;
601
+ schema: object;
602
+ } | null;
603
+ /**
604
+ * Gets the raw schema object by its URI/ID directly from the schemas map.
605
+ * This performs a direct lookup without following references.
606
+ * @param {string} uri - The schema URI to look up
607
+ * @returns {object|undefined} The raw schema object or undefined
608
+ */
609
+ getSchemaByUri(uri: string): object | undefined;
610
+ /**
611
+ * Resolves a $ref to a ValidationObject, creating it if necessary.
612
+ * @param {string} ref - The reference URI to resolve
613
+ * @param {string} path - The current path (for error messages)
614
+ * @param {object} schema - The schema containing the $ref
615
+ * @returns {ValidationObject} The resolved ValidationObject
616
+ */
617
+ resolveObject(ref: string, path: string, schema: object): ValidationObject;
618
+ /**
619
+ * Adds an error to the validation errors list.
620
+ * @param {InternalValidationError} error - The error to add
621
+ * @returns {boolean} Always returns false for convenience in validators
622
+ */
623
+ addError(error: InternalValidationError): boolean;
624
+ /**
625
+ * Validates data against the root schema.
626
+ * @param {unknown} data - The data to validate
627
+ * @returns {boolean} True if valid, false otherwise
628
+ */
629
+ validate(data: unknown): boolean;
630
+ /**
631
+ * Get the stored validator for a dynamic anchor.
632
+ * Used by $dynamicRef for runtime resolution.
633
+ * Per draft 2020-12, $dynamicRef resolves to the FIRST (outermost)
634
+ * resource in the dynamic scope that defines the anchor.
635
+ * @param {string} anchorName - The anchor name
636
+ * @returns {Function|null} The validator function or null if not set
637
+ */
638
+ getDynamicAnchorValidator(anchorName: string): Function | null;
639
+ /**
640
+ * Get the outermost validator for a recursive anchor.
641
+ * Used by $recursiveRef for runtime resolution.
642
+ * Returns the bottom of the stack (first/outermost registered validator).
643
+ * @param {string} anchorName - The anchor name (empty string for $recursiveRef)
644
+ * @returns {Function|null} The validator function or null if not set
645
+ */
646
+ getOutermostDynamicAnchorValidator(anchorName: string): Function | null;
647
+ /**
648
+ * Push a validator onto the stack for a dynamic anchor.
649
+ * Called when entering a schema with $recursiveAnchor or $dynamicAnchor.
650
+ * @param {string} anchorName - The anchor name
651
+ * @param {Function} validator - The validator function
652
+ */
653
+ pushDynamicAnchorValidator(anchorName: string, validator: Function): void;
654
+ /**
655
+ * Pop a validator from the stack for a dynamic anchor.
656
+ * Called when exiting a schema with $recursiveAnchor or $dynamicAnchor.
657
+ * @param {string} anchorName - The anchor name
658
+ */
659
+ popDynamicAnchorValidator(anchorName: string): void;
660
+ /**
661
+ * Get or create a validator for a given schema.
662
+ * This is used when we need a validator for a schema at validation time
663
+ * (e.g., for dynamic anchors collected from $defs).
664
+ * @param {object} schema - The schema to create a validator for
665
+ * @param {string} basePath - The base path for the schema
666
+ * @param {string} baseUri - The base URI for the schema
667
+ * @returns {Function} The validator function
668
+ */
669
+ getOrCreateValidator(schema: object, basePath: string, baseUri: string): Function;
670
+ }
671
+ /**
672
+ * ValidationObject represents a single schema location with its compiled validator.
673
+ * It handles the compilation of schema validation logic and provides methods for
674
+ * creating child validators and error handlers.
675
+ * @class
676
+ */
677
+ export declare class ValidationObject {
678
+ #private;
679
+ /**
680
+ * Compiles a validator function for the given schema.
681
+ * This is the main entry point for schema compilation. It handles:
682
+ * - Simple schemas (type-only, required-only) via fast paths
683
+ * - Schemas with $ref by resolving to target validators
684
+ * - Complex schemas by delegating to compileSchemaObject
685
+ * @param {ValidationObject} self - The validation object that is compiling this validator
686
+ * @param {string} path - The path to this schema object (its URI identifier)
687
+ * @param {any} schema - The schema object to compile
688
+ * @param {string} baseUri - The base URI for resolving $ref (parent's base, before any sibling $id)
689
+ * @returns {function(any, any):boolean} A function that validates data against the compiled schema and returns a boolean.
690
+ */
691
+ static compileValidator(self: ValidationObject, path: string, schema: any, baseUri: string): Function;
692
+ /**
693
+ * Creates a new ValidationObject.
694
+ * @param {ValidationRoot} root - The root validation context
695
+ * @param {string} path - The URI path identifying this schema object
696
+ * @param {any} schema - The schema object to compile
697
+ * @param {string} baseUri - The base URI for resolving $ref
698
+ * @param {number|null} [parentDeclaredDraft] - The declared draft inherited from the parent schema object
699
+ */
700
+ constructor(root: ValidationRoot, path: string, schema: any, baseUri: string, parentDeclaredDraft?: number | null);
701
+ /** @returns {string} The URI path identifying this schema object */
702
+ get path(): string;
703
+ /** @returns {string} The effective base URI for resolving relative $refs */
704
+ get baseUri(): string;
705
+ /** @returns {Array} The current validation errors from the root */
706
+ get errors(): any[];
707
+ /** @returns {function} The compiled validator function */
708
+ get validate(): Function;
709
+ /** @returns {ValidationOptions} The validation options */
710
+ get options(): ValidationOptions;
711
+ /** @returns {object} The registered format validators */
712
+ get formats(): object;
713
+ /** @returns {ValidationRoot} The root validation context */
714
+ get root(): ValidationRoot;
715
+ /** @returns {object} The schema object */
716
+ get schema(): object;
717
+ /** @returns {number|null} Draft version declared by this schema's document via $schema, or null when never declared */
718
+ get declaredDraft(): number | null;
719
+ /**
720
+ * Creates an error handler function for validation failures.
721
+ * @param {any} expected - The expected value that failed validation
722
+ * @param {string | string[]} key - The keyword or keywords that failed
723
+ * @returns {(data: unknown, ...meta: any[]) => boolean} A function that adds an error and returns false
724
+ */
725
+ createErrorHandler(expected: any, key: string | string[]): (data: unknown, ...meta: any[]) => boolean;
726
+ /**
727
+ * Creates a validator function for a child schema.
728
+ * This is used when compiling nested schemas (e.g., array items, object properties).
729
+ * @param {JSONSchema | boolean} schema - The child schema to compile
730
+ * @param {string} key - The property key where the schema is located
731
+ * @param {number} [index] - Optional array index for tuple items
732
+ * @returns {function|undefined} The compiled validator function, or undefined if schema is invalid
733
+ */
734
+ createValidator(schema: JSONSchema | boolean, key: string, index?: number): Function | undefined;
735
+ }
736
+ /**
737
+ * ValidatorOptions configures the JarenValidator instance.
738
+ * Can be created with positional arguments or an options object.
739
+ * @class
740
+ * @example
741
+ * // Positional arguments
742
+ * const options = new ValidatorOptions(formats, schemas, validation, traverse);
743
+ *
744
+ * // Options object (recommended)
745
+ * const options = new ValidatorOptions({
746
+ * formats: { custom: validator },
747
+ * collectErrors: true,
748
+ * useGrapheme: false
749
+ * });
750
+ */
751
+ export declare class ValidatorOptions {
752
+ /** @type {object} Registered format validators */
753
+ formats: object;
754
+ /** @type {object[]} Initial schemas to register */
755
+ schemas: object[];
756
+ validation: ValidationOptions;
757
+ /** @type {TraverseOptions} Schema traversal options */
758
+ traverse: TraverseOptions;
759
+ /**
760
+ * Creates validator options.
761
+ * @param {object|object[]} [formats={}] - Format validators or options object
762
+ * @param {object[]} [schemas=[]] - Initial schemas to register
763
+ * @param {ValidationOptions} [validation] - Validation behavior options
764
+ * @param {TraverseOptions} [traverse] - Schema traversal options
765
+ */
766
+ constructor(formats?: object | object[], schemas?: object[], validation?: ValidationOptions, traverse?: TraverseOptions);
767
+ }
768
+ /**
769
+ * JarenValidator is the main entry point for JSON Schema validation.
770
+ * It manages schema registration, format registration, and compilation.
771
+ * @class
772
+ * @example
773
+ * const validator = new JarenValidator();
774
+ * validator.addSchema({ $id: 'http://example.com/schema', type: 'object' });
775
+ * const validate = validator.compile({ $ref: 'http://example.com/schema' });
776
+ * const valid = validate({ foo: 'bar' }); // true
777
+ */
778
+ export declare class JarenValidator {
779
+ #private;
780
+ /**
781
+ * Creates a new JarenValidator instance.
782
+ * @param {ValidatorOptions} [options] - Validator options including formats, schemas, validation options, and traverse options
783
+ */
784
+ constructor(options?: ValidatorOptions);
785
+ /**
786
+ * Adds a format validator.
787
+ * @param {string} name - The format name (e.g., 'email', 'uri', 'date-time')
788
+ * @param {FormatCompiler} formatCompiler - A function that compiles format validators
789
+ * @returns {JarenValidator} This validator instance for chaining
790
+ * @example
791
+ * validator.addFormat('custom', (schemaObj, schema) => {
792
+ * return (data) => data.startsWith('custom:');
793
+ * });
794
+ */
795
+ addFormat(name: string, formatCompiler: FormatCompiler): JarenValidator;
796
+ /**
797
+ * Adds multiple format validators at once.
798
+ * @param {Record<string, FormatCompiler>} formatCompilers - Object mapping format names to compiler functions
799
+ * @returns {JarenValidator} This validator instance for chaining
800
+ */
801
+ addFormats(formatCompilers: Record<string, FormatCompiler>): JarenValidator;
802
+ /**
803
+ * Adds schema(s) to the validator instance.
804
+ * This method does not compile schemas - it only registers them for reference.
805
+ * Dependencies can be added in any order, and circular dependencies are supported.
806
+ * @param {JSONSchema | boolean | (JSONSchema | boolean)[]} schema - The schema(s) to add
807
+ * @param {string} [key] - Optional key/URI to register the schema under
808
+ * @returns {JarenValidator} This validator instance for chaining
809
+ * @example
810
+ * // Add a single schema
811
+ * validator.addSchema({ $id: 'http://example.com/user', type: 'object' });
812
+ *
813
+ * // Add multiple schemas
814
+ * validator.addSchema([schema1, schema2]);
815
+ *
816
+ * // Add with explicit key
817
+ * validator.addSchema({ type: 'string' }, 'http://example.com/name');
818
+ */
819
+ addSchema(schema: JSONSchema | boolean | (JSONSchema | boolean)[], key?: string): JarenValidator;
820
+ static normalizeUriKey(key: any): any;
821
+ /**
822
+ * Adds meta-schema(s) that can be used to validate schemas.
823
+ * Meta-schemas are schemas that describe the structure of valid JSON schemas.
824
+ * @param {JSONSchema | boolean | (JSONSchema | boolean)[]} schema - The meta-schema(s) to add
825
+ * @param {string} [key] - Optional key/URI for the meta-schema
826
+ * @returns {JarenValidator} This validator instance for chaining
827
+ * @example
828
+ * validator.addMetaSchema(draft7MetaSchema, 'http://json-schema.org/draft-07/schema');
829
+ */
830
+ addMetaSchema(schema: JSONSchema | boolean | (JSONSchema | boolean)[], key?: string): JarenValidator;
831
+ /**
832
+ * Retrieves a registered schema by its key/URI.
833
+ * @param {string} key - The schema URI/key
834
+ * @returns {JSONSchema | boolean | null} The registered schema, or null if not found
835
+ */
836
+ getSchema(key: string): JSONSchema | boolean | null;
837
+ /**
838
+ * Validates a schema against a registered meta-schema.
839
+ * This is used to ensure schemas are valid according to the JSON Schema specification.
840
+ * @param {JSONSchema | boolean} schema - The schema to validate
841
+ * @returns {boolean} True if the schema is valid
842
+ * @example
843
+ * validator.addMetaSchema(draft7MetaSchema);
844
+ * const isValid = validator.validateSchema({ type: 'string' }); // true
845
+ */
846
+ validateSchema(schema: JSONSchema | boolean): boolean;
847
+ /**
848
+ * Compiles a schema into a validation function.
849
+ * This is the main method for creating validators. It resolves all $ref references,
850
+ * compiles the schema structure, and returns a function that validates data.
851
+ * @param {JSONSchema | boolean} schema - The schema to compile
852
+ * @param {(JSONSchema | boolean)[]} [schemas] - Additional schemas to reference during compilation
853
+ * @returns {(data: any) => boolean | {valid: boolean, errors: ValidationError[]}} A validation function
854
+ * @example
855
+ * const validate = validator.compile({
856
+ * type: 'object',
857
+ * properties: {
858
+ * name: { type: 'string' }
859
+ * }
860
+ * });
861
+ *
862
+ * const valid = validate({ name: 'John' }); // true
863
+ * const invalid = validate({ name: 123 }); // false
864
+ *
865
+ * // With error collection
866
+ * validator = new JarenValidator({ collectErrors: true });
867
+ * const result = validate({ name: 123 });
868
+ * // result = { valid: false, errors: [...] }
869
+ */
870
+ compile(schema: JSONSchema | boolean, schemas?: (JSONSchema | boolean)[]): (data: any) => boolean | {
871
+ valid: boolean;
872
+ errors: ValidationError[];
873
+ };
874
+ }