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 json schema validation to aglintrc.json file for autocompletion #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
.DS_Store
.eslintcache
.vscode
.idea
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore InteliJ IDEA configuration files

*.iml
coverage
dist
node_modules
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/text-table": "^0.2.2",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"ajv": "^8.16.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev dependency for tests

"ci-info": "^4.0.0",
"eslint": "^8.43.0",
"eslint-config-airbnb-typescript": "^17.0.0",
Expand Down
63 changes: 63 additions & 0 deletions src/linter/aglintrc-json-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"severity": {
"oneOf": [
{
"type": "string",
"enum": ["off", "warn", "error", "fatal"]
},
{
"type": "number",
"enum": [0, 1, 2, 3]
}
]
}
},
"properties": {
"root": {
"type": "boolean"
},
"extends": {
"type": "array",
"items": {
"type": "string"
}
},
"allowInlineConfig": {
"type": "boolean"
},
"syntax": {
"type": "array",
"items": {
"type": "string",
"enum": ["Common", "AdblockPlus", "UblockOrigin", "AdGuard"]
}
},
"rules": {
"type": "object",
"patternProperties": {
".*": {
"oneOf": [
{
"$ref": "#/definitions/severity"
},
{
"type": "array",
"minItems": 1,
"items": [
{
"$ref": "#/definitions/severity"
},
{}
]
}
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
39 changes: 27 additions & 12 deletions test/linter/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { assert } from 'superstruct';
import Ajv from 'ajv';

import linterConfigJsonSchema from '../../src/linter/aglintrc-json-schema.json';
import { linterConfigSchema, mergeConfigs } from '../../src/linter/config';

describe('Linter config', () => {
Expand Down Expand Up @@ -84,15 +86,30 @@ describe('Linter config', () => {
});
});

test('check custom Superstruct validation', () => {
const assertSuperStruct = (value: unknown) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running the tests both on superstruct and the json schema, because they are duplicated.
In the future, we could migrate from Superstruct to Zod and automatically convert it to json schema using zod-to-json-schema, as @scripthunter7 suggested in AdguardTeam/VscodeAdblockSyntax#136

assert(value, linterConfigSchema);
};
const assertJsonSchema = (value: unknown): void => {
const ajv = new Ajv({ strictTuples: false });
const validate = ajv.compile(linterConfigJsonSchema);
const valid = validate(value);
if (!valid) {
throw new Error(`json schema validation failed: ${JSON.stringify(validate.errors, null, 2)}`);
}
};

test.each([
{ assertFunc: assertSuperStruct, validatorType: 'Superstruct' },
{ assertFunc: assertJsonSchema, validatorType: 'JSON schema' },
])('check custom $validatorType validation', ({ assertFunc }) => {
// Valid cases
expect(() => assert({}, linterConfigSchema)).not.toThrowError();
expect(() => assertFunc({})).not.toThrowError();

expect(() => assert({ allowInlineConfig: true }, linterConfigSchema)).not.toThrowError();
expect(() => assert({ allowInlineConfig: false }, linterConfigSchema)).not.toThrowError();
expect(() => assertFunc({ allowInlineConfig: true })).not.toThrowError();
expect(() => assertFunc({ allowInlineConfig: false })).not.toThrowError();

expect(() => assert({ rules: {} }, linterConfigSchema)).not.toThrowError();
expect(() => assert(
expect(() => assertFunc({ rules: {} })).not.toThrowError();
expect(() => assertFunc(
{
rules: {
'rule-1': 'off',
Expand All @@ -101,20 +118,18 @@ describe('Linter config', () => {
'rule-4': ['error', { a: 'b', c: [{ d: 1, e: '2' }] }, 'aaa', NaN],
},
},
linterConfigSchema,
)).not.toThrowError();

// Invalid cases
expect(() => assert(null, linterConfigSchema)).toThrowError();
expect(() => assertFunc(null)).toThrowError();

expect(() => assert({ allowInlineConfig: 'a' }, linterConfigSchema)).toThrowError();
expect(() => assert({ allowInlineConfig: 2 }, linterConfigSchema)).toThrowError();
expect(() => assertFunc({ allowInlineConfig: 'a' })).toThrowError();
expect(() => assertFunc({ allowInlineConfig: 2 })).toThrowError();

expect(() => assert(
expect(() => assertFunc(
{
rules: 'aaa',
},
linterConfigSchema,
)).toThrowError();
});
});
Loading