Skip to content

Commit d11a42e

Browse files
committed
feat: improve validation errors
Signed-off-by: Michael Dawson <[email protected]>
1 parent 7b5b631 commit d11a42e

File tree

11 files changed

+130
-239
lines changed

11 files changed

+130
-239
lines changed

β€Žindex.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs')
44
const normalizeUrl = require('normalize-url')
55
const Ajv = require('ajv')
66
const got = require('got')
7+
const betterAjvErrors = require('better-ajv-errors')
78

89
let ajv
910
let compiledSchema
@@ -17,14 +18,15 @@ module.exports.defaultPackageSupport = 'package-support.json'
1718

1819
module.exports.validateSupportElement = (obj) => {
1920
if (!ajvElement) {
20-
ajvElement = new Ajv()
21+
ajvElement = new Ajv({ jsonPointers: true })
2122
compiledSchemaElement = ajvElement.compile(supportElementSchema)
2223
}
2324

2425
const validates = compiledSchemaElement(obj)
2526
if (!validates) {
2627
const err = new Error('Validation Failed')
2728
err.validationErrors = compiledSchemaElement.errors
29+
err.prettyValidationErrors = betterAjvErrors(supportElementSchema, obj, compiledSchemaElement.errors)
2830
err.validationSchema = compiledSchemaElement.schema
2931
throw err
3032
}
@@ -33,14 +35,15 @@ module.exports.validateSupportElement = (obj) => {
3335

3436
module.exports.validate = (obj) => {
3537
if (!ajv) {
36-
ajv = new Ajv()
38+
ajv = new Ajv({ jsonPointers: true })
3739
compiledSchema = ajv.compile(schema)
3840
}
3941

4042
const validates = compiledSchema(obj)
4143
if (!validates) {
4244
const err = new Error('Validation Failed')
4345
err.validationErrors = compiledSchema.errors
46+
err.prettyValidationErrors = betterAjvErrors(schema, obj, compiledSchema.errors)
4447
err.validationSchema = compiledSchema.schema
4548
throw err
4649
}

β€Žlib/cli/commands/validate.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const fs = require('fs-extra')
44
const path = require('path')
55
const support = require('../../../index.js')
66

7+
const errorInSupportElement = 'Error in support element in package.json'
8+
79
module.exports = function (opts) {
810
return {
911
command: 'validate',
@@ -26,11 +28,18 @@ module.exports = function (opts) {
2628
let supportData = userPackageJson.support
2729
if ((supportData === false) || supportData) {
2830
// first validate that the supportData is correct
29-
try {
30-
support.validateSupportElement({ support: supportData })
31-
} catch (e) {
32-
argv.log.info('validate: ', e.validationErrors)
31+
if ((supportData !== true) && !(supportData instanceof Object)) {
32+
console.log(errorInSupportElement)
33+
console.log('Support element should either be true or an Object matching the required schema')
3334
return
35+
} else {
36+
try {
37+
support.validateSupportElement({ support: supportData })
38+
} catch (e) {
39+
console.log(errorInSupportElement)
40+
console.log(e.prettyValidationErrors)
41+
return
42+
}
3443
}
3544

3645
// support element was ok so validate the data itself
@@ -48,7 +57,7 @@ module.exports = function (opts) {
4857
return
4958
}
5059
} catch (e) {
51-
argv.log.info('validate: ', e.validationErrors)
60+
argv.log.info('validate: ', e.prettyValidationErrors)
5261
}
5362
} else {
5463
argv.log.info('support info not resolved: ' + supportData.url)

β€Žpackage.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"dependencies": {
2828
"@npmcli/arborist": "0.0.0-pre.19",
2929
"ajv": "^6.11.0",
30+
"better-ajv-errors": "^0.6.7",
3031
"got": "^11.1.3",
3132
"loggerr": "^3.0.0-0",
3233
"normalize-url": "^5.0.0",
Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,2 @@
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-
}
1+
Error in support element in package.json
2+
Support element should either be true or an Object matching the required schema
Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,2 @@
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-
}
1+
Error in support element in package.json
2+
Support element should either be true or an Object matching the required schema
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
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: 'required',
18-
dataPath: '.support',
19-
schemaPath: '#/properties/support/oneOf/2/required',
20-
params: { missingProperty: 'repository' },
21-
message: "should have required property 'repository'"
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-
}
1+
Error in support element in package.json
2+
REQUIRED should have required property 'repository'
3+
4+
> 1 | {"support":{}}
5+
| ^ ☹️ repository is missing here!
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
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: 'enum',
18-
dataPath: '.support.repository.type',
19-
schemaPath: '#Repository/properties/type/enum',
20-
params: { allowedValues: [ 'git' ] },
21-
message: 'should be equal to one of the allowed values'
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-
}
1+
Error in support element in package.json
2+
ENUM should be equal to one of the allowed values
3+
4+
> 1 | {"support":{"repository":{"type":"xxx","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ enum should be equal to one of the allowed values
6+
7+
TYPE should be string
8+
9+
> 1 | {"support":{"repository":{"type":"xxx","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ type should be string
11+
12+
ONEOF should match exactly one schema in oneOf
13+
14+
> 1 | {"support":{"repository":{"type":"xxx","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ oneOf should match exactly one schema in oneOf
16+
17+
ENUM should be equal to one of the allowed values
18+
(git)
19+
20+
> 1 | {"support":{"repository":{"type":"xxx","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
21+
| ^^^^^ πŸ‘ˆπŸ½ Did you mean git here?
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
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: 'required',
18-
dataPath: '.support.repository',
19-
schemaPath: '#Repository/required',
20-
params: { missingProperty: 'url' },
21-
message: "should have required property 'url'"
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-
}
1+
Error in support element in package.json
2+
ENUM should be equal to one of the allowed values
3+
4+
> 1 | {"support":{"repository":{"type":"git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ enum should be equal to one of the allowed values
6+
7+
TYPE should be string
8+
9+
> 1 | {"support":{"repository":{"type":"git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ type should be string
11+
12+
ONEOF should match exactly one schema in oneOf
13+
14+
> 1 | {"support":{"repository":{"type":"git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ oneOf should match exactly one schema in oneOf
16+
17+
REQUIRED should have required property 'url'
18+
19+
> 1 | {"support":{"repository":{"type":"git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
20+
| ^ ☹️ url is missing here!
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
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: 'required',
18-
dataPath: '.support.repository',
19-
schemaPath: '#Repository/required',
20-
params: { missingProperty: 'type' },
21-
message: "should have required property 'type'"
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-
}
1+
Error in support element in package.json
2+
ENUM should be equal to one of the allowed values
3+
4+
> 1 | {"support":{"repository":{"url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ enum should be equal to one of the allowed values
6+
7+
TYPE should be string
8+
9+
> 1 | {"support":{"repository":{"url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ type should be string
11+
12+
ONEOF should match exactly one schema in oneOf
13+
14+
> 1 | {"support":{"repository":{"url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ oneOf should match exactly one schema in oneOf
16+
17+
REQUIRED should have required property 'type'
18+
19+
> 1 | {"support":{"repository":{"url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2"},"path":"./MySupportPackage.json"}}
20+
| ^ ☹️ type is missing here!
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
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: 'additionalProperties',
18-
dataPath: '.support.repository',
19-
schemaPath: '#Repository/additionalProperties',
20-
params: { additionalProperty: 'extra' },
21-
message: 'should NOT have additional properties'
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-
}
1+
Error in support element in package.json
2+
ENUM should be equal to one of the allowed values
3+
4+
> 1 | {"support":{"repository":{"type":"git","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2","extra":"123"},"path":"./MySupportPackage.json"}}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ enum should be equal to one of the allowed values
6+
7+
TYPE should be string
8+
9+
> 1 | {"support":{"repository":{"type":"git","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2","extra":"123"},"path":"./MySupportPackage.json"}}
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ type should be string
11+
12+
ONEOF should match exactly one schema in oneOf
13+
14+
> 1 | {"support":{"repository":{"type":"git","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2","extra":"123"},"path":"./MySupportPackage.json"}}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ πŸ‘ˆπŸ½ oneOf should match exactly one schema in oneOf
16+
17+
ADDTIONAL PROPERTY should NOT have additional properties
18+
19+
> 1 | {"support":{"repository":{"type":"git","url":"https://github.com/pkgjs/support-separate-repo-other.git","directory":"subdir2","extra":"123"},"path":"./MySupportPackage.json"}}
20+
| ^^^^^^^ 😲 extra is not expected to be here!

0 commit comments

Comments
Β (0)