diff --git a/.changeset/violet-tools-poke.md b/.changeset/violet-tools-poke.md new file mode 100644 index 00000000..c299f743 --- /dev/null +++ b/.changeset/violet-tools-poke.md @@ -0,0 +1,5 @@ +--- +'better-ajv-errors': minor +--- + +add option to disable emojis diff --git a/src/__tests__/__snapshots__/index.js.snap b/src/__tests__/__snapshots__/index.js.snap index 7ad4364b..8baf89a8 100644 --- a/src/__tests__/__snapshots__/index.js.snap +++ b/src/__tests__/__snapshots__/index.js.snap @@ -24,3 +24,15 @@ exports[`Main should output error with reconstructed codeframe 1`] = `   8 | ]   9 | }" `; + +exports[`Main should output error without emojis 1`] = ` +"ENUM must be equal to one of the allowed values +(paragraph, codeBlock, blockquote) + +  2 | \\"type\\": \\"doc\\", +  3 | \\"version\\": 1, +> 4 | \\"content\\": [{ \\"type\\": \\"paragarph\\" }] +  | ^^^^^^^^^^^ Did you mean paragraph here? +  5 | } +  6 |" +`; diff --git a/src/__tests__/helpers/create-error-instances.js b/src/__tests__/helpers/create-error-instances.js index 92623783..fdb19b70 100644 --- a/src/__tests__/helpers/create-error-instances.js +++ b/src/__tests__/helpers/create-error-instances.js @@ -41,6 +41,7 @@ describe('createErrorInstances', () => { }, }, "schema": undefined, + "showEmojis": true, }, ] `); diff --git a/src/__tests__/index.js b/src/__tests__/index.js index a3adc6d2..7c99156a 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -30,4 +30,19 @@ describe('Main', () => { }); expect(res).toMatchSnapshot(); }); + + it('should output error without emojis', async () => { + const [schema, data, json] = await getSchemaAndData('default', __dirname); + const ajv = new Ajv(); + const validate = ajv.compile(schema); + const valid = validate(data); + expect(valid).toBeFalsy(); + + const res = betterAjvErrors(schema, data, validate.errors, { + format: 'cli', + json, + showEmojis: false, + }); + expect(res).toMatchSnapshot(); + }); }); diff --git a/src/index.js b/src/index.js index 43494fe1..9038c6e4 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,12 @@ import { parse } from '@humanwhocodes/momoa'; import prettify from './helpers'; export default (schema, data, errors, options = {}) => { - const { format = 'cli', indent = null, json = null } = options; + const { + format = 'cli', + indent = null, + json = null, + showEmojis = true, + } = options; const jsonRaw = json || JSON.stringify(data, null, indent); const jsonAst = parse(jsonRaw); @@ -14,6 +19,7 @@ export default (schema, data, errors, options = {}) => { schema, jsonAst, jsonRaw, + showEmojis, }); if (format === 'cli') { diff --git a/src/validation-errors/additional-prop.js b/src/validation-errors/additional-prop.js index c680f8a5..e4e8ba70 100644 --- a/src/validation-errors/additional-prop.js +++ b/src/validation-errors/additional-prop.js @@ -13,7 +13,9 @@ export default class AdditionalPropValidationError extends BaseValidationError { return output.concat( this.getCodeFrame( - chalk`😲 {magentaBright ${params.additionalProperty}} is not expected to be here!`, + chalk`${this.showEmoji('😲')}{magentaBright ${ + params.additionalProperty + }} is not expected to be here!`, `${this.instancePath}/${params.additionalProperty}` ) ); diff --git a/src/validation-errors/base.js b/src/validation-errors/base.js index 703f79ce..00790b4d 100644 --- a/src/validation-errors/base.js +++ b/src/validation-errors/base.js @@ -4,13 +4,18 @@ import { getMetaFromPath, getDecoratedDataPath } from '../json/index'; export default class BaseValidationError { constructor( options = { isIdentifierLocation: false }, - { data, schema, jsonAst, jsonRaw } + { data, schema, jsonAst, jsonRaw, showEmojis = true } ) { this.options = options; this.data = data; this.schema = schema; this.jsonAst = jsonAst; this.jsonRaw = jsonRaw; + this.showEmojis = showEmojis; + } + + showEmoji(emoji) { + return this.showEmojis ? `${emoji} ` : ''; } getLocation(dataPath = this.instancePath) { diff --git a/src/validation-errors/default.js b/src/validation-errors/default.js index a00df894..cf58077f 100644 --- a/src/validation-errors/default.js +++ b/src/validation-errors/default.js @@ -7,7 +7,9 @@ export default class DefaultValidationError extends BaseValidationError { const output = [chalk`{red {bold ${keyword.toUpperCase()}} ${message}}\n`]; return output.concat( - this.getCodeFrame(chalk`👈đŸŊ {magentaBright ${keyword}} ${message}`) + this.getCodeFrame( + chalk`${this.showEmoji('👈đŸŊ')}{magentaBright ${keyword}} ${message}` + ) ); } diff --git a/src/validation-errors/enum.js b/src/validation-errors/enum.js index 22d8d192..3dabe532 100644 --- a/src/validation-errors/enum.js +++ b/src/validation-errors/enum.js @@ -19,8 +19,12 @@ export default class EnumValidationError extends BaseValidationError { return output.concat( this.getCodeFrame( bestMatch !== null - ? chalk`👈đŸŊ Did you mean {magentaBright ${bestMatch}} here?` - : chalk`👈đŸŊ Unexpected value, should be equal to one of the allowed values` + ? chalk`${this.showEmoji( + '👈đŸŊ' + )}Did you mean {magentaBright ${bestMatch}} here?` + : chalk`${this.showEmoji( + '👈đŸŊ' + )}Unexpected value, should be equal to one of the allowed values` ) ); } diff --git a/src/validation-errors/required.js b/src/validation-errors/required.js index 9a438fc0..23b2acba 100644 --- a/src/validation-errors/required.js +++ b/src/validation-errors/required.js @@ -13,7 +13,9 @@ export default class RequiredValidationError extends BaseValidationError { return output.concat( this.getCodeFrame( - chalk`☚ī¸ {magentaBright ${params.missingProperty}} is missing here!` + chalk`${this.showEmoji('☚ī¸')}{magentaBright ${ + params.missingProperty + }} is missing here!` ) ); }