Skip to content

Commit c4b0303

Browse files
author
Kelly Selden
committed
feat: add avjFixerOptions option to validate-schema
1 parent df3ffc7 commit c4b0303

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

docs/rules/validate-schema.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ An options object of:
4747

4848
* `"schema"` a string of your JSON Schema
4949
* `"prettyErrors"` on by default. Set this to false if you want more machine-readable errors.
50+
* `"avjFixerOptions"` if you want to autofix some issues. Use this for supported fixers https://ajv.js.org/options.html#options-to-modify-validated-data.
5051

5152
## When Not To Use It
5253

lib/rules/validate-schema.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Ajv = require('ajv');
44
const { default: betterAjvErrors } = require('better-ajv-errors');
5+
const { EOL } = require('os');
56

67
let ajv;
78

@@ -10,6 +11,7 @@ module.exports = {
1011
docs: {
1112
description: 'require a valid JSON Schema'
1213
},
14+
fixable: 'code',
1315
schema: [
1416
{
1517
'type': 'object',
@@ -20,6 +22,10 @@ module.exports = {
2022
'prettyErrors': {
2123
'type': 'boolean',
2224
'default': true
25+
},
26+
// https://ajv.js.org/options.html#options-to-modify-validated-data
27+
'avjFixerOptions': {
28+
'type': 'object'
2329
}
2430
},
2531
'additionalProperties': false
@@ -65,7 +71,14 @@ module.exports = {
6571

6672
context.report({
6773
node: packageJsonNode,
68-
message
74+
message,
75+
fix(fixer) {
76+
let ajvFix = new Ajv(options.avjFixerOptions);
77+
let validate = ajvFix.compile(schema);
78+
validate(packageJson);
79+
80+
return fixer.replaceText(packageJsonNode, JSON.stringify(packageJson, null, 2) + EOL);
81+
}
6982
});
7083
}
7184
}

tests/lib/rules/validate-schema.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ new RuleTester().run('validate-schema', rule, preprocess({
4242
errors: [{
4343
message: color('NOT must NOT be valid\n\n> 1 | {"foo":"bar"}\n  | ^^^^^^^^^^^^^ 👈🏽 not must NOT be valid'),
4444
type: 'ObjectExpression'
45-
}]
45+
}],
46+
output: `{
47+
"foo": "bar"
48+
}
49+
`
4650
},
4751
{
4852
code: '{"foo":"bar"}',
@@ -56,7 +60,37 @@ new RuleTester().run('validate-schema', rule, preprocess({
5660
errors: [{
5761
message: '#/not must NOT be valid',
5862
type: 'ObjectExpression'
59-
}]
63+
}],
64+
output: `{
65+
"foo": "bar"
66+
}
67+
`
68+
},
69+
{
70+
code: '{"foo":"bar","bar":"foo"}',
71+
options: [{
72+
schema: schema({
73+
'type': 'object',
74+
'properties': {
75+
'foo': {
76+
'const': 'bar'
77+
}
78+
},
79+
'additionalProperties': false
80+
}),
81+
prettyErrors: false,
82+
avjFixerOptions: {
83+
removeAdditional: true
84+
}
85+
}],
86+
errors: [{
87+
message: '#/additionalProperties must NOT have additional properties',
88+
type: 'ObjectExpression'
89+
}],
90+
output: `{
91+
"foo": "bar"
92+
}
93+
`
6094
}
6195
]
6296
}));

0 commit comments

Comments
 (0)