(Experimental) Utilities for working with TypeScript + ESLint together
—
Complete JSON Schema type definitions for rule configuration schemas. This module provides comprehensive TypeScript types for JSON Schema drafts 4, 6, and 7, enabling type-safe rule option validation and configuration.
The primary JSON Schema types used for ESLint rule configuration.
/**
* JSON Schema Draft 7 - primary schema interface
*/
interface JSONSchema7 {
/** Schema identifier */
$id?: string;
/** Schema reference */
$ref?: string;
/** Schema dialect */
$schema?: string;
/** Schema comment */
$comment?: string;
/** Schema definitions */
$defs?: Record<string, JSONSchema7Definition>;
// Annotation keywords
/** Schema title */
title?: string;
/** Schema description */
description?: string;
/** Default value */
default?: JSONSchema7Type;
/** Whether the instance is read-only */
readOnly?: boolean;
/** Whether the instance is write-only */
writeOnly?: boolean;
/** Example values */
examples?: JSONSchema7Type[];
// Validation keywords for numeric instances
/** Value must be a multiple of this number */
multipleOf?: number;
/** Maximum value (inclusive) */
maximum?: number;
/** Exclusive maximum value */
exclusiveMaximum?: number;
/** Minimum value (inclusive) */
minimum?: number;
/** Exclusive minimum value */
exclusiveMinimum?: number;
// Validation keywords for strings
/** Maximum string length */
maxLength?: number;
/** Minimum string length */
minLength?: number;
/** Regular expression pattern */
pattern?: string;
// Validation keywords for arrays
/** Schema for additional array items */
additionalItems?: JSONSchema7Definition;
/** Schema(s) for array items */
items?: JSONSchema7Definition | JSONSchema7Definition[];
/** Maximum array length */
maxItems?: number;
/** Minimum array length */
minItems?: number;
/** Whether array items must be unique */
uniqueItems?: boolean;
/** Schema that at least one item must match */
contains?: JSONSchema7Definition;
// Validation keywords for objects
/** Maximum number of properties */
maxProperties?: number;
/** Minimum number of properties */
minProperties?: number;
/** Required property names */
required?: string[];
/** Schema for additional properties */
additionalProperties?: JSONSchema7Definition;
/** Legacy schema definitions */
definitions?: Record<string, JSONSchema7Definition>;
/** Schema for known properties */
properties?: Record<string, JSONSchema7Definition>;
/** Schema for properties matching patterns */
patternProperties?: Record<string, JSONSchema7Definition>;
/** Dependencies between properties */
dependencies?: Record<string, JSONSchema7Definition | string[]>;
/** Schema for property names */
propertyNames?: JSONSchema7Definition;
// Validation keywords for any instance type
/** Constant value */
const?: JSONSchema7Type;
/** Enumeration of valid values */
enum?: JSONSchema7Type[];
/** Expected type(s) */
type?: JSONSchema7TypeName | JSONSchema7TypeName[];
// String format validation
/** String format specification */
format?: string;
/** Content media type */
contentMediaType?: string;
/** Content encoding */
contentEncoding?: string;
// Conditional validation
/** If schema */
if?: JSONSchema7Definition;
/** Then schema (used with if) */
then?: JSONSchema7Definition;
/** Else schema (used with if) */
else?: JSONSchema7Definition;
// Schema composition
/** All schemas must match */
allOf?: JSONSchema7Definition[];
/** At least one schema must match */
anyOf?: JSONSchema7Definition[];
/** Exactly one schema must match */
oneOf?: JSONSchema7Definition[];
/** Schema must not match */
not?: JSONSchema7Definition;
}
/**
* JSON Schema Draft 7 definition - can be a boolean or schema object
*/
type JSONSchema7Definition = boolean | JSONSchema7;
/**
* JSON Schema Draft 7 type names
*/
type JSONSchema7TypeName =
| 'string'
| 'number'
| 'integer'
| 'boolean'
| 'object'
| 'array'
| 'null';
/**
* JSON Schema Draft 7 value types
*/
type JSONSchema7Type =
| string
| number
| boolean
| JSONSchema7Object
| JSONSchema7Array
| null;
/**
* JSON Schema Draft 7 object type
*/
interface JSONSchema7Object {
[key: string]: JSONSchema7Type;
}
/**
* JSON Schema Draft 7 array type
*/
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
/**
* JSON Schema Draft 7 version identifier
*/
type JSONSchema7Version = 'http://json-schema.org/draft-07/schema#';Previous version of JSON Schema with slightly different features.
/**
* JSON Schema Draft 6 interface
*/
interface JSONSchema6 {
/** Schema identifier */
$id?: string;
/** Schema reference */
$ref?: string;
/** Schema dialect */
$schema?: string;
// Annotation keywords
/** Schema title */
title?: string;
/** Schema description */
description?: string;
/** Default value */
default?: JSONSchema6Type;
/** Example values */
examples?: JSONSchema6Type[];
// Numeric validation
/** Value must be a multiple of this number */
multipleOf?: number;
/** Maximum value (inclusive) */
maximum?: number;
/** Exclusive maximum value */
exclusiveMaximum?: number;
/** Minimum value (inclusive) */
minimum?: number;
/** Exclusive minimum value */
exclusiveMinimum?: number;
// String validation
/** Maximum string length */
maxLength?: number;
/** Minimum string length */
minLength?: number;
/** Regular expression pattern */
pattern?: string;
// Array validation
/** Schema for additional array items */
additionalItems?: JSONSchema6Definition;
/** Schema(s) for array items */
items?: JSONSchema6Definition | JSONSchema6Definition[];
/** Maximum array length */
maxItems?: number;
/** Minimum array length */
minItems?: number;
/** Whether array items must be unique */
uniqueItems?: boolean;
/** Schema that at least one item must match */
contains?: JSONSchema6Definition;
// Object validation
/** Maximum number of properties */
maxProperties?: number;
/** Minimum number of properties */
minProperties?: number;
/** Required property names */
required?: string[];
/** Schema for additional properties */
additionalProperties?: JSONSchema6Definition;
/** Schema definitions */
definitions?: Record<string, JSONSchema6Definition>;
/** Schema for known properties */
properties?: Record<string, JSONSchema6Definition>;
/** Schema for properties matching patterns */
patternProperties?: Record<string, JSONSchema6Definition>;
/** Dependencies between properties */
dependencies?: Record<string, JSONSchema6Definition | string[]>;
/** Schema for property names */
propertyNames?: JSONSchema6Definition;
// Type validation
/** Constant value */
const?: JSONSchema6Type;
/** Enumeration of valid values */
enum?: JSONSchema6Type[];
/** Expected type(s) */
type?: JSONSchema6TypeName | JSONSchema6TypeName[];
// String format
/** String format specification */
format?: string;
// Schema composition
/** All schemas must match */
allOf?: JSONSchema6Definition[];
/** At least one schema must match */
anyOf?: JSONSchema6Definition[];
/** Exactly one schema must match */
oneOf?: JSONSchema6Definition[];
/** Schema must not match */
not?: JSONSchema6Definition;
}
/**
* JSON Schema Draft 6 definition
*/
type JSONSchema6Definition = boolean | JSONSchema6;
/**
* JSON Schema Draft 6 type names
*/
type JSONSchema6TypeName =
| 'string'
| 'number'
| 'integer'
| 'boolean'
| 'object'
| 'array'
| 'null';
/**
* JSON Schema Draft 6 value types
*/
type JSONSchema6Type =
| string
| number
| boolean
| JSONSchema6Object
| JSONSchema6Array
| null;
/**
* JSON Schema Draft 6 object type
*/
interface JSONSchema6Object {
[key: string]: JSONSchema6Type;
}
/**
* JSON Schema Draft 6 array type
*/
interface JSONSchema6Array extends Array<JSONSchema6Type> {}
/**
* JSON Schema Draft 6 version identifier
*/
type JSONSchema6Version = 'http://json-schema.org/draft-06/schema#';Legacy JSON Schema version still used in some contexts.
/**
* JSON Schema Draft 4 interface
*/
interface JSONSchema4 {
/** Schema identifier */
id?: string;
/** Schema reference */
$ref?: string;
/** Schema dialect */
$schema?: string;
// Annotation keywords
/** Schema title */
title?: string;
/** Schema description */
description?: string;
/** Default value */
default?: JSONSchema4Type;
// Numeric validation
/** Value must be a multiple of this number */
multipleOf?: number;
/** Maximum value (inclusive) */
maximum?: number;
/** Whether maximum is exclusive */
exclusiveMaximum?: boolean;
/** Minimum value (inclusive) */
minimum?: number;
/** Whether minimum is exclusive */
exclusiveMinimum?: boolean;
// String validation
/** Maximum string length */
maxLength?: number;
/** Minimum string length */
minLength?: number;
/** Regular expression pattern */
pattern?: string;
// Array validation
/** Schema for additional array items */
additionalItems?: boolean | JSONSchema4;
/** Schema(s) for array items */
items?: JSONSchema4 | JSONSchema4[];
/** Maximum array length */
maxItems?: number;
/** Minimum array length */
minItems?: number;
/** Whether array items must be unique */
uniqueItems?: boolean;
// Object validation
/** Maximum number of properties */
maxProperties?: number;
/** Minimum number of properties */
minProperties?: number;
/** Required property names */
required?: string[];
/** Schema for additional properties */
additionalProperties?: boolean | JSONSchema4;
/** Schema definitions */
definitions?: Record<string, JSONSchema4>;
/** Schema for known properties */
properties?: Record<string, JSONSchema4>;
/** Schema for properties matching patterns */
patternProperties?: Record<string, JSONSchema4>;
/** Dependencies between properties */
dependencies?: Record<string, JSONSchema4 | string[]>;
// Type validation
/** Enumeration of valid values */
enum?: JSONSchema4Type[];
/** Expected type(s) */
type?: JSONSchema4TypeName | JSONSchema4TypeName[];
// String format
/** String format specification */
format?: string;
// Schema composition
/** All schemas must match */
allOf?: JSONSchema4[];
/** At least one schema must match */
anyOf?: JSONSchema4[];
/** Exactly one schema must match */
oneOf?: JSONSchema4[];
/** Schema must not match */
not?: JSONSchema4;
}
/**
* JSON Schema Draft 4 type names
*/
type JSONSchema4TypeName =
| 'string'
| 'number'
| 'integer'
| 'boolean'
| 'object'
| 'array'
| 'null'
| 'any';
/**
* JSON Schema Draft 4 value types
*/
type JSONSchema4Type =
| string
| number
| boolean
| JSONSchema4Object
| JSONSchema4Array
| null;
/**
* JSON Schema Draft 4 object type
*/
interface JSONSchema4Object {
[key: string]: JSONSchema4Type;
}
/**
* JSON Schema Draft 4 array type
*/
interface JSONSchema4Array extends Array<JSONSchema4Type> {}
/**
* JSON Schema Draft 4 version identifier
*/
type JSONSchema4Version = 'http://json-schema.org/draft-04/schema#';Types for validation results and error reporting.
/**
* Validation result interface
*/
interface ValidationResult {
/** Whether validation passed */
valid: boolean;
/** Validation errors (if any) */
errors: ValidationError[];
/** Instance being validated */
instance: any;
/** Schema used for validation */
schema: JSONSchema7 | JSONSchema6 | JSONSchema4;
}
/**
* Validation error interface
*/
interface ValidationError {
/** Error message */
message: string;
/** Property path where error occurred */
property: string;
/** Instance path where error occurred */
instance: string;
/** Schema path that caused the error */
schema: string;
/** Stack trace (if available) */
stack?: string;
/** Error name */
name: string;
/** Validation error argument */
argument?: any;
}import { JSONSchema, TSESLint } from "@typescript-eslint/experimental-utils";
// Define rule options schema
const ruleSchema: JSONSchema.JSONSchema7 = {
type: 'object',
properties: {
ignorePatterns: {
type: 'array',
items: {
type: 'string'
},
description: 'Patterns to ignore during analysis'
},
checkTypes: {
type: 'boolean',
default: true,
description: 'Whether to perform type checking'
},
severity: {
type: 'string',
enum: ['error', 'warn', 'off'],
default: 'error',
description: 'Rule severity level'
},
maxComplexity: {
type: 'integer',
minimum: 1,
maximum: 100,
default: 10,
description: 'Maximum allowed complexity'
}
},
additionalProperties: false
};
// Use in a rule definition
const rule: TSESLint.RuleModule<'error', [typeof ruleSchema]> = {
meta: {
type: 'problem',
messages: {
error: 'Complexity too high: {{actual}} > {{max}}'
},
schema: [ruleSchema]
},
create(context, [options]) {
// options is now type-safe based on the schema
const ignorePatterns = options.ignorePatterns || [];
const checkTypes = options.checkTypes ?? true;
const severity = options.severity || 'error';
const maxComplexity = options.maxComplexity || 10;
return {
// rule implementation
};
}
};
// Complex schema with conditional validation
const conditionalSchema: JSONSchema.JSONSchema7 = {
type: 'object',
properties: {
mode: {
type: 'string',
enum: ['strict', 'loose']
},
options: {
type: 'object'
}
},
required: ['mode'],
if: {
properties: {
mode: { const: 'strict' }
}
},
then: {
properties: {
options: {
type: 'object',
properties: {
enforceReturnType: {
type: 'boolean',
default: true
},
allowAny: {
type: 'boolean',
default: false
}
},
required: ['enforceReturnType'],
additionalProperties: false
}
}
},
else: {
properties: {
options: {
type: 'object',
properties: {
skipPrivateMethods: {
type: 'boolean',
default: true
}
},
additionalProperties: false
}
}
}
};
// Schema with pattern properties
const patternSchema: JSONSchema.JSONSchema7 = {
type: 'object',
patternProperties: {
'^[a-zA-Z][a-zA-Z0-9]*$': {
type: 'object',
properties: {
enabled: { type: 'boolean' },
priority: { type: 'integer', minimum: 0 }
},
required: ['enabled']
}
},
additionalProperties: false
};
// Array schema with tuple validation
const tupleSchema: JSONSchema.JSONSchema7 = {
type: 'array',
items: [
{ type: 'string' }, // First item must be string
{ type: 'number' }, // Second item must be number
{ type: 'boolean' } // Third item must be boolean
],
additionalItems: false, // No additional items allowed
minItems: 2, // At least first two items required
maxItems: 3 // Maximum three items
};
// Schema composition example
const compositionSchema: JSONSchema.JSONSchema7 = {
oneOf: [
{
type: 'string',
pattern: '^[a-z]+$'
},
{
type: 'object',
properties: {
name: { type: 'string' },
enabled: { type: 'boolean' }
},
required: ['name'],
additionalProperties: false
},
{
type: 'array',
items: { type: 'string' },
minItems: 1
}
]
};
// Using schema for validation in tests
function validateRuleOptions(options: unknown, schema: JSONSchema.JSONSchema7): boolean {
// In a real implementation, you would use a JSON Schema validator library
// This is a simplified example showing the type structure
if (schema.type === 'object' && typeof options === 'object' && options !== null) {
const obj = options as Record<string, unknown>;
// Check required properties
if (schema.required) {
for (const prop of schema.required) {
if (!(prop in obj)) {
return false;
}
}
}
// Check property types
if (schema.properties) {
for (const [prop, propSchema] of Object.entries(schema.properties)) {
if (prop in obj) {
const value = obj[prop];
if (typeof propSchema === 'object' && 'type' in propSchema) {
if (propSchema.type === 'string' && typeof value !== 'string') {
return false;
}
if (propSchema.type === 'boolean' && typeof value !== 'boolean') {
return false;
}
if (propSchema.type === 'number' && typeof value !== 'number') {
return false;
}
}
}
}
}
return true;
}
return false;
}
// Helper function to create type-safe rule schemas
function createRuleSchema<T extends Record<string, JSONSchema.JSONSchema7>>(
properties: T
): JSONSchema.JSONSchema7 {
return {
type: 'object',
properties,
additionalProperties: false
};
}
// Usage of helper
const myRuleSchema = createRuleSchema({
checkImports: {
type: 'boolean',
default: true,
description: 'Whether to check import statements'
},
allowedModules: {
type: 'array',
items: { type: 'string' },
description: 'List of allowed module names'
}
});
// Schema with format validation
const formatSchema: JSONSchema.JSONSchema7 = {
type: 'object',
properties: {
email: {
type: 'string',
format: 'email'
},
url: {
type: 'string',
format: 'uri'
},
date: {
type: 'string',
format: 'date'
},
ipv4: {
type: 'string',
format: 'ipv4'
},
regex: {
type: 'string',
format: 'regex'
}
}
};
// Schema with content validation
const contentSchema: JSONSchema.JSONSchema7 = {
type: 'object',
properties: {
jsonData: {
type: 'string',
contentMediaType: 'application/json'
},
base64Data: {
type: 'string',
contentEncoding: 'base64'
},
htmlContent: {
type: 'string',
contentMediaType: 'text/html'
}
}
};// Utility types for common schema patterns
/**
* Schema for string with enumeration
*/
type StringEnumSchema<T extends readonly string[]> = {
type: 'string';
enum: T;
};
/**
* Schema for optional boolean with default
*/
type BooleanWithDefaultSchema = {
type: 'boolean';
default: boolean;
};
/**
* Schema for array of specific type
*/
type ArrayOfSchema<T extends JSONSchema.JSONSchema7> = {
type: 'array';
items: T;
};
/**
* Schema for object with specific properties
*/
type ObjectWithPropertiesSchema<T extends Record<string, JSONSchema.JSONSchema7>> = {
type: 'object';
properties: T;
additionalProperties: false;
};
/**
* Schema for union types using oneOf
*/
type UnionSchema<T extends readonly JSONSchema.JSONSchema7[]> = {
oneOf: T;
};
// Examples of utility usage
const colorSchema: StringEnumSchema<['red', 'green', 'blue']> = {
type: 'string',
enum: ['red', 'green', 'blue']
};
const enabledSchema: BooleanWithDefaultSchema = {
type: 'boolean',
default: false
};
const stringArraySchema: ArrayOfSchema<{ type: 'string' }> = {
type: 'array',
items: { type: 'string' }
};
const configSchema: ObjectWithPropertiesSchema<{
name: { type: 'string' };
version: { type: 'number' };
}> = {
type: 'object',
properties: {
name: { type: 'string' },
version: { type: 'number' }
},
additionalProperties: false
};
const mixedValueSchema: UnionSchema<[
{ type: 'string' },
{ type: 'number' },
{ type: 'boolean' }
]> = {
oneOf: [
{ type: 'string' },
{ type: 'number' },
{ type: 'boolean' }
]
};Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--experimental-utils