Skip to content

Commit

Permalink
Merge pull request #180 from kellyselden/avjFixerOptions
Browse files Browse the repository at this point in the history
feat: add avjFixerOptions option to validate-schema
  • Loading branch information
kellyselden authored Aug 21, 2024
2 parents df3ffc7 + c4b0303 commit d384e25
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/rules/validate-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ An options object of:

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

## When Not To Use It

Expand Down
15 changes: 14 additions & 1 deletion lib/rules/validate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Ajv = require('ajv');
const { default: betterAjvErrors } = require('better-ajv-errors');
const { EOL } = require('os');

let ajv;

Expand All @@ -10,6 +11,7 @@ module.exports = {
docs: {
description: 'require a valid JSON Schema'
},
fixable: 'code',
schema: [
{
'type': 'object',
Expand All @@ -20,6 +22,10 @@ module.exports = {
'prettyErrors': {
'type': 'boolean',
'default': true
},
// https://ajv.js.org/options.html#options-to-modify-validated-data
'avjFixerOptions': {
'type': 'object'
}
},
'additionalProperties': false
Expand Down Expand Up @@ -65,7 +71,14 @@ module.exports = {

context.report({
node: packageJsonNode,
message
message,
fix(fixer) {
let ajvFix = new Ajv(options.avjFixerOptions);
let validate = ajvFix.compile(schema);
validate(packageJson);

return fixer.replaceText(packageJsonNode, JSON.stringify(packageJson, null, 2) + EOL);
}
});
}
}
Expand Down
38 changes: 36 additions & 2 deletions tests/lib/rules/validate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ new RuleTester().run('validate-schema', rule, preprocess({
errors: [{
message: color('NOT must NOT be valid\n\n> 1 | {"foo":"bar"}\n  | ^^^^^^^^^^^^^ 👈🏽 not must NOT be valid'),
type: 'ObjectExpression'
}]
}],
output: `{
"foo": "bar"
}
`
},
{
code: '{"foo":"bar"}',
Expand All @@ -56,7 +60,37 @@ new RuleTester().run('validate-schema', rule, preprocess({
errors: [{
message: '#/not must NOT be valid',
type: 'ObjectExpression'
}]
}],
output: `{
"foo": "bar"
}
`
},
{
code: '{"foo":"bar","bar":"foo"}',
options: [{
schema: schema({
'type': 'object',
'properties': {
'foo': {
'const': 'bar'
}
},
'additionalProperties': false
}),
prettyErrors: false,
avjFixerOptions: {
removeAdditional: true
}
}],
errors: [{
message: '#/additionalProperties must NOT have additional properties',
type: 'ObjectExpression'
}],
output: `{
"foo": "bar"
}
`
}
]
}));

0 comments on commit d384e25

Please sign in to comment.