public DocumentValidatorResult Validate(ISchema schema, DocumentNode document) { if (schema is null) { throw new ArgumentNullException(nameof(schema)); } if (document is null) { throw new ArgumentNullException(nameof(document)); } DocumentValidatorContext context = _contextPool.Get(); try { PrepareContext(schema, document, context); for (int i = 0; i < _rules.Length; i++) { _rules[i].Validate(context, document); } if (context.Errors.Count > 0) { return(new DocumentValidatorResult(context.Errors)); } return(DocumentValidatorResult.OK); } finally { _contextPool.Return(context); } }
private void PrepareContext( ISchema schema, DocumentNode document, DocumentValidatorContext context, IEnumerable <KeyValuePair <string, object?> >?contextData) { context.Schema = schema; for (var i = 0; i < document.Definitions.Count; i++) { IDefinitionNode definitionNode = document.Definitions[i]; if (definitionNode.Kind == SyntaxKind.FragmentDefinition) { var fragmentDefinition = (FragmentDefinitionNode)definitionNode; context.Fragments[fragmentDefinition.Name.Value] = fragmentDefinition; } } if (contextData is not null) { foreach (KeyValuePair <string, object?> entry in contextData) { context.ContextData[entry.Key] = entry.Value; } } }
/// <inheritdoc /> public DocumentValidatorResult Validate( ISchema schema, DocumentNode document, IEnumerable <KeyValuePair <string, object?> >?contextData) { if (schema is null) { throw new ArgumentNullException(nameof(schema)); } if (document is null) { throw new ArgumentNullException(nameof(document)); } DocumentValidatorContext context = _contextPool.Get(); try { PrepareContext(schema, document, context, contextData); for (var i = 0; i < _rules.Length; i++) { _rules[i].Validate(context, document); } return(context.Errors.Count > 0 ? new DocumentValidatorResult(context.Errors) : DocumentValidatorResult.Ok); } finally { _contextPool.Return(context); } }
public static void ExpectErrors( ISchema schema, Action <IValidationBuilder> configure, string sourceText, IEnumerable <KeyValuePair <string, object> > contextData = null, params Action <IError>[] elementInspectors) { // arrange var serviceCollection = new ServiceCollection(); IValidationBuilder builder = serviceCollection .AddValidation() .ConfigureValidation(c => c.Modifiers.Add(o => o.Rules.Clear())); configure(builder); IServiceProvider services = serviceCollection.BuildServiceProvider(); var rule = services.GetRequiredService <IValidationConfiguration>() .GetRules(Schema.DefaultName).First(); DocumentValidatorContext context = ValidationUtils.CreateContext(schema); DocumentNode query = Utf8GraphQLParser.Parse(sourceText); context.Prepare(query); context.ContextData = new Dictionary <string, object>(); if (contextData is not null) { foreach ((var key, var value) in contextData) { context.ContextData[key] = value; } } // act rule.Validate(context, query); // assert Assert.NotEmpty(context.Errors); if (elementInspectors.Length > 0) { Assert.Collection(context.Errors, elementInspectors); } context.Errors.MatchSnapshot(); }
private void PrepareContext( ISchema schema, DocumentNode document, DocumentValidatorContext context) { context.Schema = schema; for (int i = 0; i < document.Definitions.Count; i++) { IDefinitionNode definitionNode = document.Definitions[i]; if (definitionNode.Kind == NodeKind.FragmentDefinition) { var fragmentDefinition = (FragmentDefinitionNode)definitionNode; context.Fragments[fragmentDefinition.Name.Value] = fragmentDefinition; } } }
/// <inheritdoc /> public DocumentValidatorResult Validate( ISchema schema, DocumentNode document, IDictionary <string, object?> contextData, bool onlyNonCacheable = false) { if (schema is null) { throw new ArgumentNullException(nameof(schema)); } if (document is null) { throw new ArgumentNullException(nameof(document)); } if (onlyNonCacheable && _nonCacheableRules.Length == 0) { return(DocumentValidatorResult.Ok); } DocumentValidatorContext context = _contextPool.Get(); IDocumentValidatorRule[] rules = onlyNonCacheable ? _nonCacheableRules : _allRules; try { PrepareContext(schema, document, context, contextData); foreach (var rule in rules) { rule.Validate(context, document); } return(context.Errors.Count > 0 ? new DocumentValidatorResult(context.Errors) : DocumentValidatorResult.Ok); } finally { _contextPool.Return(context); } }
private void PrepareContext( ISchema schema, DocumentNode document, DocumentValidatorContext context, IDictionary <string, object?> contextData) { context.Schema = schema; for (var i = 0; i < document.Definitions.Count; i++) { IDefinitionNode definitionNode = document.Definitions[i]; if (definitionNode.Kind is SyntaxKind.FragmentDefinition) { var fragmentDefinition = (FragmentDefinitionNode)definitionNode; context.Fragments[fragmentDefinition.Name.Value] = fragmentDefinition; } } context.ContextData = contextData; }