Skip to content

Commit 5d4e278

Browse files
committed
fix(cli-validator): allow empty-string JSON keys during duplicate-key check
Signed-off-by: Sergio <c@rct.ai>
1 parent 46a8b7d commit 5d4e278

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

packages/validator/src/cli-validator/run-validator.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ async function runValidator(cliArgs, parseOptions = {}) {
212212
// jsonValidator looks through the originalFile for duplicate JSON keys
213213
// this is checked for by default in readYaml
214214
const duplicateKeysError = jsonValidator.validate(originalFile);
215-
if (fileExtension === 'json' && duplicateKeysError) {
215+
if (
216+
fileExtension === 'json' &&
217+
duplicateKeysError &&
218+
!duplicateKeysError.startsWith('Syntax error: empty string near')
219+
) {
216220
throw duplicateKeysError;
217221
}
218222

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Empty string key test",
5+
"version": "1.0.0"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"Sample": {
11+
"type": "object",
12+
"properties": {
13+
"": {
14+
"type": "string"
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}

packages/validator/test/cli-validator/tests/error-handling.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ describe('cli tool - test error handling', function () {
158158
);
159159
});
160160

161+
it('should not reject valid JSON that uses an empty string key', async function () {
162+
let exitCode;
163+
try {
164+
exitCode = await testValidator([
165+
'./test/cli-validator/mock-files/oas3/empty-string-key.json',
166+
]);
167+
} catch (err) {
168+
exitCode = err;
169+
}
170+
171+
const capturedText = getCapturedText(consoleSpy.mock.calls);
172+
173+
expect(exitCode).toEqual(0);
174+
expect(capturedText.join('\n')).not.toContain('Syntax error: empty string near');
175+
});
176+
161177
it('should return an error when a JSON document contains a trailing comma', async function () {
162178
let exitCode;
163179
try {

0 commit comments

Comments
 (0)