-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e53171f
commit 25c54b7
Showing
9 changed files
with
262 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema' | ||
import type { RuleModule } from '@typescript-eslint/utils/ts-eslint' | ||
|
||
import * as jsonSpecv4 from 'ajv/lib/refs/json-schema-draft-04.json' | ||
import { TSUtils } from '@typescript-eslint/utils' | ||
import Ajv from 'ajv' | ||
|
||
// Lovingly borrowed from: https://github.com/typescript-eslint/typescript-eslint/pull/6947/files | ||
// TODO: It would be nicer to have https://github.com/eslint/eslint/pull/16823, but it's back in draft. | ||
const ajv = new Ajv({ | ||
schemaId: 'auto', | ||
async: false, | ||
meta: false, | ||
}) | ||
ajv.addMetaSchema(jsonSpecv4) | ||
// @ts-ignore | ||
ajv._opts.defaultMeta = jsonSpecv4.id | ||
// @ts-ignore | ||
ajv._refs['http://json-schema.org/schema'] = | ||
'http://json-schema.org/draft-04/schema' | ||
|
||
export let areOptionsValid = ( | ||
rule: RuleModule<string, readonly unknown[]>, | ||
options: unknown, | ||
): boolean | string => { | ||
let normalizedSchema = normalizeSchema(rule.meta.schema) | ||
|
||
let validate = ajv.compile(normalizedSchema) | ||
|
||
let valid = validate([options]) | ||
if (typeof valid !== 'boolean') { | ||
// Schema could not validate options synchronously. This is not allowed for ESLint rules. | ||
return false | ||
} | ||
|
||
return valid || ajv.errorsText(validate.errors) | ||
} | ||
|
||
let normalizeSchema = ( | ||
schema: readonly JSONSchema4[] | JSONSchema4, | ||
): JSONSchema4 => { | ||
if (!TSUtils.isArray(schema)) { | ||
return schema | ||
} | ||
|
||
if (schema.length === 0) { | ||
return { | ||
type: 'array', | ||
minItems: 0, | ||
maxItems: 0, | ||
} | ||
} | ||
|
||
return { | ||
items: schema as JSONSchema4[], | ||
maxItems: schema.length, | ||
type: 'array', | ||
minItems: 0, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters