|
1 |
| -import { ValidationRule } from 'graphql'; |
2 |
| -import { GraphQLESLintRule } from '../types'; |
3 |
| -import { validateDoc } from './validate-against-schema'; |
| 1 | +import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types'; |
4 | 2 | import { requireGraphQLSchemaFromContext } from '../utils';
|
5 | 3 |
|
| 4 | +import { validate, GraphQLSchema, DocumentNode, ASTNode, ValidationRule } from 'graphql'; |
| 5 | +import { validateSDL } from 'graphql/validation/validate'; |
| 6 | +import { GraphQLESTreeNode } from '../estree-parser'; |
| 7 | + |
| 8 | +function extractRuleName(stack: string | undefined): string | null { |
| 9 | + const match = (stack || '').match(/validation[/\\\\]rules[/\\\\](.*?)\.js:/); |
| 10 | + |
| 11 | + if (!match) { |
| 12 | + return null; |
| 13 | + } |
| 14 | + |
| 15 | + return match[1] || null; |
| 16 | +} |
| 17 | + |
| 18 | +export function validateDoc( |
| 19 | + sourceNode: GraphQLESTreeNode<ASTNode>, |
| 20 | + context: GraphQLESlintRuleContext, |
| 21 | + schema: GraphQLSchema | null, |
| 22 | + documentNode: DocumentNode, |
| 23 | + rules: ReadonlyArray<ValidationRule>, |
| 24 | + ruleName: string | null = null |
| 25 | +): void { |
| 26 | + if (documentNode && documentNode.definitions && documentNode.definitions.length > 0) { |
| 27 | + try { |
| 28 | + const validationErrors = schema ? validate(schema, documentNode, rules) : validateSDL(documentNode, null, rules); |
| 29 | + |
| 30 | + for (const error of validationErrors) { |
| 31 | + const validateRuleName = ruleName || `[${extractRuleName(error.stack)}]`; |
| 32 | + |
| 33 | + context.report({ |
| 34 | + loc: error.locations[0], |
| 35 | + message: ruleName ? error.message : `${validateRuleName} ${error.message}`, |
| 36 | + }); |
| 37 | + } |
| 38 | + } catch (e) { |
| 39 | + context.report({ |
| 40 | + node: sourceNode, |
| 41 | + message: e.message, |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
6 | 47 | const validationToRule = (
|
7 | 48 | name: string,
|
8 | 49 | ruleName: string,
|
|
0 commit comments