Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add avjFixerOptions option to validate-schema #180

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
`
}
]
}));
Loading