forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-schema-passes.js
80 lines (76 loc) · 2.16 KB
/
json-schema-passes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
const Result = require('../lib/result')
const Ajv = require('ajv')
// eslint-disable-next-line no-unused-vars
const FileSystem = require('../lib/file_system')
/**
* Check if a file matches a given JSON schema
*
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories
* @param {object} options The rule configuration
* @returns {Promise<Result>} The lint rule result
* @ignore
*/
async function jsonSchemaPasses(fs, options) {
const fileName = options.glob
const file = await fs.findFirstFile(options.glob, options.nocase)
if (file === undefined) {
return new Result(
'Did not find file matching the specified patterns',
[{ passed: false, pattern: fileName }],
!!options['succeed-on-non-existent']
)
}
// get the file contents
let fileContents = await fs.getFileContents(file)
if (fileContents === undefined) {
fileContents = ''
}
// parse them as JSON
let parsed
try {
parsed = JSON.parse(fileContents)
} catch (e) {
return new Result(
'',
[
{
path: file,
pattern: fileName,
passed: false,
message: `Failed to parse JSON with error ${e.toString()}`
}
],
false
)
}
// validate the JSON
const validator = new Ajv().compile(options.schema)
if (validator.errors) {
throw new Error(
`Failed to parse JSON schema with errors ${validator.errors
.map(e => `root${e.dataPath} ${e.message}`)
.join(', ')}`
)
}
const res = !!validator(parsed)
let message
if (options['human-readable-message']) {
message = res
? `${options['human-readable-message']} found in file`
: `${options['human-readable-message']} not found in file`
} else {
message = res
? 'JSON validation passed'
: `JSON validation failed with errors: ${validator.errors
.map(e => `root${e.dataPath} ${e.message}`)
.join(', ')}`
}
return new Result(
'',
[{ path: file, pattern: fileName, passed: res, message }],
res
)
}
module.exports = jsonSchemaPasses