@langchain/core 0.3.21 → 0.3.23

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 (81) hide show
  1. package/dist/callbacks/base.cjs +9 -1
  2. package/dist/callbacks/base.d.ts +1 -0
  3. package/dist/callbacks/base.js +7 -0
  4. package/dist/callbacks/manager.cjs +18 -0
  5. package/dist/callbacks/manager.js +19 -1
  6. package/dist/context.cjs +13 -123
  7. package/dist/context.d.ts +2 -101
  8. package/dist/context.js +9 -119
  9. package/dist/messages/tool.cjs +14 -1
  10. package/dist/messages/tool.d.ts +13 -1
  11. package/dist/messages/tool.js +12 -0
  12. package/dist/output_parsers/transform.cjs +2 -2
  13. package/dist/output_parsers/transform.js +1 -1
  14. package/dist/runnables/base.cjs +2 -2
  15. package/dist/runnables/base.d.ts +1 -2
  16. package/dist/runnables/base.js +3 -3
  17. package/dist/runnables/config.cjs +17 -1
  18. package/dist/runnables/config.d.ts +1 -0
  19. package/dist/runnables/config.js +15 -0
  20. package/dist/runnables/index.cjs +2 -1
  21. package/dist/runnables/index.d.ts +1 -1
  22. package/dist/runnables/index.js +1 -1
  23. package/dist/runnables/iter.cjs +3 -2
  24. package/dist/runnables/iter.d.ts +1 -1
  25. package/dist/runnables/iter.js +3 -2
  26. package/dist/runnables/types.d.ts +1 -1
  27. package/dist/singletons/async_local_storage/context.cjs +189 -0
  28. package/dist/singletons/async_local_storage/context.d.ts +155 -0
  29. package/dist/singletons/async_local_storage/context.js +182 -0
  30. package/dist/singletons/async_local_storage/globals.cjs +2 -1
  31. package/dist/singletons/async_local_storage/globals.d.ts +2 -1
  32. package/dist/singletons/async_local_storage/globals.js +1 -0
  33. package/dist/singletons/async_local_storage/index.cjs +4 -5
  34. package/dist/singletons/async_local_storage/index.d.ts +0 -1
  35. package/dist/singletons/async_local_storage/index.js +1 -2
  36. package/dist/singletons/callbacks.cjs +6 -4
  37. package/dist/singletons/callbacks.js +6 -4
  38. package/dist/singletons/index.cjs +3 -3
  39. package/dist/singletons/index.d.ts +4 -2
  40. package/dist/singletons/index.js +4 -3
  41. package/dist/tools/index.cjs +14 -12
  42. package/dist/tools/index.d.ts +11 -8
  43. package/dist/tools/index.js +16 -14
  44. package/dist/types/stream.d.ts +1 -0
  45. package/dist/utils/json_schema.cjs +3 -3
  46. package/dist/utils/json_schema.d.ts +1 -1
  47. package/dist/utils/json_schema.js +1 -1
  48. package/dist/utils/stream.cjs +3 -2
  49. package/dist/utils/stream.d.ts +2 -1
  50. package/dist/utils/stream.js +3 -2
  51. package/package.json +2 -1
  52. package/dist/utils/@cfworker/json-schema/index.cjs +0 -17
  53. package/dist/utils/@cfworker/json-schema/index.d.ts +0 -1
  54. package/dist/utils/@cfworker/json-schema/index.js +0 -1
  55. package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  56. package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  57. package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  58. package/dist/utils/@cfworker/json-schema/src/dereference.cjs +0 -171
  59. package/dist/utils/@cfworker/json-schema/src/dereference.d.ts +0 -12
  60. package/dist/utils/@cfworker/json-schema/src/dereference.js +0 -167
  61. package/dist/utils/@cfworker/json-schema/src/format.cjs +0 -139
  62. package/dist/utils/@cfworker/json-schema/src/format.d.ts +0 -2
  63. package/dist/utils/@cfworker/json-schema/src/format.js +0 -136
  64. package/dist/utils/@cfworker/json-schema/src/index.cjs +0 -24
  65. package/dist/utils/@cfworker/json-schema/src/index.d.ts +0 -8
  66. package/dist/utils/@cfworker/json-schema/src/index.js +0 -8
  67. package/dist/utils/@cfworker/json-schema/src/pointer.cjs +0 -11
  68. package/dist/utils/@cfworker/json-schema/src/pointer.d.ts +0 -2
  69. package/dist/utils/@cfworker/json-schema/src/pointer.js +0 -6
  70. package/dist/utils/@cfworker/json-schema/src/types.d.ts +0 -72
  71. package/dist/utils/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  72. package/dist/utils/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  73. package/dist/utils/@cfworker/json-schema/src/ucs2-length.js +0 -24
  74. package/dist/utils/@cfworker/json-schema/src/validate.cjs +0 -808
  75. package/dist/utils/@cfworker/json-schema/src/validate.d.ts +0 -3
  76. package/dist/utils/@cfworker/json-schema/src/validate.js +0 -804
  77. package/dist/utils/@cfworker/json-schema/src/validator.cjs +0 -44
  78. package/dist/utils/@cfworker/json-schema/src/validator.d.ts +0 -10
  79. package/dist/utils/@cfworker/json-schema/src/validator.js +0 -40
  80. /package/dist/{utils/@cfworker/json-schema/src/types.cjs → types/stream.cjs} +0 -0
  81. /package/dist/{utils/@cfworker/json-schema/src/types.js → types/stream.js} +0 -0
@@ -1,808 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validate = void 0;
4
- const deep_compare_strict_js_1 = require("./deep-compare-strict.cjs");
5
- const dereference_js_1 = require("./dereference.cjs");
6
- const format_js_1 = require("./format.cjs");
7
- const pointer_js_1 = require("./pointer.cjs");
8
- const ucs2_length_js_1 = require("./ucs2-length.cjs");
9
- function validate(instance, schema, draft = "2019-09", lookup = (0, dereference_js_1.dereference)(schema), shortCircuit = true, recursiveAnchor = null, instanceLocation = "#", schemaLocation = "#", evaluated = Object.create(null)) {
10
- if (schema === true) {
11
- return { valid: true, errors: [] };
12
- }
13
- if (schema === false) {
14
- return {
15
- valid: false,
16
- errors: [
17
- {
18
- instanceLocation,
19
- keyword: "false",
20
- keywordLocation: instanceLocation,
21
- error: "False boolean schema.",
22
- },
23
- ],
24
- };
25
- }
26
- const rawInstanceType = typeof instance;
27
- let instanceType;
28
- switch (rawInstanceType) {
29
- case "boolean":
30
- case "number":
31
- case "string":
32
- instanceType = rawInstanceType;
33
- break;
34
- case "object":
35
- if (instance === null) {
36
- instanceType = "null";
37
- }
38
- else if (Array.isArray(instance)) {
39
- instanceType = "array";
40
- }
41
- else {
42
- instanceType = "object";
43
- }
44
- break;
45
- default:
46
- // undefined, bigint, function, symbol
47
- throw new Error(`Instances of "${rawInstanceType}" type are not supported.`);
48
- }
49
- const { $ref, $recursiveRef, $recursiveAnchor, type: $type, const: $const, enum: $enum, required: $required, not: $not, anyOf: $anyOf, allOf: $allOf, oneOf: $oneOf, if: $if, then: $then, else: $else, format: $format, properties: $properties, patternProperties: $patternProperties, additionalProperties: $additionalProperties, unevaluatedProperties: $unevaluatedProperties, minProperties: $minProperties, maxProperties: $maxProperties, propertyNames: $propertyNames, dependentRequired: $dependentRequired, dependentSchemas: $dependentSchemas, dependencies: $dependencies, prefixItems: $prefixItems, items: $items, additionalItems: $additionalItems, unevaluatedItems: $unevaluatedItems, contains: $contains, minContains: $minContains, maxContains: $maxContains, minItems: $minItems, maxItems: $maxItems, uniqueItems: $uniqueItems, minimum: $minimum, maximum: $maximum, exclusiveMinimum: $exclusiveMinimum, exclusiveMaximum: $exclusiveMaximum, multipleOf: $multipleOf, minLength: $minLength, maxLength: $maxLength, pattern: $pattern, __absolute_ref__, __absolute_recursive_ref__, } = schema;
50
- const errors = [];
51
- if ($recursiveAnchor === true && recursiveAnchor === null) {
52
- recursiveAnchor = schema;
53
- }
54
- if ($recursiveRef === "#") {
55
- const refSchema = recursiveAnchor === null
56
- ? lookup[__absolute_recursive_ref__]
57
- : recursiveAnchor;
58
- const keywordLocation = `${schemaLocation}/$recursiveRef`;
59
- const result = validate(instance, recursiveAnchor === null ? schema : recursiveAnchor, draft, lookup, shortCircuit, refSchema, instanceLocation, keywordLocation, evaluated);
60
- if (!result.valid) {
61
- errors.push({
62
- instanceLocation,
63
- keyword: "$recursiveRef",
64
- keywordLocation,
65
- error: "A subschema had errors.",
66
- }, ...result.errors);
67
- }
68
- }
69
- if ($ref !== undefined) {
70
- const uri = __absolute_ref__ || $ref;
71
- const refSchema = lookup[uri];
72
- if (refSchema === undefined) {
73
- let message = `Unresolved $ref "${$ref}".`;
74
- if (__absolute_ref__ && __absolute_ref__ !== $ref) {
75
- message += ` Absolute URI "${__absolute_ref__}".`;
76
- }
77
- message += `\nKnown schemas:\n- ${Object.keys(lookup).join("\n- ")}`;
78
- throw new Error(message);
79
- }
80
- const keywordLocation = `${schemaLocation}/$ref`;
81
- const result = validate(instance, refSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation, evaluated);
82
- if (!result.valid) {
83
- errors.push({
84
- instanceLocation,
85
- keyword: "$ref",
86
- keywordLocation,
87
- error: "A subschema had errors.",
88
- }, ...result.errors);
89
- }
90
- if (draft === "4" || draft === "7") {
91
- return { valid: errors.length === 0, errors };
92
- }
93
- }
94
- if (Array.isArray($type)) {
95
- let length = $type.length;
96
- let valid = false;
97
- for (let i = 0; i < length; i++) {
98
- if (instanceType === $type[i] ||
99
- ($type[i] === "integer" &&
100
- instanceType === "number" &&
101
- instance % 1 === 0 &&
102
- instance === instance)) {
103
- valid = true;
104
- break;
105
- }
106
- }
107
- if (!valid) {
108
- errors.push({
109
- instanceLocation,
110
- keyword: "type",
111
- keywordLocation: `${schemaLocation}/type`,
112
- error: `Instance type "${instanceType}" is invalid. Expected "${$type.join('", "')}".`,
113
- });
114
- }
115
- }
116
- else if ($type === "integer") {
117
- if (instanceType !== "number" || instance % 1 || instance !== instance) {
118
- errors.push({
119
- instanceLocation,
120
- keyword: "type",
121
- keywordLocation: `${schemaLocation}/type`,
122
- error: `Instance type "${instanceType}" is invalid. Expected "${$type}".`,
123
- });
124
- }
125
- }
126
- else if ($type !== undefined && instanceType !== $type) {
127
- errors.push({
128
- instanceLocation,
129
- keyword: "type",
130
- keywordLocation: `${schemaLocation}/type`,
131
- error: `Instance type "${instanceType}" is invalid. Expected "${$type}".`,
132
- });
133
- }
134
- if ($const !== undefined) {
135
- if (instanceType === "object" || instanceType === "array") {
136
- if (!(0, deep_compare_strict_js_1.deepCompareStrict)(instance, $const)) {
137
- errors.push({
138
- instanceLocation,
139
- keyword: "const",
140
- keywordLocation: `${schemaLocation}/const`,
141
- error: `Instance does not match ${JSON.stringify($const)}.`,
142
- });
143
- }
144
- }
145
- else if (instance !== $const) {
146
- errors.push({
147
- instanceLocation,
148
- keyword: "const",
149
- keywordLocation: `${schemaLocation}/const`,
150
- error: `Instance does not match ${JSON.stringify($const)}.`,
151
- });
152
- }
153
- }
154
- if ($enum !== undefined) {
155
- if (instanceType === "object" || instanceType === "array") {
156
- if (!$enum.some((value) => (0, deep_compare_strict_js_1.deepCompareStrict)(instance, value))) {
157
- errors.push({
158
- instanceLocation,
159
- keyword: "enum",
160
- keywordLocation: `${schemaLocation}/enum`,
161
- error: `Instance does not match any of ${JSON.stringify($enum)}.`,
162
- });
163
- }
164
- }
165
- else if (!$enum.some((value) => instance === value)) {
166
- errors.push({
167
- instanceLocation,
168
- keyword: "enum",
169
- keywordLocation: `${schemaLocation}/enum`,
170
- error: `Instance does not match any of ${JSON.stringify($enum)}.`,
171
- });
172
- }
173
- }
174
- if ($not !== undefined) {
175
- const keywordLocation = `${schemaLocation}/not`;
176
- const result = validate(instance, $not, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation /*,
177
- evaluated*/);
178
- if (result.valid) {
179
- errors.push({
180
- instanceLocation,
181
- keyword: "not",
182
- keywordLocation,
183
- error: 'Instance matched "not" schema.',
184
- });
185
- }
186
- }
187
- let subEvaluateds = [];
188
- if ($anyOf !== undefined) {
189
- const keywordLocation = `${schemaLocation}/anyOf`;
190
- const errorsLength = errors.length;
191
- let anyValid = false;
192
- for (let i = 0; i < $anyOf.length; i++) {
193
- const subSchema = $anyOf[i];
194
- const subEvaluated = Object.create(evaluated);
195
- const result = validate(instance, subSchema, draft, lookup, shortCircuit, $recursiveAnchor === true ? recursiveAnchor : null, instanceLocation, `${keywordLocation}/${i}`, subEvaluated);
196
- errors.push(...result.errors);
197
- anyValid = anyValid || result.valid;
198
- if (result.valid) {
199
- subEvaluateds.push(subEvaluated);
200
- }
201
- }
202
- if (anyValid) {
203
- errors.length = errorsLength;
204
- }
205
- else {
206
- errors.splice(errorsLength, 0, {
207
- instanceLocation,
208
- keyword: "anyOf",
209
- keywordLocation,
210
- error: "Instance does not match any subschemas.",
211
- });
212
- }
213
- }
214
- if ($allOf !== undefined) {
215
- const keywordLocation = `${schemaLocation}/allOf`;
216
- const errorsLength = errors.length;
217
- let allValid = true;
218
- for (let i = 0; i < $allOf.length; i++) {
219
- const subSchema = $allOf[i];
220
- const subEvaluated = Object.create(evaluated);
221
- const result = validate(instance, subSchema, draft, lookup, shortCircuit, $recursiveAnchor === true ? recursiveAnchor : null, instanceLocation, `${keywordLocation}/${i}`, subEvaluated);
222
- errors.push(...result.errors);
223
- allValid = allValid && result.valid;
224
- if (result.valid) {
225
- subEvaluateds.push(subEvaluated);
226
- }
227
- }
228
- if (allValid) {
229
- errors.length = errorsLength;
230
- }
231
- else {
232
- errors.splice(errorsLength, 0, {
233
- instanceLocation,
234
- keyword: "allOf",
235
- keywordLocation,
236
- error: `Instance does not match every subschema.`,
237
- });
238
- }
239
- }
240
- if ($oneOf !== undefined) {
241
- const keywordLocation = `${schemaLocation}/oneOf`;
242
- const errorsLength = errors.length;
243
- const matches = $oneOf.filter((subSchema, i) => {
244
- const subEvaluated = Object.create(evaluated);
245
- const result = validate(instance, subSchema, draft, lookup, shortCircuit, $recursiveAnchor === true ? recursiveAnchor : null, instanceLocation, `${keywordLocation}/${i}`, subEvaluated);
246
- errors.push(...result.errors);
247
- if (result.valid) {
248
- subEvaluateds.push(subEvaluated);
249
- }
250
- return result.valid;
251
- }).length;
252
- if (matches === 1) {
253
- errors.length = errorsLength;
254
- }
255
- else {
256
- errors.splice(errorsLength, 0, {
257
- instanceLocation,
258
- keyword: "oneOf",
259
- keywordLocation,
260
- error: `Instance does not match exactly one subschema (${matches} matches).`,
261
- });
262
- }
263
- }
264
- if (instanceType === "object" || instanceType === "array") {
265
- Object.assign(evaluated, ...subEvaluateds);
266
- }
267
- if ($if !== undefined) {
268
- const keywordLocation = `${schemaLocation}/if`;
269
- const conditionResult = validate(instance, $if, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation, evaluated).valid;
270
- if (conditionResult) {
271
- if ($then !== undefined) {
272
- const thenResult = validate(instance, $then, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${schemaLocation}/then`, evaluated);
273
- if (!thenResult.valid) {
274
- errors.push({
275
- instanceLocation,
276
- keyword: "if",
277
- keywordLocation,
278
- error: `Instance does not match "then" schema.`,
279
- }, ...thenResult.errors);
280
- }
281
- }
282
- }
283
- else if ($else !== undefined) {
284
- const elseResult = validate(instance, $else, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${schemaLocation}/else`, evaluated);
285
- if (!elseResult.valid) {
286
- errors.push({
287
- instanceLocation,
288
- keyword: "if",
289
- keywordLocation,
290
- error: `Instance does not match "else" schema.`,
291
- }, ...elseResult.errors);
292
- }
293
- }
294
- }
295
- if (instanceType === "object") {
296
- if ($required !== undefined) {
297
- for (const key of $required) {
298
- if (!(key in instance)) {
299
- errors.push({
300
- instanceLocation,
301
- keyword: "required",
302
- keywordLocation: `${schemaLocation}/required`,
303
- error: `Instance does not have required property "${key}".`,
304
- });
305
- }
306
- }
307
- }
308
- const keys = Object.keys(instance);
309
- if ($minProperties !== undefined && keys.length < $minProperties) {
310
- errors.push({
311
- instanceLocation,
312
- keyword: "minProperties",
313
- keywordLocation: `${schemaLocation}/minProperties`,
314
- error: `Instance does not have at least ${$minProperties} properties.`,
315
- });
316
- }
317
- if ($maxProperties !== undefined && keys.length > $maxProperties) {
318
- errors.push({
319
- instanceLocation,
320
- keyword: "maxProperties",
321
- keywordLocation: `${schemaLocation}/maxProperties`,
322
- error: `Instance does not have at least ${$maxProperties} properties.`,
323
- });
324
- }
325
- if ($propertyNames !== undefined) {
326
- const keywordLocation = `${schemaLocation}/propertyNames`;
327
- for (const key in instance) {
328
- const subInstancePointer = `${instanceLocation}/${(0, pointer_js_1.encodePointer)(key)}`;
329
- const result = validate(key, $propertyNames, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, keywordLocation);
330
- if (!result.valid) {
331
- errors.push({
332
- instanceLocation,
333
- keyword: "propertyNames",
334
- keywordLocation,
335
- error: `Property name "${key}" does not match schema.`,
336
- }, ...result.errors);
337
- }
338
- }
339
- }
340
- if ($dependentRequired !== undefined) {
341
- const keywordLocation = `${schemaLocation}/dependantRequired`;
342
- for (const key in $dependentRequired) {
343
- if (key in instance) {
344
- const required = $dependentRequired[key];
345
- for (const dependantKey of required) {
346
- if (!(dependantKey in instance)) {
347
- errors.push({
348
- instanceLocation,
349
- keyword: "dependentRequired",
350
- keywordLocation,
351
- error: `Instance has "${key}" but does not have "${dependantKey}".`,
352
- });
353
- }
354
- }
355
- }
356
- }
357
- }
358
- if ($dependentSchemas !== undefined) {
359
- for (const key in $dependentSchemas) {
360
- const keywordLocation = `${schemaLocation}/dependentSchemas`;
361
- if (key in instance) {
362
- const result = validate(instance, $dependentSchemas[key], draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${(0, pointer_js_1.encodePointer)(key)}`, evaluated);
363
- if (!result.valid) {
364
- errors.push({
365
- instanceLocation,
366
- keyword: "dependentSchemas",
367
- keywordLocation,
368
- error: `Instance has "${key}" but does not match dependant schema.`,
369
- }, ...result.errors);
370
- }
371
- }
372
- }
373
- }
374
- if ($dependencies !== undefined) {
375
- const keywordLocation = `${schemaLocation}/dependencies`;
376
- for (const key in $dependencies) {
377
- if (key in instance) {
378
- const propsOrSchema = $dependencies[key];
379
- if (Array.isArray(propsOrSchema)) {
380
- for (const dependantKey of propsOrSchema) {
381
- if (!(dependantKey in instance)) {
382
- errors.push({
383
- instanceLocation,
384
- keyword: "dependencies",
385
- keywordLocation,
386
- error: `Instance has "${key}" but does not have "${dependantKey}".`,
387
- });
388
- }
389
- }
390
- }
391
- else {
392
- const result = validate(instance, propsOrSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${(0, pointer_js_1.encodePointer)(key)}`);
393
- if (!result.valid) {
394
- errors.push({
395
- instanceLocation,
396
- keyword: "dependencies",
397
- keywordLocation,
398
- error: `Instance has "${key}" but does not match dependant schema.`,
399
- }, ...result.errors);
400
- }
401
- }
402
- }
403
- }
404
- }
405
- const thisEvaluated = Object.create(null);
406
- let stop = false;
407
- if ($properties !== undefined) {
408
- const keywordLocation = `${schemaLocation}/properties`;
409
- for (const key in $properties) {
410
- if (!(key in instance)) {
411
- continue;
412
- }
413
- const subInstancePointer = `${instanceLocation}/${(0, pointer_js_1.encodePointer)(key)}`;
414
- const result = validate(instance[key], $properties[key], draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, `${keywordLocation}/${(0, pointer_js_1.encodePointer)(key)}`);
415
- if (result.valid) {
416
- evaluated[key] = thisEvaluated[key] = true;
417
- }
418
- else {
419
- stop = shortCircuit;
420
- errors.push({
421
- instanceLocation,
422
- keyword: "properties",
423
- keywordLocation,
424
- error: `Property "${key}" does not match schema.`,
425
- }, ...result.errors);
426
- if (stop)
427
- break;
428
- }
429
- }
430
- }
431
- if (!stop && $patternProperties !== undefined) {
432
- const keywordLocation = `${schemaLocation}/patternProperties`;
433
- for (const pattern in $patternProperties) {
434
- const regex = new RegExp(pattern);
435
- const subSchema = $patternProperties[pattern];
436
- for (const key in instance) {
437
- if (!regex.test(key)) {
438
- continue;
439
- }
440
- const subInstancePointer = `${instanceLocation}/${(0, pointer_js_1.encodePointer)(key)}`;
441
- const result = validate(instance[key], subSchema, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, `${keywordLocation}/${(0, pointer_js_1.encodePointer)(pattern)}`);
442
- if (result.valid) {
443
- evaluated[key] = thisEvaluated[key] = true;
444
- }
445
- else {
446
- stop = shortCircuit;
447
- errors.push({
448
- instanceLocation,
449
- keyword: "patternProperties",
450
- keywordLocation,
451
- error: `Property "${key}" matches pattern "${pattern}" but does not match associated schema.`,
452
- }, ...result.errors);
453
- }
454
- }
455
- }
456
- }
457
- if (!stop && $additionalProperties !== undefined) {
458
- const keywordLocation = `${schemaLocation}/additionalProperties`;
459
- for (const key in instance) {
460
- if (thisEvaluated[key]) {
461
- continue;
462
- }
463
- const subInstancePointer = `${instanceLocation}/${(0, pointer_js_1.encodePointer)(key)}`;
464
- const result = validate(instance[key], $additionalProperties, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, keywordLocation);
465
- if (result.valid) {
466
- evaluated[key] = true;
467
- }
468
- else {
469
- stop = shortCircuit;
470
- errors.push({
471
- instanceLocation,
472
- keyword: "additionalProperties",
473
- keywordLocation,
474
- error: `Property "${key}" does not match additional properties schema.`,
475
- }, ...result.errors);
476
- }
477
- }
478
- }
479
- else if (!stop && $unevaluatedProperties !== undefined) {
480
- const keywordLocation = `${schemaLocation}/unevaluatedProperties`;
481
- for (const key in instance) {
482
- if (!evaluated[key]) {
483
- const subInstancePointer = `${instanceLocation}/${(0, pointer_js_1.encodePointer)(key)}`;
484
- const result = validate(instance[key], $unevaluatedProperties, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, keywordLocation);
485
- if (result.valid) {
486
- evaluated[key] = true;
487
- }
488
- else {
489
- errors.push({
490
- instanceLocation,
491
- keyword: "unevaluatedProperties",
492
- keywordLocation,
493
- error: `Property "${key}" does not match unevaluated properties schema.`,
494
- }, ...result.errors);
495
- }
496
- }
497
- }
498
- }
499
- }
500
- else if (instanceType === "array") {
501
- if ($maxItems !== undefined && instance.length > $maxItems) {
502
- errors.push({
503
- instanceLocation,
504
- keyword: "maxItems",
505
- keywordLocation: `${schemaLocation}/maxItems`,
506
- error: `Array has too many items (${instance.length} > ${$maxItems}).`,
507
- });
508
- }
509
- if ($minItems !== undefined && instance.length < $minItems) {
510
- errors.push({
511
- instanceLocation,
512
- keyword: "minItems",
513
- keywordLocation: `${schemaLocation}/minItems`,
514
- error: `Array has too few items (${instance.length} < ${$minItems}).`,
515
- });
516
- }
517
- const length = instance.length;
518
- let i = 0;
519
- let stop = false;
520
- if ($prefixItems !== undefined) {
521
- const keywordLocation = `${schemaLocation}/prefixItems`;
522
- const length2 = Math.min($prefixItems.length, length);
523
- for (; i < length2; i++) {
524
- const result = validate(instance[i], $prefixItems[i], draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, `${keywordLocation}/${i}`);
525
- evaluated[i] = true;
526
- if (!result.valid) {
527
- stop = shortCircuit;
528
- errors.push({
529
- instanceLocation,
530
- keyword: "prefixItems",
531
- keywordLocation,
532
- error: `Items did not match schema.`,
533
- }, ...result.errors);
534
- if (stop)
535
- break;
536
- }
537
- }
538
- }
539
- if ($items !== undefined) {
540
- const keywordLocation = `${schemaLocation}/items`;
541
- if (Array.isArray($items)) {
542
- const length2 = Math.min($items.length, length);
543
- for (; i < length2; i++) {
544
- const result = validate(instance[i], $items[i], draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, `${keywordLocation}/${i}`);
545
- evaluated[i] = true;
546
- if (!result.valid) {
547
- stop = shortCircuit;
548
- errors.push({
549
- instanceLocation,
550
- keyword: "items",
551
- keywordLocation,
552
- error: `Items did not match schema.`,
553
- }, ...result.errors);
554
- if (stop)
555
- break;
556
- }
557
- }
558
- }
559
- else {
560
- for (; i < length; i++) {
561
- const result = validate(instance[i], $items, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation);
562
- evaluated[i] = true;
563
- if (!result.valid) {
564
- stop = shortCircuit;
565
- errors.push({
566
- instanceLocation,
567
- keyword: "items",
568
- keywordLocation,
569
- error: `Items did not match schema.`,
570
- }, ...result.errors);
571
- if (stop)
572
- break;
573
- }
574
- }
575
- }
576
- if (!stop && $additionalItems !== undefined) {
577
- const keywordLocation = `${schemaLocation}/additionalItems`;
578
- for (; i < length; i++) {
579
- const result = validate(instance[i], $additionalItems, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation);
580
- evaluated[i] = true;
581
- if (!result.valid) {
582
- stop = shortCircuit;
583
- errors.push({
584
- instanceLocation,
585
- keyword: "additionalItems",
586
- keywordLocation,
587
- error: `Items did not match additional items schema.`,
588
- }, ...result.errors);
589
- }
590
- }
591
- }
592
- }
593
- if ($contains !== undefined) {
594
- if (length === 0 && $minContains === undefined) {
595
- errors.push({
596
- instanceLocation,
597
- keyword: "contains",
598
- keywordLocation: `${schemaLocation}/contains`,
599
- error: `Array is empty. It must contain at least one item matching the schema.`,
600
- });
601
- }
602
- else if ($minContains !== undefined && length < $minContains) {
603
- errors.push({
604
- instanceLocation,
605
- keyword: "minContains",
606
- keywordLocation: `${schemaLocation}/minContains`,
607
- error: `Array has less items (${length}) than minContains (${$minContains}).`,
608
- });
609
- }
610
- else {
611
- const keywordLocation = `${schemaLocation}/contains`;
612
- const errorsLength = errors.length;
613
- let contained = 0;
614
- for (let j = 0; j < length; j++) {
615
- const result = validate(instance[j], $contains, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${j}`, keywordLocation);
616
- if (result.valid) {
617
- evaluated[j] = true;
618
- contained++;
619
- }
620
- else {
621
- errors.push(...result.errors);
622
- }
623
- }
624
- if (contained >= ($minContains || 0)) {
625
- errors.length = errorsLength;
626
- }
627
- if ($minContains === undefined &&
628
- $maxContains === undefined &&
629
- contained === 0) {
630
- errors.splice(errorsLength, 0, {
631
- instanceLocation,
632
- keyword: "contains",
633
- keywordLocation,
634
- error: `Array does not contain item matching schema.`,
635
- });
636
- }
637
- else if ($minContains !== undefined && contained < $minContains) {
638
- errors.push({
639
- instanceLocation,
640
- keyword: "minContains",
641
- keywordLocation: `${schemaLocation}/minContains`,
642
- error: `Array must contain at least ${$minContains} items matching schema. Only ${contained} items were found.`,
643
- });
644
- }
645
- else if ($maxContains !== undefined && contained > $maxContains) {
646
- errors.push({
647
- instanceLocation,
648
- keyword: "maxContains",
649
- keywordLocation: `${schemaLocation}/maxContains`,
650
- error: `Array may contain at most ${$maxContains} items matching schema. ${contained} items were found.`,
651
- });
652
- }
653
- }
654
- }
655
- if (!stop && $unevaluatedItems !== undefined) {
656
- const keywordLocation = `${schemaLocation}/unevaluatedItems`;
657
- for (i; i < length; i++) {
658
- if (evaluated[i]) {
659
- continue;
660
- }
661
- const result = validate(instance[i], $unevaluatedItems, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation);
662
- evaluated[i] = true;
663
- if (!result.valid) {
664
- errors.push({
665
- instanceLocation,
666
- keyword: "unevaluatedItems",
667
- keywordLocation,
668
- error: `Items did not match unevaluated items schema.`,
669
- }, ...result.errors);
670
- }
671
- }
672
- }
673
- if ($uniqueItems) {
674
- for (let j = 0; j < length; j++) {
675
- const a = instance[j];
676
- const ao = typeof a === "object" && a !== null;
677
- for (let k = 0; k < length; k++) {
678
- if (j === k) {
679
- continue;
680
- }
681
- const b = instance[k];
682
- const bo = typeof b === "object" && b !== null;
683
- if (a === b || (ao && bo && (0, deep_compare_strict_js_1.deepCompareStrict)(a, b))) {
684
- errors.push({
685
- instanceLocation,
686
- keyword: "uniqueItems",
687
- keywordLocation: `${schemaLocation}/uniqueItems`,
688
- error: `Duplicate items at indexes ${j} and ${k}.`,
689
- });
690
- j = Number.MAX_SAFE_INTEGER;
691
- k = Number.MAX_SAFE_INTEGER;
692
- }
693
- }
694
- }
695
- }
696
- }
697
- else if (instanceType === "number") {
698
- if (draft === "4") {
699
- if ($minimum !== undefined &&
700
- (($exclusiveMinimum === true && instance <= $minimum) ||
701
- instance < $minimum)) {
702
- errors.push({
703
- instanceLocation,
704
- keyword: "minimum",
705
- keywordLocation: `${schemaLocation}/minimum`,
706
- error: `${instance} is less than ${$exclusiveMinimum ? "or equal to " : ""} ${$minimum}.`,
707
- });
708
- }
709
- if ($maximum !== undefined &&
710
- (($exclusiveMaximum === true && instance >= $maximum) ||
711
- instance > $maximum)) {
712
- errors.push({
713
- instanceLocation,
714
- keyword: "maximum",
715
- keywordLocation: `${schemaLocation}/maximum`,
716
- error: `${instance} is greater than ${$exclusiveMaximum ? "or equal to " : ""} ${$maximum}.`,
717
- });
718
- }
719
- }
720
- else {
721
- if ($minimum !== undefined && instance < $minimum) {
722
- errors.push({
723
- instanceLocation,
724
- keyword: "minimum",
725
- keywordLocation: `${schemaLocation}/minimum`,
726
- error: `${instance} is less than ${$minimum}.`,
727
- });
728
- }
729
- if ($maximum !== undefined && instance > $maximum) {
730
- errors.push({
731
- instanceLocation,
732
- keyword: "maximum",
733
- keywordLocation: `${schemaLocation}/maximum`,
734
- error: `${instance} is greater than ${$maximum}.`,
735
- });
736
- }
737
- if ($exclusiveMinimum !== undefined && instance <= $exclusiveMinimum) {
738
- errors.push({
739
- instanceLocation,
740
- keyword: "exclusiveMinimum",
741
- keywordLocation: `${schemaLocation}/exclusiveMinimum`,
742
- error: `${instance} is less than ${$exclusiveMinimum}.`,
743
- });
744
- }
745
- if ($exclusiveMaximum !== undefined && instance >= $exclusiveMaximum) {
746
- errors.push({
747
- instanceLocation,
748
- keyword: "exclusiveMaximum",
749
- keywordLocation: `${schemaLocation}/exclusiveMaximum`,
750
- error: `${instance} is greater than or equal to ${$exclusiveMaximum}.`,
751
- });
752
- }
753
- }
754
- if ($multipleOf !== undefined) {
755
- const remainder = instance % $multipleOf;
756
- if (Math.abs(0 - remainder) >= 1.1920929e-7 &&
757
- Math.abs($multipleOf - remainder) >= 1.1920929e-7) {
758
- errors.push({
759
- instanceLocation,
760
- keyword: "multipleOf",
761
- keywordLocation: `${schemaLocation}/multipleOf`,
762
- error: `${instance} is not a multiple of ${$multipleOf}.`,
763
- });
764
- }
765
- }
766
- }
767
- else if (instanceType === "string") {
768
- const length = $minLength === undefined && $maxLength === undefined
769
- ? 0
770
- : (0, ucs2_length_js_1.ucs2length)(instance);
771
- if ($minLength !== undefined && length < $minLength) {
772
- errors.push({
773
- instanceLocation,
774
- keyword: "minLength",
775
- keywordLocation: `${schemaLocation}/minLength`,
776
- error: `String is too short (${length} < ${$minLength}).`,
777
- });
778
- }
779
- if ($maxLength !== undefined && length > $maxLength) {
780
- errors.push({
781
- instanceLocation,
782
- keyword: "maxLength",
783
- keywordLocation: `${schemaLocation}/maxLength`,
784
- error: `String is too long (${length} > ${$maxLength}).`,
785
- });
786
- }
787
- if ($pattern !== undefined && !new RegExp($pattern).test(instance)) {
788
- errors.push({
789
- instanceLocation,
790
- keyword: "pattern",
791
- keywordLocation: `${schemaLocation}/pattern`,
792
- error: `String does not match pattern.`,
793
- });
794
- }
795
- if ($format !== undefined &&
796
- format_js_1.fastFormat[$format] &&
797
- !format_js_1.fastFormat[$format](instance)) {
798
- errors.push({
799
- instanceLocation,
800
- keyword: "format",
801
- keywordLocation: `${schemaLocation}/format`,
802
- error: `String does not match format "${$format}".`,
803
- });
804
- }
805
- }
806
- return { valid: errors.length === 0, errors };
807
- }
808
- exports.validate = validate;