Skip to content

Commit 6955583

Browse files
committed
feat: add validation of support element
- Add validation of support element. Previous validation was only for package-support.json and did not include validation of the support element in Package.json - Fix validation support to work on more than just 'true' for support - Add a number of validation tests Signed-off-by: Michael Dawson <[email protected]>
1 parent b291e92 commit 6955583

File tree

30 files changed

+439
-2
lines changed

30 files changed

+439
-2
lines changed

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,30 @@ const got = require('got')
77

88
let ajv
99
let compiledSchema
10+
let ajvElement
11+
let compiledSchemaElement
1012

1113
const schema = module.exports.schema = require('./schema.json')
14+
const supportElementSchema = module.exports.schema = require('./support-element-schema.json')
1215

1316
module.exports.defaultPackageSupport = 'package-support.json'
1417

18+
module.exports.validateSupportElement = (obj) => {
19+
if (!ajvElement) {
20+
ajvElement = new Ajv()
21+
compiledSchemaElement = ajvElement.compile(supportElementSchema)
22+
}
23+
24+
const validates = compiledSchemaElement(obj)
25+
if (!validates) {
26+
const err = new Error('Validation Failed')
27+
err.validationErrors = compiledSchemaElement.errors
28+
err.validationSchema = compiledSchemaElement.schema
29+
throw err
30+
}
31+
return true
32+
}
33+
1534
module.exports.validate = (obj) => {
1635
if (!ajv) {
1736
ajv = new Ajv()

lib/cli/commands/validate.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ module.exports = function (opts) {
2424
}
2525

2626
let supportData = userPackageJson.support
27-
if (supportData === true) {
27+
if ((supportData === false) || supportData) {
28+
// first validate that the supportData is correct
29+
try {
30+
support.validateSupportElement({ support: supportData })
31+
} catch (e) {
32+
argv.log.info('validate: ', e.validationErrors)
33+
return
34+
}
35+
36+
// support element was ok so validate the data itself
2837
supportData = await support.getSupportData(userPackageJson,
2938
path.join(process.cwd()),
3039
argv.canonical,
@@ -39,7 +48,7 @@ module.exports = function (opts) {
3948
return
4049
}
4150
} catch (e) {
42-
argv.log.error('validate: ', e.validationErrors)
51+
argv.log.info('validate: ', e.validationErrors)
4352
}
4453
} else {
4554
argv.log.info('support info not resolved: ' + supportData.url)

support-element-schema.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$id": "https://raw.githubusercontent.com/pkgjs/support/master/support-element-schema.json",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"title": "Package Support Package.json element",
5+
6+
"type": "object",
7+
"properties": {
8+
"support": {
9+
"oneOf": [{
10+
"enum": [ true ]
11+
}, {
12+
"type": "string"
13+
}, {
14+
"type": "object",
15+
"required": ["repository"],
16+
"properties": {
17+
"repository": { "$ref": "#Repository" },
18+
"path": { "type": "string" }
19+
},
20+
"additionalProperties": false
21+
}]
22+
}
23+
},
24+
25+
"definitions": {
26+
"repository": {
27+
"$id": "#Repository",
28+
"type": "object",
29+
"required": ["type", "url"],
30+
"properties": {
31+
"type": { "enum": [ "git" ] },
32+
"url": { "type": "string" },
33+
"directory": { "type": "string" }
34+
},
35+
"additionalProperties": false
36+
}
37+
}
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
validate
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
info › validate:
2+
- 0: {
3+
keyword: 'enum',
4+
dataPath: '.support',
5+
schemaPath: '#/properties/support/oneOf/0/enum',
6+
params: { allowedValues: [ true ] },
7+
message: 'should be equal to one of the allowed values'
8+
}
9+
- 1: {
10+
keyword: 'type',
11+
dataPath: '.support',
12+
schemaPath: '#/properties/support/oneOf/1/type',
13+
params: { type: 'string' },
14+
message: 'should be string'
15+
}
16+
- 2: {
17+
keyword: 'type',
18+
dataPath: '.support',
19+
schemaPath: '#/properties/support/oneOf/2/type',
20+
params: { type: 'object' },
21+
message: 'should be object'
22+
}
23+
- 3: {
24+
keyword: 'oneOf',
25+
dataPath: '.support',
26+
schemaPath: '#/properties/support/oneOf',
27+
params: { passingSchemas: null },
28+
message: 'should match exactly one schema in oneOf'
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@pkgjs/support-show-local",
3+
"version": "0.0.1",
4+
"support": false,
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/pkgjs/support.git"
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
validate
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
info › validate:
2+
- 0: {
3+
keyword: 'enum',
4+
dataPath: '.support',
5+
schemaPath: '#/properties/support/oneOf/0/enum',
6+
params: { allowedValues: [ true ] },
7+
message: 'should be equal to one of the allowed values'
8+
}
9+
- 1: {
10+
keyword: 'type',
11+
dataPath: '.support',
12+
schemaPath: '#/properties/support/oneOf/1/type',
13+
params: { type: 'string' },
14+
message: 'should be string'
15+
}
16+
- 2: {
17+
keyword: 'type',
18+
dataPath: '.support',
19+
schemaPath: '#/properties/support/oneOf/2/type',
20+
params: { type: 'object' },
21+
message: 'should be object'
22+
}
23+
- 3: {
24+
keyword: 'oneOf',
25+
dataPath: '.support',
26+
schemaPath: '#/properties/support/oneOf',
27+
params: { passingSchemas: null },
28+
message: 'should match exactly one schema in oneOf'
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@pkgjs/support-show-local",
3+
"version": "0.0.1",
4+
"support": 1235,
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/pkgjs/support.git"
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
validate

0 commit comments

Comments
 (0)