langchain 0.0.195 → 0.0.197-rc.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.
Files changed (236) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/openai/index.cjs +6 -2
  3. package/dist/agents/openai/index.js +6 -2
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +5 -70
  6. package/dist/base_language/count_tokens.d.ts +1 -10
  7. package/dist/base_language/count_tokens.js +1 -65
  8. package/dist/base_language/index.cjs +6 -196
  9. package/dist/base_language/index.d.ts +1 -111
  10. package/dist/base_language/index.js +1 -191
  11. package/dist/cache/base.cjs +15 -37
  12. package/dist/cache/base.d.ts +1 -20
  13. package/dist/cache/base.js +1 -33
  14. package/dist/cache/index.cjs +2 -46
  15. package/dist/cache/index.d.ts +1 -29
  16. package/dist/cache/index.js +1 -45
  17. package/dist/callbacks/base.cjs +3 -139
  18. package/dist/callbacks/base.d.ts +1 -266
  19. package/dist/callbacks/base.js +1 -126
  20. package/dist/callbacks/handlers/console.cjs +14 -221
  21. package/dist/callbacks/handlers/console.d.ts +1 -117
  22. package/dist/callbacks/handlers/console.js +1 -217
  23. package/dist/callbacks/handlers/initialize.cjs +15 -30
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  25. package/dist/callbacks/handlers/initialize.js +1 -27
  26. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  28. package/dist/callbacks/handlers/log_stream.js +1 -289
  29. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  31. package/dist/callbacks/handlers/run_collector.js +1 -46
  32. package/dist/callbacks/handlers/tracer.cjs +15 -375
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  34. package/dist/callbacks/handlers/tracer.js +1 -373
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  41. package/dist/callbacks/manager.cjs +15 -676
  42. package/dist/callbacks/manager.d.ts +1 -180
  43. package/dist/callbacks/manager.js +1 -666
  44. package/dist/callbacks/promises.cjs +14 -42
  45. package/dist/callbacks/promises.d.ts +1 -11
  46. package/dist/callbacks/promises.js +1 -37
  47. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  48. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  49. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  50. package/dist/chains/openai_functions/structured_output.js +1 -1
  51. package/dist/chat_models/anthropic.cjs +15 -348
  52. package/dist/chat_models/anthropic.d.ts +1 -156
  53. package/dist/chat_models/anthropic.js +1 -346
  54. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  55. package/dist/chat_models/base.cjs +15 -296
  56. package/dist/chat_models/base.d.ts +1 -122
  57. package/dist/chat_models/base.js +1 -292
  58. package/dist/chat_models/bedrock/web.cjs +21 -1
  59. package/dist/chat_models/bedrock/web.d.ts +2 -2
  60. package/dist/chat_models/bedrock/web.js +21 -1
  61. package/dist/chat_models/fireworks.d.ts +1 -1
  62. package/dist/document.cjs +2 -24
  63. package/dist/document.d.ts +1 -12
  64. package/dist/document.js +1 -23
  65. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  66. package/dist/document_loaders/web/github.cjs +105 -0
  67. package/dist/document_loaders/web/github.d.ts +26 -0
  68. package/dist/document_loaders/web/github.js +105 -0
  69. package/dist/document_loaders/web/s3.d.ts +1 -1
  70. package/dist/embeddings/base.cjs +15 -22
  71. package/dist/embeddings/base.d.ts +1 -33
  72. package/dist/embeddings/base.js +1 -20
  73. package/dist/embeddings/cache_backed.cjs +2 -2
  74. package/dist/embeddings/cache_backed.js +1 -1
  75. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  76. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  77. package/dist/evaluation/qa/prompt.d.ts +2 -2
  78. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  79. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  80. package/dist/llms/base.cjs +15 -278
  81. package/dist/llms/base.d.ts +1 -115
  82. package/dist/llms/base.js +1 -275
  83. package/dist/llms/bedrock/web.cjs +21 -1
  84. package/dist/llms/bedrock/web.d.ts +2 -2
  85. package/dist/llms/bedrock/web.js +21 -1
  86. package/dist/llms/fireworks.d.ts +1 -1
  87. package/dist/load/import_map.cjs +2 -1
  88. package/dist/load/import_map.d.ts +1 -0
  89. package/dist/load/import_map.js +1 -0
  90. package/dist/load/index.cjs +7 -148
  91. package/dist/load/index.js +7 -148
  92. package/dist/load/map_keys.cjs +0 -24
  93. package/dist/load/map_keys.d.ts +0 -6
  94. package/dist/load/map_keys.js +1 -17
  95. package/dist/load/serializable.cjs +15 -178
  96. package/dist/load/serializable.d.ts +1 -66
  97. package/dist/load/serializable.js +1 -175
  98. package/dist/memory/base.cjs +17 -92
  99. package/dist/memory/base.d.ts +2 -68
  100. package/dist/memory/base.js +2 -87
  101. package/dist/output_parsers/list.cjs +4 -122
  102. package/dist/output_parsers/list.d.ts +1 -57
  103. package/dist/output_parsers/list.js +1 -119
  104. package/dist/output_parsers/openai_functions.cjs +2 -2
  105. package/dist/output_parsers/openai_functions.d.ts +1 -1
  106. package/dist/output_parsers/openai_functions.js +1 -1
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.d.ts +1 -1
  109. package/dist/prompts/base.cjs +8 -183
  110. package/dist/prompts/base.d.ts +3 -132
  111. package/dist/prompts/base.js +3 -178
  112. package/dist/prompts/chat.cjs +13 -477
  113. package/dist/prompts/chat.d.ts +2 -219
  114. package/dist/prompts/chat.js +2 -466
  115. package/dist/prompts/few_shot.cjs +3 -352
  116. package/dist/prompts/few_shot.d.ts +1 -192
  117. package/dist/prompts/few_shot.js +1 -350
  118. package/dist/prompts/index.cjs +3 -2
  119. package/dist/prompts/index.d.ts +2 -1
  120. package/dist/prompts/index.js +2 -1
  121. package/dist/prompts/pipeline.cjs +2 -141
  122. package/dist/prompts/pipeline.d.ts +1 -98
  123. package/dist/prompts/pipeline.js +1 -140
  124. package/dist/prompts/prompt.cjs +2 -145
  125. package/dist/prompts/prompt.d.ts +1 -92
  126. package/dist/prompts/prompt.js +1 -144
  127. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +2 -147
  128. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  129. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  130. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  131. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  132. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  133. package/dist/prompts/selectors/conditional.cjs +5 -73
  134. package/dist/prompts/selectors/conditional.d.ts +1 -63
  135. package/dist/prompts/selectors/conditional.js +1 -69
  136. package/dist/prompts/serde.d.ts +1 -43
  137. package/dist/prompts/template.cjs +8 -88
  138. package/dist/prompts/template.d.ts +1 -36
  139. package/dist/prompts/template.js +1 -83
  140. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  141. package/dist/runnables/index.d.ts +1 -0
  142. package/dist/runnables/index.js +1 -0
  143. package/dist/schema/document.cjs +3 -34
  144. package/dist/schema/document.d.ts +2 -29
  145. package/dist/schema/document.js +2 -32
  146. package/dist/schema/index.cjs +37 -612
  147. package/dist/schema/index.d.ts +11 -311
  148. package/dist/schema/index.js +8 -583
  149. package/dist/schema/output_parser.cjs +15 -309
  150. package/dist/schema/output_parser.d.ts +1 -173
  151. package/dist/schema/output_parser.js +1 -301
  152. package/dist/schema/retriever.cjs +15 -77
  153. package/dist/schema/retriever.d.ts +1 -43
  154. package/dist/schema/retriever.js +1 -75
  155. package/dist/schema/runnable/base.cjs +10 -1072
  156. package/dist/schema/runnable/base.d.ts +1 -356
  157. package/dist/schema/runnable/base.js +1 -1060
  158. package/dist/schema/runnable/branch.cjs +2 -131
  159. package/dist/schema/runnable/branch.d.ts +1 -94
  160. package/dist/schema/runnable/branch.js +1 -130
  161. package/dist/schema/runnable/config.cjs +0 -6
  162. package/dist/schema/runnable/config.d.ts +1 -3
  163. package/dist/schema/runnable/config.js +1 -4
  164. package/dist/schema/runnable/index.cjs +15 -16
  165. package/dist/schema/runnable/index.d.ts +1 -5
  166. package/dist/schema/runnable/index.js +1 -4
  167. package/dist/schema/runnable/passthrough.cjs +3 -113
  168. package/dist/schema/runnable/passthrough.d.ts +1 -72
  169. package/dist/schema/runnable/passthrough.js +1 -111
  170. package/dist/schema/runnable/router.cjs +2 -71
  171. package/dist/schema/runnable/router.d.ts +1 -29
  172. package/dist/schema/runnable/router.js +1 -70
  173. package/dist/schema/storage.cjs +15 -8
  174. package/dist/schema/storage.d.ts +1 -57
  175. package/dist/schema/storage.js +1 -6
  176. package/dist/tools/bingserpapi.d.ts +1 -1
  177. package/dist/tools/searchapi.d.ts +1 -1
  178. package/dist/tools/serpapi.d.ts +1 -1
  179. package/dist/tools/serper.d.ts +1 -1
  180. package/dist/util/async_caller.cjs +14 -128
  181. package/dist/util/async_caller.d.ts +1 -45
  182. package/dist/util/async_caller.js +1 -124
  183. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  184. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  185. package/dist/vectorstores/momento_vector_index.js +40 -1
  186. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  187. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  188. package/dist/vectorstores/mongodb_atlas.js +22 -2
  189. package/package.json +18 -11
  190. package/runnables.cjs +1 -0
  191. package/runnables.d.ts +1 -0
  192. package/runnables.js +1 -0
  193. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  194. package/dist/util/@cfworker/json-schema/index.js +0 -1
  195. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  196. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  197. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  198. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  199. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  200. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  201. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  202. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  203. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  204. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  205. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  206. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  207. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  208. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  209. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  210. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  211. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  212. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  213. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  214. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  215. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  216. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  217. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  218. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  219. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  220. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  221. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  222. package/dist/util/fast-json-patch/index.cjs +0 -49
  223. package/dist/util/fast-json-patch/index.d.ts +0 -22
  224. package/dist/util/fast-json-patch/index.js +0 -16
  225. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  226. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  227. package/dist/util/fast-json-patch/src/core.js +0 -459
  228. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  229. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  230. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  231. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  232. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  233. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  234. package/dist/util/js-sha1/hash.cjs +0 -358
  235. package/dist/util/js-sha1/hash.d.ts +0 -1
  236. package/dist/util/js-sha1/hash.js +0 -355
@@ -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;