Skip to content

Commit 55e559f

Browse files
committed
feat: add options to disable emojis
1 parent 70356a5 commit 55e559f

File tree

9 files changed

+56
-7
lines changed

9 files changed

+56
-7
lines changed

β€Žsrc/__tests__/__snapshots__/index.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ exports[`Main should output error with reconstructed codeframe 1`] = `
2424
  8 | ]
2525
  9 | }"
2626
`;
27+
28+
exports[`Main should output error without emojis 1`] = `
29+
"ENUM must be equal to one of the allowed values
30+
(paragraph, codeBlock, blockquote)
31+
32+
  2 | \\"type\\": \\"doc\\",
33+
  3 | \\"version\\": 1,
34+
> 4 | \\"content\\": [{ \\"type\\": \\"paragarph\\" }]
35+
  | ^^^^^^^^^^^ Did you mean paragraph here?
36+
  5 | }
37+
  6 |"
38+
`;

β€Žsrc/__tests__/helpers/create-error-instances.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('createErrorInstances', () => {
4141
},
4242
},
4343
"schema": undefined,
44+
"showEmojis": true,
4445
},
4546
]
4647
`);

β€Žsrc/__tests__/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@ describe('Main', () => {
3030
});
3131
expect(res).toMatchSnapshot();
3232
});
33+
34+
it('should output error without emojis', async () => {
35+
const [schema, data, json] = await getSchemaAndData('default', __dirname);
36+
const ajv = new Ajv();
37+
const validate = ajv.compile(schema);
38+
const valid = validate(data);
39+
expect(valid).toBeFalsy();
40+
41+
const res = betterAjvErrors(schema, data, validate.errors, {
42+
format: 'cli',
43+
json,
44+
showEmojis: false,
45+
});
46+
expect(res).toMatchSnapshot();
47+
});
3348
});

β€Žsrc/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { parse } from '@humanwhocodes/momoa';
22
import prettify from './helpers';
33

44
export default (schema, data, errors, options = {}) => {
5-
const { format = 'cli', indent = null, json = null } = options;
5+
const {
6+
format = 'cli',
7+
indent = null,
8+
json = null,
9+
showEmojis = true,
10+
} = options;
611

712
const jsonRaw = json || JSON.stringify(data, null, indent);
813
const jsonAst = parse(jsonRaw);
@@ -14,6 +19,7 @@ export default (schema, data, errors, options = {}) => {
1419
schema,
1520
jsonAst,
1621
jsonRaw,
22+
showEmojis,
1723
});
1824

1925
if (format === 'cli') {

β€Žsrc/validation-errors/additional-prop.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export default class AdditionalPropValidationError extends BaseValidationError {
1313

1414
return output.concat(
1515
this.getCodeFrame(
16-
chalk`😲 {magentaBright ${params.additionalProperty}} is not expected to be here!`,
16+
chalk`${this.showEmoji('😲')}{magentaBright ${
17+
params.additionalProperty
18+
}} is not expected to be here!`,
1719
`${this.instancePath}/${params.additionalProperty}`
1820
)
1921
);

β€Žsrc/validation-errors/base.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import { getMetaFromPath, getDecoratedDataPath } from '../json/index';
44
export default class BaseValidationError {
55
constructor(
66
options = { isIdentifierLocation: false },
7-
{ data, schema, jsonAst, jsonRaw }
7+
{ data, schema, jsonAst, jsonRaw, showEmojis = true }
88
) {
99
this.options = options;
1010
this.data = data;
1111
this.schema = schema;
1212
this.jsonAst = jsonAst;
1313
this.jsonRaw = jsonRaw;
14+
this.showEmojis = showEmojis;
15+
}
16+
17+
showEmoji(emoji) {
18+
return this.showEmojis ? `${emoji} ` : '';
1419
}
1520

1621
getLocation(dataPath = this.instancePath) {

β€Žsrc/validation-errors/default.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export default class DefaultValidationError extends BaseValidationError {
77
const output = [chalk`{red {bold ${keyword.toUpperCase()}} ${message}}\n`];
88

99
return output.concat(
10-
this.getCodeFrame(chalk`πŸ‘ˆπŸ½ {magentaBright ${keyword}} ${message}`)
10+
this.getCodeFrame(
11+
chalk`${this.showEmoji('πŸ‘ˆπŸ½')}{magentaBright ${keyword}} ${message}`
12+
)
1113
);
1214
}
1315

β€Žsrc/validation-errors/enum.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ export default class EnumValidationError extends BaseValidationError {
1919
return output.concat(
2020
this.getCodeFrame(
2121
bestMatch !== null
22-
? chalk`πŸ‘ˆπŸ½ Did you mean {magentaBright ${bestMatch}} here?`
23-
: chalk`πŸ‘ˆπŸ½ Unexpected value, should be equal to one of the allowed values`
22+
? chalk`${this.showEmoji(
23+
'πŸ‘ˆπŸ½'
24+
)}Did you mean {magentaBright ${bestMatch}} here?`
25+
: chalk`${this.showEmoji(
26+
'πŸ‘ˆπŸ½'
27+
)}Unexpected value, should be equal to one of the allowed values`
2428
)
2529
);
2630
}

β€Žsrc/validation-errors/required.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export default class RequiredValidationError extends BaseValidationError {
1313

1414
return output.concat(
1515
this.getCodeFrame(
16-
chalk`☹️ {magentaBright ${params.missingProperty}} is missing here!`
16+
chalk`${this.showEmoji('☹️')}{magentaBright ${
17+
params.missingProperty
18+
}} is missing here!`
1719
)
1820
);
1921
}

0 commit comments

Comments
Β (0)