123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- import { devAssert } from '../jsutils/devAssert.mjs';
- import { GraphQLError } from '../error/GraphQLError.mjs';
- import { visit, visitInParallel } from '../language/visitor.mjs';
- import { assertValidSchema } from '../type/validate.mjs';
- import { TypeInfo, visitWithTypeInfo } from '../utilities/TypeInfo.mjs';
- import { specifiedRules, specifiedSDLRules } from './specifiedRules.mjs';
- import {
- SDLValidationContext,
- ValidationContext,
- } from './ValidationContext.mjs';
- export function validate(
- schema,
- documentAST,
- rules = specifiedRules,
- options,
-
- typeInfo = new TypeInfo(schema),
- ) {
- var _options$maxErrors;
- const maxErrors =
- (_options$maxErrors =
- options === null || options === void 0 ? void 0 : options.maxErrors) !==
- null && _options$maxErrors !== void 0
- ? _options$maxErrors
- : 100;
- documentAST || devAssert(false, 'Must provide document.');
- assertValidSchema(schema);
- const abortObj = Object.freeze({});
- const errors = [];
- const context = new ValidationContext(
- schema,
- documentAST,
- typeInfo,
- (error) => {
- if (errors.length >= maxErrors) {
- errors.push(
- new GraphQLError(
- 'Too many validation errors, error limit reached. Validation aborted.',
- ),
- );
- throw abortObj;
- }
- errors.push(error);
- },
- );
-
- const visitor = visitInParallel(rules.map((rule) => rule(context)));
- try {
- visit(documentAST, visitWithTypeInfo(typeInfo, visitor));
- } catch (e) {
- if (e !== abortObj) {
- throw e;
- }
- }
- return errors;
- }
- export function validateSDL(
- documentAST,
- schemaToExtend,
- rules = specifiedSDLRules,
- ) {
- const errors = [];
- const context = new SDLValidationContext(
- documentAST,
- schemaToExtend,
- (error) => {
- errors.push(error);
- },
- );
- const visitors = rules.map((rule) => rule(context));
- visit(documentAST, visitInParallel(visitors));
- return errors;
- }
- export function assertValidSDL(documentAST) {
- const errors = validateSDL(documentAST);
- if (errors.length !== 0) {
- throw new Error(errors.map((error) => error.message).join('\n\n'));
- }
- }
- export function assertValidSDLExtension(documentAST, schema) {
- const errors = validateSDL(documentAST, schema);
- if (errors.length !== 0) {
- throw new Error(errors.map((error) => error.message).join('\n\n'));
- }
- }
|