Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to disable emojis #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-tools-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'better-ajv-errors': minor
---

add option to disable emojis
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 |"
`;
1 change: 1 addition & 0 deletions src/__tests__/helpers/create-error-instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('createErrorInstances', () => {
},
},
"schema": undefined,
"showEmojis": true,
},
]
`);
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -14,6 +19,7 @@ export default (schema, data, errors, options = {}) => {
schema,
jsonAst,
jsonRaw,
showEmojis,
});

if (format === 'cli') {
Expand Down
4 changes: 3 additions & 1 deletion src/validation-errors/additional-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
);
Expand Down
7 changes: 6 additions & 1 deletion src/validation-errors/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/validation-errors/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/validation-errors/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/validation-errors/required.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!`
)
);
}
Expand Down