Skip to content

Commit d562826

Browse files
committed
feat: Support parser options for customisation and performance in JSON schema parsing too
1 parent 1639356 commit d562826

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ const validate = validator.compile('string with JSON schema')
101101
validate(parser.parse('string with JSON data'))
102102
```
103103

104+
Compiling JSON schema supports the same options for customisation and performance improvement as parsing JSON data (`ignoreComments`, `allowSingleQuotedStrings`, `limitedErrorInfo`). They can be passed as the second (object) parameter. The optional second `environment` parameter can be passed as an additional property in the options object:
105+
106+
```js
107+
const validator = require('@prantlf/jsonlint/lib/validator')
108+
const validate = validator.compile('string with JSON schema', {
109+
limitedErrorInfo: true,
110+
environment: 'json-schema-draft-04'
111+
})
112+
validate(jsonData)
113+
```
114+
104115
### Performance
105116

106117
This is a part of [performance test results](./benchmarks/results/performance.md) of parsing a 3.8 KB formatted string ([package.json](./package,json)) with Node.js 10.15.3:

lib/cli.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,24 @@ if (options.compact) {
6666
}
6767

6868
function parse (source, file) {
69-
var parsed
70-
var formatted
69+
var parserOptions, parsed, formatted
7170
parsedFile = file
7271
try {
73-
parsed = parser.parse(source, {
72+
parserOptions = {
7473
ignoreComments: options.comments,
7574
allowSingleQuotedStrings: options.singleQuotedStrings,
7675
limitedErrorInfo: !(options.comments || options.singleQuotedStrings)
77-
})
76+
}
77+
parsed = parser.parse(source, parserOptions)
7878
if (options.sortKeys) {
7979
parsed = sorter.sortObject(parsed)
8080
}
8181
if (options.validate) {
8282
var validate
8383
try {
8484
var schema = fs.readFileSync(path.normalize(options.validate), 'utf8')
85-
validate = validator.compile(schema, options.environment)
85+
parserOptions.environment = options.environment
86+
validate = validator.compile(schema, parserOptions)
8687
} catch (error) {
8788
var message = 'Loading the JSON schema failed: "' +
8889
options.validate + '".\n' + error.message

lib/validator.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
'use strict'
3030

3131
function compile (schema, environment) {
32+
var options = {}
33+
if (typeof environment === 'object') {
34+
options = environment
35+
environment = options.environment
36+
}
3237
var ajv
3338
if (!environment) {
3439
ajv = new Ajv({ schemaId: 'auto' })
@@ -48,7 +53,11 @@
4853
}
4954
var validate
5055
try {
51-
schema = jsonlint.parse(schema)
56+
schema = jsonlint.parse(schema, {
57+
ignoreComments: options.ignoreComments,
58+
allowSingleQuotedStrings: options.allowSingleQuotedStrings,
59+
limitedErrorInfo: options.limitedErrorInfo
60+
})
5261
validate = ajv.compile(schema)
5362
} catch (error) {
5463
throw new Error('Compiling the JSON schema failed.\n' + error.message)

test/all-tests.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,15 @@ exports['test schema validation success'] = function () {
326326
exports['test schema validation failure'] = function () {
327327
var data = fs.readFileSync(path.join(__dirname, '/passes/3.schema.json')).toString()
328328
var schema = fs.readFileSync(path.join(__dirname, '/passes/3.schema.json')).toString()
329-
var validate = validator.compile(schema)
330-
assert['throws'](function () { validate(parser.parse(data)) }, 'should throw error')
329+
var validate = validator.compile(schema, {
330+
limitedErrorInfo: nativeParser,
331+
environment: 'json-schema-draft-04'
332+
})
333+
assert['throws'](function () {
334+
validate(parser.parse(data, {
335+
limitedErrorInfo: nativeParser
336+
}))
337+
}, 'should throw error')
331338
}
332339

333340
if (require.main === module) { require('test').run(exports) }

web/jsonlint.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,15 @@ <h2>Result</h2>
137137
document.getElementById('result-section').style.display = 'block'
138138
try {
139139
var parser = jsonlint.parser
140-
var parsed = parser.parse(document.getElementById('data').value, {
140+
var parserOptions = {
141141
ignoreComments: document.getElementById('comments').checked,
142-
allowSingleQuotedStrings: document.getElementById('single-quoted-strings').checked
143-
})
142+
allowSingleQuotedStrings: document.getElementById('single-quoted-strings').checked,
143+
limitedErrorInfo: !(options.comments || options.singleQuotedStrings)
144+
}
145+
var parsed = parser.parse(document.getElementById('data').value, parserOptions)
144146
if (document.getElementById('with-schema').checked) {
145147
var schema = document.getElementById('schema').value
146-
var validate = jsonlintValidator.compile(schema)
148+
var validate = jsonlintValidator.compile(schema, parserOptions)
147149
validate(parsed)
148150
}
149151
document.getElementById('result').innerText = 'Data is valid!'

0 commit comments

Comments
 (0)