3 Object.defineProperty(exports, "__esModule", {
6 exports.default = void 0;
11 } = require("./util/hints");
12 /** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
14 /** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */
16 /** @typedef {import("./validate").Schema} Schema */
18 /** @typedef {import("./validate").ValidationErrorConfiguration} ValidationErrorConfiguration */
20 /** @typedef {import("./validate").PostFormatter} PostFormatter */
22 /** @typedef {import("./validate").SchemaUtilErrorObject} SchemaUtilErrorObject */
58 additionalProperties: 2,
63 * @param {Array<SchemaUtilErrorObject>} array
64 * @param {(item: SchemaUtilErrorObject) => number} fn
65 * @returns {Array<SchemaUtilErrorObject>}
68 function filterMax(array, fn) {
69 const evaluatedMax = array.reduce((max, item) => Math.max(max, fn(item)), 0);
70 return array.filter(item => fn(item) === evaluatedMax);
74 * @param {Array<SchemaUtilErrorObject>} children
75 * @returns {Array<SchemaUtilErrorObject>}
79 function filterChildren(children) {
80 let newChildren = children;
81 newChildren = filterMax(newChildren,
84 * @param {SchemaUtilErrorObject} error
87 error => error.dataPath ? error.dataPath.length : 0);
88 newChildren = filterMax(newChildren,
90 * @param {SchemaUtilErrorObject} error
94 /** @type {keyof typeof SPECIFICITY} */
99 * Find all children errors
100 * @param {Array<SchemaUtilErrorObject>} children
101 * @param {Array<string>} schemaPaths
102 * @return {number} returns index of first child
106 function findAllChildren(children, schemaPaths) {
107 let i = children.length - 1;
111 * @param {string} schemaPath
114 schemaPath => children[i].schemaPath.indexOf(schemaPath) !== 0;
116 while (i > -1 && !schemaPaths.every(predicate)) {
117 if (children[i].keyword === "anyOf" || children[i].keyword === "oneOf") {
118 const refs = extractRefs(children[i]);
119 const childrenStart = findAllChildren(children.slice(0, i), refs.concat(children[i].schemaPath));
120 i = childrenStart - 1;
129 * Extracts all refs from schema
130 * @param {SchemaUtilErrorObject} error
131 * @return {Array<string>}
135 function extractRefs(error) {
140 if (!Array.isArray(schema)) {
146 }) => $ref).filter(s => s);
149 * Groups children by their first level parent (assuming that error is root)
150 * @param {Array<SchemaUtilErrorObject>} children
151 * @return {Array<SchemaUtilErrorObject>}
155 function groupChildrenByFirstChild(children) {
157 let i = children.length - 1;
160 const child = children[i];
162 if (child.keyword === "anyOf" || child.keyword === "oneOf") {
163 const refs = extractRefs(child);
164 const childrenStart = findAllChildren(children.slice(0, i), refs.concat(child.schemaPath));
166 if (childrenStart !== i) {
167 result.push(Object.assign({}, child, {
168 children: children.slice(childrenStart, i)
182 result.push(children[i]);
185 return result.reverse();
188 * @param {string} str
189 * @param {string} prefix
194 function indent(str, prefix) {
195 return str.replace(/\n(?!$)/g, `\n${prefix}`);
198 * @param {Schema} schema
199 * @returns {schema is (Schema & {not: Schema})}
203 function hasNotInSchema(schema) {
207 * @param {Schema} schema
212 function findFirstTypedSchema(schema) {
213 if (hasNotInSchema(schema)) {
214 return findFirstTypedSchema(schema.not);
220 * @param {Schema} schema
225 function canApplyNot(schema) {
226 const typedSchema = findFirstTypedSchema(schema);
227 return likeNumber(typedSchema) || likeInteger(typedSchema) || likeString(typedSchema) || likeNull(typedSchema) || likeBoolean(typedSchema);
230 * @param {any} maybeObj
235 function isObject(maybeObj) {
236 return typeof maybeObj === "object" && maybeObj !== null;
239 * @param {Schema} schema
244 function likeNumber(schema) {
245 return schema.type === "number" || typeof schema.minimum !== "undefined" || typeof schema.exclusiveMinimum !== "undefined" || typeof schema.maximum !== "undefined" || typeof schema.exclusiveMaximum !== "undefined" || typeof schema.multipleOf !== "undefined";
248 * @param {Schema} schema
253 function likeInteger(schema) {
254 return schema.type === "integer" || typeof schema.minimum !== "undefined" || typeof schema.exclusiveMinimum !== "undefined" || typeof schema.maximum !== "undefined" || typeof schema.exclusiveMaximum !== "undefined" || typeof schema.multipleOf !== "undefined";
257 * @param {Schema} schema
262 function likeString(schema) {
263 return schema.type === "string" || typeof schema.minLength !== "undefined" || typeof schema.maxLength !== "undefined" || typeof schema.pattern !== "undefined" || typeof schema.format !== "undefined" || typeof schema.formatMinimum !== "undefined" || typeof schema.formatMaximum !== "undefined";
266 * @param {Schema} schema
271 function likeBoolean(schema) {
272 return schema.type === "boolean";
275 * @param {Schema} schema
280 function likeArray(schema) {
281 return schema.type === "array" || typeof schema.minItems === "number" || typeof schema.maxItems === "number" || typeof schema.uniqueItems !== "undefined" || typeof schema.items !== "undefined" || typeof schema.additionalItems !== "undefined" || typeof schema.contains !== "undefined";
284 * @param {Schema & {patternRequired?: Array<string>}} schema
289 function likeObject(schema) {
290 return schema.type === "object" || typeof schema.minProperties !== "undefined" || typeof schema.maxProperties !== "undefined" || typeof schema.required !== "undefined" || typeof schema.properties !== "undefined" || typeof schema.patternProperties !== "undefined" || typeof schema.additionalProperties !== "undefined" || typeof schema.dependencies !== "undefined" || typeof schema.propertyNames !== "undefined" || typeof schema.patternRequired !== "undefined";
293 * @param {Schema} schema
298 function likeNull(schema) {
299 return schema.type === "null";
302 * @param {string} type
307 function getArticle(type) {
308 if (/^[aeiou]/i.test(type)) {
315 * @param {Schema=} schema
320 function getSchemaNonTypes(schema) {
326 if (likeNumber(schema) || likeInteger(schema)) {
327 return " | should be any non-number";
330 if (likeString(schema)) {
331 return " | should be any non-string";
334 if (likeArray(schema)) {
335 return " | should be any non-array";
338 if (likeObject(schema)) {
339 return " | should be any non-object";
346 * @param {Array<string>} hints
351 function formatHints(hints) {
352 return hints.length > 0 ? `(${hints.join(", ")})` : "";
355 * @param {Schema} schema
356 * @param {boolean} logic
357 * @returns {string[]}
361 function getHints(schema, logic) {
362 if (likeNumber(schema) || likeInteger(schema)) {
363 return numberHints(schema, logic);
364 } else if (likeString(schema)) {
365 return stringHints(schema, logic);
371 class ValidationError extends Error {
373 * @param {Array<SchemaUtilErrorObject>} errors
374 * @param {Schema} schema
375 * @param {ValidationErrorConfiguration} configuration
377 constructor(errors, schema, configuration = {}) {
379 /** @type {string} */
381 this.name = "ValidationError";
382 /** @type {Array<SchemaUtilErrorObject>} */
384 this.errors = errors;
385 /** @type {Schema} */
387 this.schema = schema;
388 let headerNameFromSchema;
389 let baseDataPathFromSchema;
391 if (schema.title && (!configuration.name || !configuration.baseDataPath)) {
392 const splittedTitleFromSchema = schema.title.match(/^(.+) (.+)$/);
394 if (splittedTitleFromSchema) {
395 if (!configuration.name) {
396 [, headerNameFromSchema] = splittedTitleFromSchema;
399 if (!configuration.baseDataPath) {
400 [,, baseDataPathFromSchema] = splittedTitleFromSchema;
404 /** @type {string} */
407 this.headerName = configuration.name || headerNameFromSchema || "Object";
408 /** @type {string} */
410 this.baseDataPath = configuration.baseDataPath || baseDataPathFromSchema || "configuration";
411 /** @type {PostFormatter | null} */
413 this.postFormatter = configuration.postFormatter || null;
414 const header = `Invalid ${this.baseDataPath} object. ${this.headerName} has been initialized using ${getArticle(this.baseDataPath)} ${this.baseDataPath} object that does not match the API schema.\n`;
415 /** @type {string} */
417 this.message = `${header}${this.formatValidationErrors(errors)}`;
418 Error.captureStackTrace(this, this.constructor);
421 * @param {string} path
426 getSchemaPart(path) {
427 const newPath = path.split("/");
428 let schemaPart = this.schema;
430 for (let i = 1; i < newPath.length; i++) {
431 const inner = schemaPart[
432 /** @type {keyof Schema} */
445 * @param {Schema} schema
446 * @param {boolean} logic
447 * @param {Array<Object>} prevSchemas
452 formatSchema(schema, logic = true, prevSchemas = []) {
453 let newLogic = logic;
455 const formatInnerSchema =
458 * @param {Object} innerSchema
459 * @param {boolean=} addSelf
462 (innerSchema, addSelf) => {
464 return this.formatSchema(innerSchema, newLogic, prevSchemas);
467 if (prevSchemas.includes(innerSchema)) {
468 return "(recursive)";
471 return this.formatSchema(innerSchema, newLogic, prevSchemas.concat(schema));
474 if (hasNotInSchema(schema) && !likeObject(schema)) {
475 if (canApplyNot(schema.not)) {
477 return formatInnerSchema(schema.not);
480 const needApplyLogicHere = !schema.not.not;
481 const prefix = logic ? "" : "non ";
483 return needApplyLogicHere ? prefix + formatInnerSchema(schema.not) : formatInnerSchema(schema.not);
487 /** @type {Schema & {instanceof: string | Array<string>}} */
492 /** @type {Schema & {instanceof: string | Array<string>}} */
494 const values = !Array.isArray(value) ? [value] : value;
497 * @param {string} item
500 item => item === "Function" ? "function" : item).join(" | ");
505 /** @type {Array<any>} */
506 schema.enum.map(item => JSON.stringify(item)).join(" | ")
510 if (typeof schema.const !== "undefined") {
511 return JSON.stringify(schema.const);
516 /** @type {Array<Schema>} */
517 schema.oneOf.map(item => formatInnerSchema(item, true)).join(" | ")
523 /** @type {Array<Schema>} */
524 schema.anyOf.map(item => formatInnerSchema(item, true)).join(" | ")
530 /** @type {Array<Schema>} */
531 schema.allOf.map(item => formatInnerSchema(item, true)).join(" & ")
536 /** @type {JSONSchema7} */
543 /** @type {JSONSchema7} */
545 return `${ifValue ? `if ${formatInnerSchema(ifValue)}` : ""}${thenValue ? ` then ${formatInnerSchema(thenValue)}` : ""}${elseValue ? ` else ${formatInnerSchema(elseValue)}` : ""}`;
549 return formatInnerSchema(this.getSchemaPart(schema.$ref), true);
552 if (likeNumber(schema) || likeInteger(schema)) {
553 const [type, ...hints] = getHints(schema, logic);
554 const str = `${type}${hints.length > 0 ? ` ${formatHints(hints)}` : ""}`;
555 return logic ? str : hints.length > 0 ? `non-${type} | ${str}` : `non-${type}`;
558 if (likeString(schema)) {
559 const [type, ...hints] = getHints(schema, logic);
560 const str = `${type}${hints.length > 0 ? ` ${formatHints(hints)}` : ""}`;
561 return logic ? str : str === "string" ? "non-string" : `non-string | ${str}`;
564 if (likeBoolean(schema)) {
565 return `${logic ? "" : "non-"}boolean`;
568 if (likeArray(schema)) {
569 // not logic already applied in formatValidationError
573 if (typeof schema.minItems === "number") {
574 hints.push(`should not have fewer than ${schema.minItems} item${schema.minItems > 1 ? "s" : ""}`);
577 if (typeof schema.maxItems === "number") {
578 hints.push(`should not have more than ${schema.maxItems} item${schema.maxItems > 1 ? "s" : ""}`);
581 if (schema.uniqueItems) {
582 hints.push("should not have duplicate items");
585 const hasAdditionalItems = typeof schema.additionalItems === "undefined" || Boolean(schema.additionalItems);
589 if (Array.isArray(schema.items) && schema.items.length > 0) {
591 /** @type {Array<Schema>} */
592 schema.items.map(item => formatInnerSchema(item)).join(", ")}`;
594 if (hasAdditionalItems) {
595 if (schema.additionalItems && isObject(schema.additionalItems) && Object.keys(schema.additionalItems).length > 0) {
596 hints.push(`additional items should be ${formatInnerSchema(schema.additionalItems)}`);
599 } else if (schema.items && Object.keys(schema.items).length > 0) {
600 // "additionalItems" is ignored
601 items = `${formatInnerSchema(schema.items)}`;
603 // Fallback for empty `items` value
607 // "additionalItems" is ignored
611 if (schema.contains && Object.keys(schema.contains).length > 0) {
612 hints.push(`should contains at least one ${this.formatSchema(schema.contains)} item`);
615 return `[${items}${hasAdditionalItems ? ", ..." : ""}]${hints.length > 0 ? ` (${hints.join(", ")})` : ""}`;
618 if (likeObject(schema)) {
619 // not logic already applied in formatValidationError
623 if (typeof schema.minProperties === "number") {
624 hints.push(`should not have fewer than ${schema.minProperties} ${schema.minProperties > 1 ? "properties" : "property"}`);
627 if (typeof schema.maxProperties === "number") {
628 hints.push(`should not have more than ${schema.maxProperties} ${schema.minProperties && schema.minProperties > 1 ? "properties" : "property"}`);
631 if (schema.patternProperties && Object.keys(schema.patternProperties).length > 0) {
632 const patternProperties = Object.keys(schema.patternProperties);
633 hints.push(`additional property names should match pattern${patternProperties.length > 1 ? "s" : ""} ${patternProperties.map(pattern => JSON.stringify(pattern)).join(" | ")}`);
636 const properties = schema.properties ? Object.keys(schema.properties) : [];
637 const required = schema.required ? schema.required : [];
638 const allProperties = [...new Set(
639 /** @type {Array<string>} */
640 [].concat(required).concat(properties))];
641 const objectStructure = allProperties.map(property => {
642 const isRequired = required.includes(property); // Some properties need quotes, maybe we should add check
643 // Maybe we should output type of property (`foo: string`), but it is looks very unreadable
645 return `${property}${isRequired ? "" : "?"}`;
646 }).concat(typeof schema.additionalProperties === "undefined" || Boolean(schema.additionalProperties) ? schema.additionalProperties && isObject(schema.additionalProperties) ? [`<key>: ${formatInnerSchema(schema.additionalProperties)}`] : ["…"] : []).join(", ");
652 /** @type {Schema & {patternRequired?: Array<string>;}} */
656 Object.keys(dependencies).forEach(dependencyName => {
657 const dependency = dependencies[dependencyName];
659 if (Array.isArray(dependency)) {
660 hints.push(`should have ${dependency.length > 1 ? "properties" : "property"} ${dependency.map(dep => `'${dep}'`).join(", ")} when property '${dependencyName}' is present`);
662 hints.push(`should be valid according to the schema ${formatInnerSchema(dependency)} when property '${dependencyName}' is present`);
667 if (propertyNames && Object.keys(propertyNames).length > 0) {
668 hints.push(`each property name should match format ${JSON.stringify(schema.propertyNames.format)}`);
671 if (patternRequired && patternRequired.length > 0) {
672 hints.push(`should have property matching pattern ${patternRequired.map(
674 * @param {string} item
677 item => JSON.stringify(item))}`);
680 return `object {${objectStructure ? ` ${objectStructure} ` : ""}}${hints.length > 0 ? ` (${hints.join(", ")})` : ""}`;
683 if (likeNull(schema)) {
684 return `${logic ? "" : "non-"}null`;
687 if (Array.isArray(schema.type)) {
688 // not logic already applied in formatValidationError
689 return `${schema.type.join(" | ")}`;
690 } // Fallback for unknown keywords
691 // not logic already applied in formatValidationError
693 /* istanbul ignore next */
696 return JSON.stringify(schema, null, 2);
699 * @param {Schema=} schemaPart
700 * @param {(boolean | Array<string>)=} additionalPath
701 * @param {boolean=} needDot
702 * @param {boolean=} logic
707 getSchemaPartText(schemaPart, additionalPath, needDot = false, logic = true) {
712 if (Array.isArray(additionalPath)) {
713 for (let i = 0; i < additionalPath.length; i++) {
714 /** @type {Schema | undefined} */
715 const inner = schemaPart[
716 /** @type {keyof Schema} */
720 // eslint-disable-next-line no-param-reassign
728 while (schemaPart.$ref) {
729 // eslint-disable-next-line no-param-reassign
730 schemaPart = this.getSchemaPart(schemaPart.$ref);
733 let schemaText = `${this.formatSchema(schemaPart, logic)}${needDot ? "." : ""}`;
735 if (schemaPart.description) {
736 schemaText += `\n-> ${schemaPart.description}`;
739 if (schemaPart.link) {
740 schemaText += `\n-> Read more at ${schemaPart.link}`;
746 * @param {Schema=} schemaPart
751 getSchemaPartDescription(schemaPart) {
756 while (schemaPart.$ref) {
757 // eslint-disable-next-line no-param-reassign
758 schemaPart = this.getSchemaPart(schemaPart.$ref);
763 if (schemaPart.description) {
764 schemaText += `\n-> ${schemaPart.description}`;
767 if (schemaPart.link) {
768 schemaText += `\n-> Read more at ${schemaPart.link}`;
774 * @param {SchemaUtilErrorObject} error
779 formatValidationError(error) {
782 dataPath: errorDataPath
784 const dataPath = `${this.baseDataPath}${errorDataPath}`;
792 } = error; // eslint-disable-next-line default-case
795 /** @type {import("ajv").TypeParams} */
798 return `${dataPath} should be a ${this.getSchemaPartText(parentSchema, false, true)}`;
801 return `${dataPath} should be an ${this.getSchemaPartText(parentSchema, false, true)}`;
804 return `${dataPath} should be a ${this.getSchemaPartText(parentSchema, false, true)}`;
807 return `${dataPath} should be a ${this.getSchemaPartText(parentSchema, false, true)}`;
810 return `${dataPath} should be an array:\n${this.getSchemaPartText(parentSchema)}`;
813 return `${dataPath} should be an object:\n${this.getSchemaPartText(parentSchema)}`;
816 return `${dataPath} should be a ${this.getSchemaPartText(parentSchema, false, true)}`;
819 return `${dataPath} should be:\n${this.getSchemaPartText(parentSchema)}`;
828 return `${dataPath} should be an instance of ${this.getSchemaPartText(parentSchema, false, true)}`;
840 /** @type {import("ajv").PatternParams} */
842 return `${dataPath} should match pattern ${JSON.stringify(pattern)}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
854 /** @type {import("ajv").FormatParams} */
856 return `${dataPath} should match format ${JSON.stringify(format)}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
859 case "formatMinimum":
860 case "formatMaximum":
870 /** @type {import("ajv").ComparisonParams} */
872 return `${dataPath} should be ${comparison} ${JSON.stringify(limit)}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
877 case "exclusiveMinimum":
878 case "exclusiveMaximum":
888 /** @type {import("ajv").ComparisonParams} */
890 const [, ...hints] = getHints(
891 /** @type {Schema} */
894 if (hints.length === 0) {
895 hints.push(`should be ${comparison} ${limit}`);
898 return `${dataPath} ${hints.join(" ")}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
910 /** @type {import("ajv").MultipleOfParams} */
912 return `${dataPath} should be multiple of ${multipleOf}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
915 case "patternRequired":
924 /** @type {import("ajv").PatternRequiredParams} */
926 return `${dataPath} should have property matching pattern ${JSON.stringify(missingPattern)}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
938 /** @type {import("ajv").LimitParams} */
942 return `${dataPath} should be a non-empty string${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
945 const length = limit - 1;
946 return `${dataPath} should be longer than ${length} character${length > 1 ? "s" : ""}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
958 /** @type {import("ajv").LimitParams} */
962 return `${dataPath} should be a non-empty array${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
965 return `${dataPath} should not have fewer than ${limit} items${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
968 case "minProperties":
977 /** @type {import("ajv").LimitParams} */
981 return `${dataPath} should be a non-empty object${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
984 return `${dataPath} should not have fewer than ${limit} properties${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
996 /** @type {import("ajv").LimitParams} */
998 const max = limit + 1;
999 return `${dataPath} should be shorter than ${max} character${max > 1 ? "s" : ""}${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
1011 /** @type {import("ajv").LimitParams} */
1013 return `${dataPath} should not have more than ${limit} items${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
1016 case "maxProperties":
1025 /** @type {import("ajv").LimitParams} */
1027 return `${dataPath} should not have more than ${limit} properties${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
1039 /** @type {import("ajv").UniqueItemsParams} */
1041 return `${dataPath} should not contain the item '${error.data[i]}' twice${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
1044 case "additionalItems":
1053 /** @type {import("ajv").LimitParams} */
1055 return `${dataPath} should not have more than ${limit} items${getSchemaNonTypes(parentSchema)}. These items are valid:\n${this.getSchemaPartText(parentSchema)}`;
1063 return `${dataPath} should contains at least one ${this.getSchemaPartText(parentSchema, ["contains"])} item${getSchemaNonTypes(parentSchema)}.`;
1072 const missingProperty =
1073 /** @type {import("ajv").DependenciesParams} */
1074 params.missingProperty.replace(/^\./, "");
1075 const hasProperty = parentSchema && Boolean(
1076 /** @type {Schema} */
1077 parentSchema.properties &&
1078 /** @type {Schema} */
1079 parentSchema.properties[missingProperty]);
1080 return `${dataPath} misses the property '${missingProperty}'${getSchemaNonTypes(parentSchema)}.${hasProperty ? ` Should be:\n${this.getSchemaPartText(parentSchema, ["properties", missingProperty])}` : this.getSchemaPartDescription(parentSchema)}`;
1083 case "additionalProperties":
1092 /** @type {import("ajv").AdditionalPropertiesParams} */
1094 return `${dataPath} has an unknown property '${additionalProperty}'${getSchemaNonTypes(parentSchema)}. These properties are valid:\n${this.getSchemaPartText(parentSchema)}`;
1097 case "dependencies":
1107 /** @type {import("ajv").DependenciesParams} */
1109 const dependencies = deps.split(",").map(
1111 * @param {string} dep
1114 dep => `'${dep.trim()}'`).join(", ");
1115 return `${dataPath} should have properties ${dependencies} when property '${property}' is present${getSchemaNonTypes(parentSchema)}.${this.getSchemaPartDescription(parentSchema)}`;
1118 case "propertyNames":
1128 /** @type {import("ajv").PropertyNamesParams} */
1130 return `${dataPath} property name '${propertyName}' is invalid${getSchemaNonTypes(parentSchema)}. Property names should be match format ${JSON.stringify(schema.format)}.${this.getSchemaPartDescription(parentSchema)}`;
1140 /** @type {Schema} */
1141 parentSchema.enum &&
1142 /** @type {Schema} */
1143 parentSchema.enum.length === 1) {
1144 return `${dataPath} should be ${this.getSchemaPartText(parentSchema, false, true)}`;
1147 return `${dataPath} should be one of these:\n${this.getSchemaPartText(parentSchema)}`;
1155 return `${dataPath} should be equal to constant ${this.getSchemaPartText(parentSchema, false, true)}`;
1160 const postfix = likeObject(
1161 /** @type {Schema} */
1162 error.parentSchema) ? `\n${this.getSchemaPartText(error.parentSchema)}` : "";
1163 const schemaOutput = this.getSchemaPartText(error.schema, false, false, false);
1165 if (canApplyNot(error.schema)) {
1166 return `${dataPath} should be any ${schemaOutput}${postfix}.`;
1173 return `${dataPath} should not be ${this.getSchemaPartText(schema, false, true)}${parentSchema && likeObject(parentSchema) ? `\n${this.getSchemaPartText(parentSchema)}` : ""}`;
1184 if (children && children.length > 0) {
1185 if (error.schema.length === 1) {
1186 const lastChild = children[children.length - 1];
1187 const remainingChildren = children.slice(0, children.length - 1);
1188 return this.formatValidationError(Object.assign({}, lastChild, {
1189 children: remainingChildren,
1190 parentSchema: Object.assign({}, parentSchema, lastChild.parentSchema)
1194 let filteredChildren = filterChildren(children);
1196 if (filteredChildren.length === 1) {
1197 return this.formatValidationError(filteredChildren[0]);
1200 filteredChildren = groupChildrenByFirstChild(filteredChildren);
1201 return `${dataPath} should be one of these:\n${this.getSchemaPartText(parentSchema)}\nDetails:\n${filteredChildren.map(
1203 * @param {SchemaUtilErrorObject} nestedError
1206 nestedError => ` * ${indent(this.formatValidationError(nestedError), " ")}`).join("\n")}`;
1209 return `${dataPath} should be one of these:\n${this.getSchemaPartText(parentSchema)}`;
1221 /** @type {import("ajv").IfParams} */
1223 return `${dataPath} should match "${failingKeyword}" schema:\n${this.getSchemaPartText(parentSchema, [failingKeyword])}`;
1226 case "absolutePath":
1232 return `${dataPath}: ${message}${this.getSchemaPartDescription(parentSchema)}`;
1235 /* istanbul ignore next */
1243 const ErrorInJSON = JSON.stringify(error, null, 2); // For `custom`, `false schema`, `$ref` keywords
1244 // Fallback for unknown keywords
1246 return `${dataPath} ${message} (${ErrorInJSON}).\n${this.getSchemaPartText(parentSchema, false)}`;
1251 * @param {Array<SchemaUtilErrorObject>} errors
1256 formatValidationErrors(errors) {
1257 return errors.map(error => {
1258 let formattedError = this.formatValidationError(error);
1260 if (this.postFormatter) {
1261 formattedError = this.postFormatter(formattedError, error);
1264 return ` - ${indent(formattedError, " ")}`;
1270 var _default = ValidationError;
1271 exports.default = _default;