Skip to content

Commit 1f75daf

Browse files
authored
Merge pull request #88 from ralfhandl/patch-1
Support JSONC
2 parents d53d73e + 868fee7 commit 1f75daf

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "4.1.0",
44
"description": "Command line interface for Ajv JSON schema validator",
55
"scripts": {
6-
"build": "rm -rf dist && tsc",
6+
"build": "rimraf dist && tsc",
77
"prepublish": "npm run build",
88
"eslint": "eslint \"src/**/*.*s\" \"test/**/*.js\"",
99
"prettier:write": "prettier --write \"./**/*.{md,json,yaml,js,ts}\"",
@@ -63,6 +63,7 @@
6363
"nyc": "^15.0.1",
6464
"pre-commit": "^1.2.0",
6565
"prettier": "^2.2.1",
66+
"rimraf": "^3.0.2",
6667
"ts-node": "^9.1.0",
6768
"typescript": "^4.1.2"
6869
},

src/commands/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function decodeFile(contents: string, format: string): any {
3030
switch (format) {
3131
case "json":
3232
return JSON.parse(contents)
33+
case "jsonc":
3334
case "json5":
3435
return JSON5.parse(contents)
3536
case "yml":

test/valid_data.jsonc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
// with comments
3+
{
4+
"id": 2,
5+
"name": "An ice sculpture",
6+
"price": 12.50,
7+
"tags": ["cold", "ice"],
8+
"dimensions": {
9+
"length": 7.0,
10+
"width": 12.0,
11+
"height": 9.5
12+
},
13+
"warehouseLocation": {
14+
"latitude": -78.75,
15+
"longitude": 20.4
16+
}
17+
},
18+
{
19+
"id": 3,
20+
"name": "A blue mouse",
21+
"price": 25.50,
22+
"dimensions": {
23+
"length": 3.1,
24+
"width": 1.0,
25+
"height": 1.0
26+
},
27+
"warehouseLocation": {
28+
"latitude": 54.4,
29+
"longitude": -32.7
30+
}
31+
}
32+
]

test/validate.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ describe("validate", function () {
4242
})
4343
})
4444

45+
it('should validate valid data with the "jsonc" extension', (done) => {
46+
cli("-s test/schema -d test/valid_data.jsonc", (error, stdout, stderr) => {
47+
assert.strictEqual(error, null)
48+
assertValid(stdout, 1)
49+
assert.strictEqual(stderr, "")
50+
done()
51+
})
52+
})
53+
4554
it("falls back to require on unsupported formats", (done) => {
4655
cli(
4756
"-s test/schema.json -d test/invalid_format.cson --errors=line",

0 commit comments

Comments
 (0)