Skip to content

Commit

Permalink
Moved validation error messages to resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Apr 11, 2020
1 parent 05aeda5 commit 91d1f03
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using HotChocolate.Validation.Options;
using HotChocolate.Validation.Properties;

namespace HotChocolate.Validation
{
Expand All @@ -22,7 +23,7 @@ public IDocumentValidator CreateValidator(string schemaName)
if (string.IsNullOrEmpty(schemaName))
{
throw new ArgumentException(
"The schema name is mandatory in order to create a validator.",
Resources.DefaultDocumentValidatorFactory_Schema_Name_Is_Mandatory,
nameof(schemaName));
}

Expand Down
19 changes: 6 additions & 13 deletions src/HotChocolate/Core/src/Validation/DocumentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ public DocumentValidator(
DocumentValidatorContextPool contextPool,
IEnumerable<IDocumentValidatorRule> rules)
{
if (contextPool is null)
{
throw new ArgumentNullException(nameof(contextPool));
}

if (rules is null)
{
throw new ArgumentNullException(nameof(rules));
}

_contextPool = contextPool;
_contextPool = contextPool ?? throw new ArgumentNullException(nameof(contextPool));
_rules = rules.ToArray();
}

Expand All @@ -46,16 +41,14 @@ public DocumentValidatorResult Validate(ISchema schema, DocumentNode document)
{
PrepareContext(schema, document, context);

for (int i = 0; i < _rules.Length; i++)
for (var i = 0; i < _rules.Length; i++)
{
_rules[i].Validate(context, document);
}

if (context.Errors.Count > 0)
{
return new DocumentValidatorResult(context.Errors);
}
return DocumentValidatorResult.OK;
return context.Errors.Count > 0
? new DocumentValidatorResult(context.Errors)
: DocumentValidatorResult.Ok;
}
finally
{
Expand All @@ -70,7 +63,7 @@ private void PrepareContext(
{
context.Schema = schema;

for (int i = 0; i < document.Definitions.Count; i++)
for (var i = 0; i < document.Definitions.Count; i++)
{
IDefinitionNode definitionNode = document.Definitions[i];
if (definitionNode.Kind == NodeKind.FragmentDefinition)
Expand Down
11 changes: 6 additions & 5 deletions src/HotChocolate/Core/src/Validation/DocumentValidatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Validation.Properties;

namespace HotChocolate.Validation
{
Expand All @@ -17,7 +18,7 @@ public sealed class DocumentValidatorContext : IDocumentValidatorContext

private ISchema? _schema;
private IOutputType? _nonNullString;
private bool unexpectedErrorsDetected;
private bool _unexpectedErrorsDetected;

public ISchema Schema
{
Expand All @@ -27,7 +28,7 @@ public ISchema Schema
{
// TODO : resources
throw new InvalidOperationException(
"The context has an invalid state and is missing the schema.");
Resources.DocumentValidatorContext_Context_Invalid_State);
}
return _schema;
}
Expand All @@ -46,7 +47,7 @@ public IOutputType NonNullString
{
// TODO : resources
throw new InvalidOperationException(
"The context has an invalid state and is missing the schema.");
Resources.DocumentValidatorContext_Context_Invalid_State);
}
return _nonNullString;
}
Expand Down Expand Up @@ -93,10 +94,10 @@ public IOutputType NonNullString

public bool UnexpectedErrorsDetected
{
get => unexpectedErrorsDetected;
get => _unexpectedErrorsDetected;
set
{
unexpectedErrorsDetected = value;
_unexpectedErrorsDetected = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public DocumentValidatorResult(IEnumerable<IError> errors)

public IReadOnlyList<IError> Errors { get; }

public static DocumentValidatorResult OK { get; } =
public static DocumentValidatorResult Ok { get; } =
new DocumentValidatorResult();
}
}
Loading

0 comments on commit 91d1f03

Please sign in to comment.