-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
.DS_Store | ||
.eslintcache | ||
.vscode | ||
.idea | ||
*.iml | ||
coverage | ||
dist | ||
node_modules | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
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 | ||
} |
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', () => { | ||
|
@@ -84,15 +86,30 @@ describe('Linter config', () => { | |
}); | ||
}); | ||
|
||
test('check custom Superstruct validation', () => { | ||
const assertSuperStruct = (value: unknown) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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', | ||
|
@@ -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(); | ||
}); | ||
}); |
There was a problem hiding this comment.
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