better-ajv-errors
Version:
JSON Schema validation for Human
8 lines (7 loc) • 3.09 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/validation-errors/enum.js"],
"sourcesContent": ["import chalk from 'chalk';\nimport leven from 'leven';\nimport pointer from 'jsonpointer';\nimport BaseValidationError from './base';\n\nexport default class EnumValidationError extends BaseValidationError {\n print() {\n const {\n message,\n params: { allowedValues },\n } = this.options;\n const bestMatch = this.findBestMatch();\n\n const output = [\n chalk`{red {bold ENUM} ${message}}`,\n chalk`{red (${allowedValues.join(', ')})}\\n`,\n ];\n\n return output.concat(\n this.getCodeFrame(\n bestMatch !== null\n ? chalk`\uD83D\uDC48\uD83C\uDFFD Did you mean {magentaBright ${bestMatch}} here?`\n : chalk`\uD83D\uDC48\uD83C\uDFFD Unexpected value, should be equal to one of the allowed values`\n )\n );\n }\n\n getError() {\n const { message, params } = this.options;\n const bestMatch = this.findBestMatch();\n const allowedValues = params.allowedValues.join(', ');\n\n const output = {\n ...this.getLocation(),\n error: `${this.getDecoratedPath()} ${message}: ${allowedValues}`,\n path: this.instancePath,\n };\n\n if (bestMatch !== null) {\n output.suggestion = `Did you mean ${bestMatch}?`;\n }\n\n return output;\n }\n\n findBestMatch() {\n const {\n params: { allowedValues },\n } = this.options;\n\n const currentValue =\n this.instancePath === ''\n ? this.data\n : pointer.get(this.data, this.instancePath);\n\n if (!currentValue) {\n return null;\n }\n\n const bestMatch = allowedValues\n .map(value => ({\n value,\n weight: leven(value, currentValue.toString()),\n }))\n .sort((x, y) =>\n x.weight > y.weight ? 1 : x.weight < y.weight ? -1 : 0\n )[0];\n\n return allowedValues.length === 1 ||\n bestMatch.weight < bestMatch.value.length\n ? bestMatch.value\n : null;\n }\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AAEA,wCAAiD,oBAAoB;AAAA,EACnE,QAAQ;AACN,UAAM;AAAA,MACJ;AAAA,MACA,QAAQ,EAAE;AAAA,QACR,KAAK;AACT,U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