Skip to content

Commit

Permalink
fix: add support for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
SherifMega committed Dec 14, 2022
1 parent b702ebf commit 6d98fb7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
12 changes: 5 additions & 7 deletions src/__snapshots__/valid-message-syntax.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,25 @@ exports[`Snapshot Tests for Invalid Code nested translations - icu syntax check
}"
`;

exports[`Snapshot Tests for Invalid Code no arrays or numbers 1`] = `
exports[`Snapshot Tests for Invalid Code no empty objects 1`] = `
"
- Expected
+ Received
Object {
- \\"levelOne\\": \\"ObjectContaining<ValidMessages> | ValidMessage<String>\\",
- \\"levelTwo\\": \\"ValidMessage<String>\\",
+ \\"levelOne\\": \\"Array [] ===> TypeError: An Array cannot be a translation value.\\",
+ \\"levelTwo\\": \\"Number(5) ===> Message must be a String.\\",
+ \\"levelOne\\": \\"Object {} ===> SyntaxError: Empty object.\\",
}"
`;

exports[`Snapshot Tests for Invalid Code no empty objects 1`] = `
exports[`Snapshot Tests for Invalid Code no numbers 1`] = `
"
- Expected
+ Received
Object {
- \\"levelOne\\": \\"ObjectContaining<ValidMessages> | ValidMessage<String>\\",
+ \\"levelOne\\": \\"Object {} ===> SyntaxError: Empty object.\\",
- \\"levelOne\\": \\"ValidMessage<String>\\",
+ \\"levelOne\\": \\"Number(5) ===> Message must be a String.\\",
}"
`;

Expand Down
11 changes: 0 additions & 11 deletions src/valid-message-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const getTranslationFileSource = require('./util/get-translation-file-source');

/* Error tokens */
const EMPTY_OBJECT = Symbol.for('EMPTY_OBJECT');
const ARRAY = Symbol.for('ARRAY');

/* Formatting */
const ALL_BACKSLASHES = /[\\]/g;
Expand All @@ -31,7 +30,6 @@ const prettyFormatTypePlugin = {
const formatExpectedValue = ({ value }) => {
switch (value) {
case EMPTY_OBJECT:
case ARRAY:
return 'ObjectContaining<ValidMessages> | ValidMessage<String>';
default:
return 'ValidMessage<String>';
Expand All @@ -45,8 +43,6 @@ const formatReceivedValue = ({ value, error }) => {
switch (value) {
case EMPTY_OBJECT:
return `${prettyFormat({})} ===> ${error}`;
case ARRAY:
return `${prettyFormat([])} ===> ${error}`;
default:
return `${prettyFormat(value, {
plugins: [prettyFormatTypePlugin]
Expand Down Expand Up @@ -140,13 +136,6 @@ const validMessageSyntax = (context, source) => {
error: new SyntaxError('Empty object.')
});
}
} else if (Array.isArray(value)) {
invalidMessages.push({
value: ARRAY,
key,
path,
error: new TypeError('An Array cannot be a translation value.')
});
} else {
try {
validate(value, key);
Expand Down
5 changes: 2 additions & 3 deletions src/valid-message-syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,11 @@ describe('Snapshot Tests for Invalid Code', () => {
});
expect(strip(errors[0].message)).toMatchSnapshot();
});
test('no arrays or numbers', () => {
test('no numbers', () => {
const errors = run({
code: `
/*{
"levelOne": [ "data" ],
"levelTwo": 5
"levelOne": 5
}*//*path/to/file.json*/
`,
options: [
Expand Down

0 comments on commit 6d98fb7

Please sign in to comment.