Feature request: rule option schema validation
While rules specify a typescript interface for the types there is no schema validation for the options.
my-rule.ts
:
interface Options {
foo: string;
bar: "a" | "b";
}
class MyRule extends Rule<void, Options> {}
.htmlvalidate.json
:
{
"rules": {
"my-rule": ["error", {
"foo": 12,
"bar: "spam"
}]
}
}
In the above case the current schema validation will accept the configuration and unless the rule includes actual validation it will fail.
Suggested solution
Add an optional method (static or instance), template argument, export or similar which exposes a partial JSON schema. Preferably an array of valid schemas which is joined by anyOf
.
class MyRule extends Rule<void, Options> {
static get schema(): RuleOptionsSchema[] {
return [
{
type: "object",
properties: {
"foo": {
"type": "string",
},
"bar": {
"enum": ["a", "b"]
},
}
}
];
}
}
Preferably the schema would be validated before passing the options to the rule constructor.
Edited by David Sveningsson