graphql-mandatory-validator
Version:
A GraphQL schema validator using AST-only parsing for mandatory fields with default values, array validation, and composite type validation
113 lines • 3.5 kB
TypeScript
export interface ValidationOptions {
/**
* Base directory to search for GraphQL files (default: 'src/type-defs')
*/
baseDir?: string;
/**
* Custom scalar type defaults
*/
scalarDefaults?: Record<string, string>;
/**
* Whether to use colored output (default: true)
*/
colorOutput?: boolean;
/**
* Whether to exit process on validation failure (default: true)
*/
exitOnError?: boolean;
}
export interface ValidationResult {
success: boolean;
errors: ValidationError[];
filesChecked: number;
}
export interface ValidationError {
file: string;
line: number;
fieldName: string;
fieldType: string;
expectedDefault?: string;
message: string;
errorType: 'missing-default' | 'empty-composite' | 'missing-enum-default' | 'invalid-enum-default' | 'missing-array-default' | 'invalid-array-default';
}
export declare class GraphQLValidator {
private options;
constructor(options?: ValidationOptions);
/**
* Get staged .graphql files from git
*/
getStagedGraphqlFiles(): string[];
/**
* Get all .graphql files in the base directory
*/
getAllGraphqlFiles(projectRoot?: string): string[];
/**
* Validate newly added mandatory fields in staged files
*/
validateStagedFiles(): Promise<ValidationResult>;
/**
* Validate all GraphQL files in the project
*/
validateProject(projectRoot?: string): Promise<ValidationResult>;
/**
* Parse git diff to extract added lines with correct line numbers
*/
private parseGitDiffForAddedLines;
/**
* Parse GraphQL schema from files to understand type definitions
* For enum validation, we need to parse ALL files in the project to get complete enum registry
*/
private parseGraphQLSchema;
/**
* Parse enums from a single GraphQL file to build complete enum registry
*/
private parseEnumsFromFile;
/**
* Parse a single GraphQL file using GraphQL AST parser
*/
private parseFileSchema;
/**
* Extract type name from GraphQL AST type node
*/
private extractTypeFromAST;
/**
* Get line number from GraphQL AST location
*/
private getLineNumberFromLocation;
/**
* Check if a type is an array/list type based on AST parsing
*/
private isArrayType;
/**
* Check if a string is a valid GraphQL identifier without using regex
*/
private isValidIdentifier;
/**
* Fallback AST-based parsing for when primary AST parsing fails
* This is a simplified version that only extracts basic type information
*/
private parseFileSchemeFallback;
/**
* Validate composite fields using AST-based approach with line filtering
*/
private validateCompositeFields;
/**
* Validate that mandatory composite types have at least one mandatory field
* Only checks newly added fields when onlyDiffChanges is true
*/
private validateCompositeTypes;
/**
* Validate specific GraphQL files using AST-based approach
*/
validateFiles(files: string[], onlyDiffChanges: boolean): Promise<ValidationResult>;
/**
* Report validation results
*/
private reportResults;
/**
* Run validation (convenience method for CLI usage)
*/
run(mode?: 'staged' | 'all', projectRoot?: string): Promise<ValidationResult>;
}
export default GraphQLValidator;
//# sourceMappingURL=index.d.ts.map