Skip to content

⬅️ Back to Table of Contents

📄 areOptionsValid.ts

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 4
📊 Variables & Constants 1

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/tests/areOptionsValid.ts

📦 Imports

Name Source
RuleModule @typescript-eslint/utils/ts-eslint
JSONSchema4 json-schema
TSUtils @typescript-eslint/utils
Ajv ajv

Variables & Constants

Name Type Kind Value Exported
ajv any const new Ajv({ async: false })

Functions

areOptionsValid(rule: RuleModule<string, readonly unknown[]>, options: unknown): boolean

Code
export function areOptionsValid(
  rule: RuleModule<string, readonly unknown[]>,
  options: unknown,
): boolean {
  const normalizedSchema = normalizeSchema(rule.meta.schema);

  const valid = ajv.validate(normalizedSchema, options);
  if (typeof valid !== 'boolean') {
    // Schema could not validate options synchronously. This is not allowed for ESLint rules.
    return false;
  }

  return valid;
}
  • Parameters:
  • rule: RuleModule<string, readonly unknown[]>
  • options: unknown
  • Return Type: boolean
  • Calls:
  • normalizeSchema
  • ajv.validate
  • Internal Comments:
    // Schema could not validate options synchronously. This is not allowed for ESLint rules.
    

normalizeSchema(schema: JSONSchema4 | readonly JSONSchema4[]): JSONSchema4

Code
function normalizeSchema(
  schema: JSONSchema4 | readonly JSONSchema4[],
): JSONSchema4 {
  if (!TSUtils.isArray(schema)) {
    return schema;
  }

  if (schema.length === 0) {
    return {
      maxItems: 0,
      minItems: 0,
      type: 'array',
    };
  }

  return {
    items: schema as JSONSchema4[],
    maxItems: schema.length,
    minItems: 0,
    type: 'array',
  };
}
  • Parameters:
  • schema: JSONSchema4 | readonly JSONSchema4[]
  • Return Type: JSONSchema4
  • Calls:
  • TSUtils.isArray