fix: Friendly error messages for null and undefined values inside message files (#1041)

Co-authored-by: windmillcode0 <michaelodumosu57@gmail.com>
This commit is contained in:
windmillcode0
2024-10-04 14:19:35 -04:00
committed by GitHub
parent faa4fcb718
commit 2c5ba64b61
2 changed files with 18 additions and 0 deletions
+13
View File
@@ -161,4 +161,17 @@ describe('Built Tools', () => {
);
expect(mockWriteFile).toBeCalledWith('output.d.ts', expectedDts, 'utf8');
});
it('should throw an error if messages file contains null or undefined', async () => {
const invalidFileContent = stringifyYAML({
simple: 'example',
invalidField: null,
});
mockReadFile.mockResolvedValue(invalidFileContent);
await expect(parseMessagesFile('invalid.yml')).rejects.toThrowError(
'Messages file should not contain `null` (found at "invalidField")',
);
});
});
+5
View File
@@ -166,6 +166,11 @@ function _parseMessagesObject(
];
}
case 'object':
if ([null, undefined].includes(object)) {
throw new Error(
`Messages file should not contain \`${object}\` (found at "${path.join('.')}")`,
);
}
if (Array.isArray(object))
return object.flatMap((item, i) =>
_parseMessagesObject(path.concat(String(i)), item, depth + 1),