@apify/input_schema 2.1.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,19 +1,590 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
15
10
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./intl"), exports);
18
- __exportStar(require("./input_schema"), exports);
19
- __exportStar(require("./utilities"), exports);
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ inputSchema: () => schema_default,
25
+ m: () => m,
26
+ makeInputJsFieldsReadable: () => makeInputJsFieldsReadable,
27
+ parseAjvError: () => parseAjvError,
28
+ validateInputSchema: () => validateInputSchema,
29
+ validateInputUsingValidator: () => validateInputUsingValidator
30
+ });
31
+ module.exports = __toCommonJS(src_exports);
32
+
33
+ // src/intl.ts
34
+ var intlStrings = {
35
+ "inputSchema.validation.generic": "Field {rootName}.{fieldKey} {message}",
36
+ "inputSchema.validation.required": "Field {rootName}.{fieldKey} is required",
37
+ "inputSchema.validation.proxyRequired": "Field {rootName}.{fieldKey} is required. Please provide custom proxy URLs or use Apify Proxy.",
38
+ "inputSchema.validation.requestListSourcesInvalid": "Items in {rootName}.{fieldKey} at positions [{invalidIndexes}] do not contain valid URLs",
39
+ "inputSchema.validation.arrayKeysInvalid": 'Keys in {rootName}.{fieldKey} at positions [{invalidIndexes}] must match regular expression "{pattern}"',
40
+ "inputSchema.validation.arrayValuesInvalid": 'Values in {rootName}.{fieldKey} at positions [{invalidIndexes}] must match regular expression "{pattern}"',
41
+ "inputSchema.validation.objectKeysInvalid": 'Keys [{invalidKeys}] in {rootName}.{fieldKey} must match regular expression "{pattern}',
42
+ "inputSchema.validation.objectValuesInvalid": 'Keys [{invalidKeys}] in {rootName}.{fieldKey} must have string value which matches regular expression "{pattern}"',
43
+ "inputSchema.validation.additionalProperty": "Property {rootName}.{fieldKey} is not allowed.",
44
+ "inputSchema.validation.proxyGroupsNotAvailable": "You currently do not have access to proxy groups: {groups}",
45
+ "inputSchema.validation.customProxyInvalid": 'Proxy URL "{invalidUrl}" has invalid format, it must be http://[username[:password]]@hostname:port.',
46
+ "inputSchema.validation.apifyProxyCountryInvalid": 'Country code "{invalidCountry}" is invalid. Only ISO 3166-1 alpha-2 country codes are supported.',
47
+ "inputSchema.validation.apifyProxyCountryWithoutApifyProxyForbidden": "The country for Apify Proxy can be specified only when using Apify Proxy.",
48
+ "inputSchema.validation.noAvailableAutoProxy": "Currently you do not have access to any proxy group usable in automatic mode.",
49
+ "inputSchema.validation.noMatchingDefinition": "Field schema.properties.{fieldKey} is not matching any input schema type definition. Please make sure that it's type is valid."
50
+ };
51
+ function m(stringId, variables) {
52
+ let text = intlStrings[stringId];
53
+ if (!text)
54
+ return stringId;
55
+ if (variables) {
56
+ Object.keys(variables).forEach((variableName) => {
57
+ text = text.split(`{${variableName}}`).join(variables[variableName]);
58
+ });
59
+ }
60
+ return text;
61
+ }
62
+ __name(m, "m");
63
+
64
+ // src/schema.json
65
+ var schema_default = {
66
+ title: "JSON schema of Apify Actor INPUT_SCHEMA.json",
67
+ type: "object",
68
+ properties: {
69
+ $schema: {
70
+ type: "string"
71
+ },
72
+ title: {
73
+ type: "string"
74
+ },
75
+ schemaVersion: {
76
+ type: "integer",
77
+ minimum: 1,
78
+ maximum: 1
79
+ },
80
+ description: {
81
+ type: "string"
82
+ },
83
+ type: {
84
+ enum: ["object"]
85
+ },
86
+ required: {
87
+ type: "array",
88
+ minItems: 0,
89
+ items: { type: "string" },
90
+ uniqueItems: true
91
+ },
92
+ additionalProperties: {
93
+ type: "boolean"
94
+ },
95
+ properties: {
96
+ type: "object",
97
+ patternProperties: {
98
+ "^": {
99
+ oneOf: [
100
+ { $ref: "#/definitions/stringProperty" },
101
+ { $ref: "#/definitions/stringEnumProperty" },
102
+ { $ref: "#/definitions/arrayProperty" },
103
+ { $ref: "#/definitions/objectProperty" },
104
+ { $ref: "#/definitions/integerProperty" },
105
+ { $ref: "#/definitions/booleanProperty" },
106
+ { $ref: "#/definitions/anyProperty" }
107
+ ]
108
+ }
109
+ }
110
+ }
111
+ },
112
+ additionalProperties: false,
113
+ required: ["title", "type", "properties", "schemaVersion"],
114
+ definitions: {
115
+ stringEnumProperty: {
116
+ title: "Enum property",
117
+ type: "object",
118
+ additionalProperties: false,
119
+ properties: {
120
+ type: { enum: ["string"] },
121
+ editor: { enum: ["select"] },
122
+ title: { type: "string" },
123
+ description: { type: "string" },
124
+ default: { type: "string" },
125
+ prefill: { type: "string" },
126
+ example: { type: "string" },
127
+ nullable: { type: "boolean" },
128
+ sectionCaption: { type: "string" },
129
+ sectionDescription: { type: "string" },
130
+ enum: {
131
+ type: "array",
132
+ items: { type: "string" }
133
+ },
134
+ enumTitles: {
135
+ type: "array",
136
+ items: { type: "string" }
137
+ }
138
+ },
139
+ required: ["type", "title", "description", "enum"]
140
+ },
141
+ stringProperty: {
142
+ title: "String property",
143
+ type: "object",
144
+ additionalProperties: false,
145
+ properties: {
146
+ type: { enum: ["string"] },
147
+ title: { type: "string" },
148
+ description: { type: "string" },
149
+ default: { type: "string" },
150
+ prefill: { type: "string" },
151
+ example: { type: "string" },
152
+ pattern: { type: "string" },
153
+ nullable: { type: "boolean" },
154
+ minLength: { type: "integer" },
155
+ maxLength: { type: "integer" },
156
+ editor: { enum: ["javascript", "python", "textfield", "textarea", "hidden"] },
157
+ sectionCaption: { type: "string" },
158
+ sectionDescription: { type: "string" }
159
+ },
160
+ required: ["type", "title", "description", "editor"]
161
+ },
162
+ arrayProperty: {
163
+ title: "Array property",
164
+ type: "object",
165
+ additionalProperties: false,
166
+ properties: {
167
+ type: { enum: ["array"] },
168
+ title: { type: "string" },
169
+ description: { type: "string" },
170
+ default: { type: "array" },
171
+ prefill: { type: "array" },
172
+ example: { type: "array" },
173
+ placeholderKey: { type: "string" },
174
+ placeholderValue: { type: "string" },
175
+ patternKey: { type: "string" },
176
+ patternValue: { type: "string" },
177
+ nullable: { type: "boolean" },
178
+ minItems: { type: "integer" },
179
+ maxItems: { type: "integer" },
180
+ uniqueItems: { type: "boolean" },
181
+ editor: { enum: ["json", "requestListSources", "pseudoUrls", "keyValue", "stringList", "hidden"] },
182
+ sectionCaption: { type: "string" },
183
+ sectionDescription: { type: "string" }
184
+ },
185
+ required: ["type", "title", "description", "editor"]
186
+ },
187
+ objectProperty: {
188
+ title: "Object property",
189
+ type: "object",
190
+ additionalProperties: false,
191
+ properties: {
192
+ type: { enum: ["object"] },
193
+ title: { type: "string" },
194
+ description: { type: "string" },
195
+ default: { type: "object" },
196
+ prefill: { type: "object" },
197
+ example: { type: "object" },
198
+ patternKey: { type: "string" },
199
+ patternValue: { type: "string" },
200
+ nullable: { type: "boolean" },
201
+ minProperties: { type: "integer" },
202
+ maxProperties: { type: "integer" },
203
+ editor: { enum: ["json", "proxy", "hidden"] },
204
+ sectionCaption: { type: "string" },
205
+ sectionDescription: { type: "string" }
206
+ },
207
+ required: ["type", "title", "description", "editor"]
208
+ },
209
+ integerProperty: {
210
+ title: "Integer property",
211
+ type: "object",
212
+ additionalProperties: false,
213
+ properties: {
214
+ type: { enum: ["integer"] },
215
+ title: { type: "string" },
216
+ description: { type: "string" },
217
+ default: { type: "integer" },
218
+ prefill: { type: "integer" },
219
+ example: { type: "integer" },
220
+ nullable: { type: "boolean" },
221
+ minimum: { type: "integer" },
222
+ maximum: { type: "integer" },
223
+ unit: { type: "string" },
224
+ editor: { enum: ["number", "hidden"] },
225
+ sectionCaption: { type: "string" },
226
+ sectionDescription: { type: "string" }
227
+ },
228
+ required: ["type", "title", "description"]
229
+ },
230
+ booleanProperty: {
231
+ title: "Boolean property",
232
+ type: "object",
233
+ additionalProperties: false,
234
+ properties: {
235
+ type: { enum: ["boolean"] },
236
+ title: { type: "string" },
237
+ description: { type: "string" },
238
+ default: { type: "boolean" },
239
+ example: { type: "boolean" },
240
+ nullable: { type: "boolean" },
241
+ groupCaption: { type: "string" },
242
+ groupDescription: { type: "string" },
243
+ editor: { enum: ["checkbox", "hidden"] },
244
+ sectionCaption: { type: "string" },
245
+ sectionDescription: { type: "string" }
246
+ },
247
+ required: ["type", "title", "description"]
248
+ },
249
+ anyProperty: {
250
+ title: "Any property",
251
+ type: "object",
252
+ additionalProperties: false,
253
+ properties: {
254
+ type: {
255
+ type: ["array"],
256
+ items: {
257
+ type: "string",
258
+ enum: ["object", "array", "string", "integer", "boolean"]
259
+ },
260
+ uniqueItems: true,
261
+ additionalItems: false,
262
+ minItems: 1
263
+ },
264
+ title: { type: "string" },
265
+ description: { type: "string" },
266
+ default: { type: ["object", "array", "string", "integer", "boolean"] },
267
+ prefill: { type: ["object", "array", "string", "integer", "boolean"] },
268
+ example: { type: ["object", "array", "string", "integer", "boolean"] },
269
+ nullable: { type: "boolean" },
270
+ editor: { enum: ["json", "hidden"] },
271
+ sectionCaption: { type: "string" },
272
+ sectionDescription: { type: "string" }
273
+ },
274
+ required: ["type", "title", "description", "editor"]
275
+ }
276
+ }
277
+ };
278
+
279
+ // src/input_schema.ts
280
+ var { definitions } = schema_default;
281
+ function parseAjvError(error, rootName, properties = {}, input = {}) {
282
+ let fieldKey;
283
+ let message;
284
+ if (error.keyword === "type") {
285
+ fieldKey = error.instancePath.split("/").pop();
286
+ if (properties[fieldKey] && properties[fieldKey].nullable && input[fieldKey] === null) {
287
+ return null;
288
+ }
289
+ message = m("inputSchema.validation.generic", { rootName, fieldKey, message: error.message });
290
+ } else if (error.keyword === "required") {
291
+ fieldKey = error.params.missingProperty;
292
+ message = m("inputSchema.validation.required", { rootName, fieldKey });
293
+ } else if (error.keyword === "additionalProperties") {
294
+ fieldKey = error.params.additionalProperty;
295
+ message = m("inputSchema.validation.additionalProperty", { rootName, fieldKey });
296
+ } else if (error.keyword === "enum") {
297
+ fieldKey = error.instancePath.split("/").pop();
298
+ const errorMessage = `${error.message}: "${error.params.allowedValues.join('", "')}"`;
299
+ message = m("inputSchema.validation.generic", { rootName, fieldKey, message: errorMessage });
300
+ } else {
301
+ fieldKey = error.instancePath.split("/").pop();
302
+ message = m("inputSchema.validation.generic", { rootName, fieldKey, message: error.message });
303
+ }
304
+ return { fieldKey, message };
305
+ }
306
+ __name(parseAjvError, "parseAjvError");
307
+ var validateAgainstSchemaOrThrow = /* @__PURE__ */ __name((validator, obj, inputSchema, rootName) => {
308
+ if (validator.validate(inputSchema, obj))
309
+ return;
310
+ const errorMessage = parseAjvError(validator.errors[0], rootName)?.message;
311
+ throw new Error(`Input schema is not valid (${errorMessage})`);
312
+ }, "validateAgainstSchemaOrThrow");
313
+ var validateBasicStructure = /* @__PURE__ */ __name((validator, obj) => {
314
+ const schemaWithoutProperties = { ...schema_default };
315
+ schemaWithoutProperties.properties = { ...schema_default.properties, properties: { type: "object" } };
316
+ validateAgainstSchemaOrThrow(validator, obj, schemaWithoutProperties, "schema");
317
+ }, "validateBasicStructure");
318
+ var validateField = /* @__PURE__ */ __name((validator, fieldSchema, fieldKey) => {
319
+ const matchingDefinitions = Object.values(definitions).filter((definition2) => {
320
+ return definition2.properties.type.enum ? definition2.properties.type.enum[0] === fieldSchema.type : Array.isArray(fieldSchema.type);
321
+ });
322
+ if (matchingDefinitions.length === 0) {
323
+ const errorMessage = m("inputSchema.validation.noMatchingDefinition", { fieldKey });
324
+ throw new Error(`Input schema is not valid (${errorMessage})`);
325
+ }
326
+ if (matchingDefinitions.length === 1) {
327
+ validateAgainstSchemaOrThrow(validator, fieldSchema, matchingDefinitions[0], `schema.properties.${fieldKey}`);
328
+ return;
329
+ }
330
+ if (fieldSchema.enum) {
331
+ const definition2 = matchingDefinitions.filter((item) => !!item.properties.enum).pop();
332
+ if (!definition2)
333
+ throw new Error('Input schema validation failed to find "enum property" definition');
334
+ validateAgainstSchemaOrThrow(validator, fieldSchema, definition2, `schema.properties.${fieldKey}.enum`);
335
+ return;
336
+ }
337
+ const definition = matchingDefinitions.filter((item) => !item.properties.enum).pop();
338
+ if (!definition)
339
+ throw new Error('Input schema validation failed to find other than "enum property" definition');
340
+ validateAgainstSchemaOrThrow(validator, fieldSchema, definition, `schema.properties.${fieldKey}`);
341
+ }, "validateField");
342
+ function validateInputSchema(validator, inputSchema) {
343
+ validateBasicStructure(validator, inputSchema);
344
+ Object.entries(inputSchema.properties).forEach(([fieldKey, fieldSchema]) => validateField(validator, fieldSchema, fieldKey));
345
+ validateAgainstSchemaOrThrow(validator, inputSchema, schema_default, "schema");
346
+ }
347
+ __name(validateInputSchema, "validateInputSchema");
348
+
349
+ // src/utilities.ts
350
+ var import_consts = require("@apify/consts");
351
+ var import_countries_list = require("countries-list");
352
+ var import_escaya = require("escaya");
353
+ function validateProxyField(fieldKey, value, isRequired = false, options = null) {
354
+ const fieldErrors = [];
355
+ if (isRequired) {
356
+ if (value === null)
357
+ return fieldErrors;
358
+ if (!value) {
359
+ const message = m("inputSchema.validation.required", { rootName: "input", fieldKey });
360
+ fieldErrors.push(message);
361
+ return fieldErrors;
362
+ }
363
+ const { useApifyProxy: useApifyProxy2, proxyUrls: proxyUrls2 } = value;
364
+ if (!useApifyProxy2 && (!Array.isArray(proxyUrls2) || proxyUrls2.length === 0)) {
365
+ fieldErrors.push(m("inputSchema.validation.proxyRequired", { rootName: "input", fieldKey }));
366
+ return fieldErrors;
367
+ }
368
+ }
369
+ if (!value)
370
+ return fieldErrors;
371
+ const { useApifyProxy, proxyUrls, apifyProxyGroups, apifyProxyCountry } = value;
372
+ if (!useApifyProxy && Array.isArray(proxyUrls)) {
373
+ let invalidUrl = false;
374
+ proxyUrls.forEach((url) => {
375
+ if (!import_consts.PROXY_URL_REGEX.test(url.trim()))
376
+ invalidUrl = url.trim();
377
+ });
378
+ if (invalidUrl) {
379
+ fieldErrors.push(m("inputSchema.validation.customProxyInvalid", { invalidUrl }));
380
+ }
381
+ }
382
+ if (!useApifyProxy && apifyProxyCountry) {
383
+ fieldErrors.push(m("inputSchema.validation.apifyProxyCountryWithoutApifyProxyForbidden"));
384
+ }
385
+ if (!useApifyProxy)
386
+ return fieldErrors;
387
+ if (apifyProxyCountry && !import_countries_list.countries[apifyProxyCountry]) {
388
+ fieldErrors.push(m("inputSchema.validation.apifyProxyCountryInvalid", { invalidCountry: apifyProxyCountry }));
389
+ }
390
+ if (!options)
391
+ return fieldErrors;
392
+ const selectedProxyGroups = apifyProxyGroups || [];
393
+ if (!selectedProxyGroups.length && !options.hasAutoProxyGroups) {
394
+ fieldErrors.push(m("inputSchema.validation.noAvailableAutoProxy"));
395
+ return fieldErrors;
396
+ }
397
+ const availableProxyGroupsById = {};
398
+ (options.availableProxyGroups || []).forEach((group) => {
399
+ availableProxyGroupsById[group] = true;
400
+ });
401
+ const unavailableProxyGroups = selectedProxyGroups.filter((group) => !availableProxyGroupsById[group]);
402
+ if (unavailableProxyGroups.length) {
403
+ fieldErrors.push(m("inputSchema.validation.proxyGroupsNotAvailable", {
404
+ rootName: "input",
405
+ fieldKey,
406
+ groups: unavailableProxyGroups.join(", ")
407
+ }));
408
+ }
409
+ const blockedProxyGroupsById = options.disabledProxyGroups || {};
410
+ selectedProxyGroups.filter((group) => blockedProxyGroupsById[group]).forEach((blockedGroup) => {
411
+ fieldErrors.push(blockedProxyGroupsById[blockedGroup]);
412
+ });
413
+ return fieldErrors;
414
+ }
415
+ __name(validateProxyField, "validateProxyField");
416
+ function validateInputUsingValidator(validator, inputSchema, input, options = {}) {
417
+ const isValid = validator(input);
418
+ const { properties } = inputSchema;
419
+ const required = inputSchema.required || [];
420
+ let errors = [];
421
+ if (!isValid) {
422
+ errors = validator.errors.map((error) => parseAjvError(error, "input", properties, input)).filter((error) => !!error);
423
+ }
424
+ Object.keys(properties).forEach((property) => {
425
+ const value = input[property];
426
+ const { type, editor, patternKey, patternValue } = properties[property];
427
+ const fieldErrors = [];
428
+ if (type === "object" && editor === "proxy") {
429
+ const proxyValidationErrors = validateProxyField(property, value, required.includes(property), options.proxy);
430
+ proxyValidationErrors.forEach((error) => {
431
+ fieldErrors.push(error);
432
+ });
433
+ }
434
+ if (type === "array" && value && Array.isArray(value)) {
435
+ if (editor === "requestListSources") {
436
+ const invalidIndexes = [];
437
+ value.forEach((item, index) => {
438
+ if (!item)
439
+ invalidIndexes.push(index);
440
+ else if (!item.url && !item.requestsFromUrl)
441
+ invalidIndexes.push(index);
442
+ else if (item.url && !import_consts.URL_REGEX.test(item.url))
443
+ invalidIndexes.push(index);
444
+ else if (item.requestsFromUrl && !import_consts.URL_REGEX.test(item.requestsFromUrl))
445
+ invalidIndexes.push(index);
446
+ });
447
+ if (invalidIndexes.length) {
448
+ fieldErrors.push(m("inputSchema.validation.requestListSourcesInvalid", {
449
+ rootName: "input",
450
+ fieldKey: property,
451
+ invalidIndexes: invalidIndexes.join(",")
452
+ }));
453
+ }
454
+ }
455
+ if (patternKey && editor === "keyValue") {
456
+ const check = new RegExp(patternKey);
457
+ const invalidIndexes = [];
458
+ value.forEach((item, index) => {
459
+ if (!check.test(item.key))
460
+ invalidIndexes.push(index);
461
+ });
462
+ if (invalidIndexes.length) {
463
+ fieldErrors.push(m("inputSchema.validation.arrayKeysInvalid", {
464
+ rootName: "input",
465
+ fieldKey: property,
466
+ invalidIndexes: invalidIndexes.join(","),
467
+ pattern: patternKey
468
+ }));
469
+ }
470
+ }
471
+ if (patternValue && editor === "keyValue") {
472
+ const check = new RegExp(patternValue);
473
+ const invalidIndexes = [];
474
+ value.forEach((item, index) => {
475
+ if (!check.test(item.value))
476
+ invalidIndexes.push(index);
477
+ });
478
+ if (invalidIndexes.length) {
479
+ fieldErrors.push(m("inputSchema.validation.arrayValuesInvalid", {
480
+ rootName: "input",
481
+ fieldKey: property,
482
+ invalidIndexes: invalidIndexes.join(","),
483
+ pattern: patternValue
484
+ }));
485
+ }
486
+ } else if (patternValue && editor === "stringList") {
487
+ const check = new RegExp(patternValue);
488
+ const invalidIndexes = [];
489
+ value.forEach((item, index) => {
490
+ if (!check.test(item))
491
+ invalidIndexes.push(index);
492
+ });
493
+ if (invalidIndexes.length) {
494
+ fieldErrors.push(m("inputSchema.validation.arrayValuesInvalid", {
495
+ rootName: "input",
496
+ fieldKey: property,
497
+ invalidIndexes: invalidIndexes.join(","),
498
+ pattern: patternValue
499
+ }));
500
+ }
501
+ }
502
+ }
503
+ if (type === "object" && value) {
504
+ if (patternKey) {
505
+ const check = new RegExp(patternKey);
506
+ const invalidKeys = [];
507
+ Object.keys(value).forEach((key) => {
508
+ if (!check.test(key))
509
+ invalidKeys.push(key);
510
+ });
511
+ if (invalidKeys.length) {
512
+ fieldErrors.push(m("inputSchema.validation.objectKeysInvalid", {
513
+ rootName: "input",
514
+ fieldKey: property,
515
+ invalidKeys: invalidKeys.join(","),
516
+ pattern: patternKey
517
+ }));
518
+ }
519
+ }
520
+ if (patternValue) {
521
+ const check = new RegExp(patternValue);
522
+ const invalidKeys = [];
523
+ Object.keys(value).forEach((key) => {
524
+ const propertyValue = value[key];
525
+ if (typeof propertyValue !== "string" || !check.test(propertyValue))
526
+ invalidKeys.push(key);
527
+ });
528
+ if (invalidKeys.length) {
529
+ fieldErrors.push(m("inputSchema.validation.objectValuesInvalid", {
530
+ rootName: "input",
531
+ fieldKey: property,
532
+ invalidKeys: invalidKeys.join(","),
533
+ pattern: patternValue
534
+ }));
535
+ }
536
+ }
537
+ }
538
+ if (fieldErrors.length > 0) {
539
+ const message = fieldErrors.join(", ");
540
+ errors.push({ fieldKey: property, message });
541
+ }
542
+ });
543
+ return errors;
544
+ }
545
+ __name(validateInputUsingValidator, "validateInputUsingValidator");
546
+ function makeInputJsFieldsReadable(json, jsFields, jsonSpacing = 4, globalSpacing = 0) {
547
+ const parsedJson = JSON.parse(json);
548
+ const replacements = {};
549
+ jsFields.forEach((field) => {
550
+ let maybeFunction = parsedJson[field];
551
+ if (!maybeFunction || typeof maybeFunction !== "string")
552
+ return;
553
+ let ast;
554
+ try {
555
+ ast = (0, import_escaya.parseScript)(maybeFunction, { cst: true });
556
+ } catch (err) {
557
+ return;
558
+ }
559
+ const isMultiline = maybeFunction.includes("\n");
560
+ const isSingleFunction = ast && ast.leafs && ast.leafs.length === 1 && ast.leafs[0] && (ast.leafs[0].type === "FunctionDeclaration" || ast.leafs[0].type === "ExpressionStatement" && ast.leafs[0].expression.type === "ArrowFunction");
561
+ if (!isSingleFunction && !isMultiline)
562
+ return;
563
+ const spaces = new Array(isSingleFunction ? jsonSpacing : jsonSpacing * 2).fill(" ").join("");
564
+ maybeFunction = maybeFunction.split("\n").join(`
565
+ ${spaces}`).trim();
566
+ const replacementValue = isSingleFunction ? maybeFunction.replace(/[;]+$/g, "") : `\`${maybeFunction}\``;
567
+ const replacementToken = `<<<REPLACEMENT_TOKEN:${Math.random()}>>>`;
568
+ replacements[replacementToken] = replacementValue;
569
+ parsedJson[field] = replacementToken;
570
+ });
571
+ let niceJson = JSON.stringify(parsedJson, null, jsonSpacing);
572
+ Object.entries(replacements).forEach(([replacementToken, replacementValue]) => {
573
+ niceJson = niceJson.replace(`"${replacementToken}"`, replacementValue);
574
+ });
575
+ const globalSpaces = new Array(globalSpacing).fill(" ").join("");
576
+ niceJson = niceJson.split("\n").join(`
577
+ ${globalSpaces}`);
578
+ return niceJson;
579
+ }
580
+ __name(makeInputJsFieldsReadable, "makeInputJsFieldsReadable");
581
+ // Annotate the CommonJS export names for ESM import in node:
582
+ 0 && (module.exports = {
583
+ inputSchema,
584
+ m,
585
+ makeInputJsFieldsReadable,
586
+ parseAjvError,
587
+ validateInputSchema,
588
+ validateInputUsingValidator
589
+ });
590
+ //# sourceMappingURL=index.js.map