Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HotChocolate.Events.Contracts;
using HotChocolate.Language;
using HotChocolate.Types;
using DirectiveLocation = HotChocolate.Types.DirectiveLocation;

namespace HotChocolate.Events;

Expand Down Expand Up @@ -45,9 +46,13 @@ public readonly record struct DirectiveArgumentAssignmentEvent(
/// <summary>
/// Represents an event that is triggered when a directive is encountered during schema validation.
/// </summary>
/// <param name="Directive">The applied directive.</param>
/// <param name="Member">The type system member the directive is applied to.</param>
/// <param name="Location">The location at which the directive is applied.</param>
public readonly record struct DirectiveEvent(
IDirective Directive,
ITypeSystemMember Member) : IValidationEvent;
ITypeSystemMember Member,
DirectiveLocation Location) : IValidationEvent;

/// <summary>
/// Represents an event that is triggered when an Enum type is encountered during schema validation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ internal static class LogEntryCodes
public const string IncompatibleInputFieldDefaultValue = "HCV0029";
public const string InvalidObjectDeprecation = "HCV0030";
public const string DirectiveNotUnique = "HCV0031";
public const string DirectiveInInvalidLocation = "HCV0032";
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public static LogEntry DirectiveDefinitionSelfApplication(IDirectiveDefinition d
.Build();
}

public static LogEntry DirectiveInInvalidLocation(
IDirective directive,
ITypeSystemMember member,
DirectiveLocation location)
{
return LogEntryBuilder.New()
.SetMessage(
LogEntryHelper_DirectiveInInvalidLocation,
directive.Name,
GetDirectiveMemberName(member),
location.Format())
.SetCode(LogEntryCodes.DirectiveInInvalidLocation)
.SetSeverity(LogSeverity.Error)
.SetTypeSystemMember(member)
.Build();
}

public static LogEntry DirectiveNotUnique(IDirective directive, ITypeSystemMember member)
{
return LogEntryBuilder.New()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<data name="LogEntryHelper_DirectiveDefinitionSelfApplication" xml:space="preserve">
<value>The directive definition '@{0}' must not reference itself.</value>
</data>
<data name="LogEntryHelper_DirectiveInInvalidLocation" xml:space="preserve">
<value>The directive '@{0}' on '{1}' is not allowed on the location '{2}'.</value>
</data>
<data name="LogEntryHelper_DirectiveNotUnique" xml:space="preserve">
<value>The non-repeatable directive '@{0}' on '{1}' is applied more than once.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class DirectiveIsDefinedRule : IValidationEventHandler<DirectiveEv
/// </summary>
public void Handle(DirectiveEvent @event, ValidationContext context)
{
var (directive, member) = @event;
var (directive, member, _) = @event;

if (DirectiveNames.IsSpecDirective(directive.Name))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using HotChocolate.Events;
using HotChocolate.Events.Contracts;
using HotChocolate.Types;
using static HotChocolate.Logging.LogEntryHelper;

namespace HotChocolate.Rules;

/// <summary>
/// Checks that a directive is applied only in locations its definition declares.
/// </summary>
public sealed class DirectiveIsInValidLocationRule : IValidationEventHandler<DirectiveEvent>
{
/// <summary>
/// Checks that a directive is applied only in locations its definition declares.
/// </summary>
public void Handle(DirectiveEvent @event, ValidationContext context)
{
var (directive, member, location) = @event;

// DirectiveIsDefinedRule reports directives without a definition.
if (directive.Definition is IMissingDirectiveDefinition)
{
return;
}

if ((directive.Definition.Locations & location) != location)
{
context.Log.Write(DirectiveInInvalidLocation(directive, member, location));
}
Comment thread
glen-84 marked this conversation as resolved.
Resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class DirectiveIsUniqueRule : IValidationEventHandler<DirectiveEve
/// </summary>
public void Handle(DirectiveEvent @event, ValidationContext context)
{
var (directive, member) = @event;
var (directive, member, _) = @event;

if (directive.Definition.IsRepeatable || member is not IDirectivesProvider provider)
{
Expand Down
45 changes: 36 additions & 9 deletions src/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using HotChocolate.Logging.Contracts;
using HotChocolate.Rules;
using HotChocolate.Types;
using DirectiveLocation = HotChocolate.Types.DirectiveLocation;

namespace HotChocolate;

Expand Down Expand Up @@ -52,6 +53,7 @@ public void AddDefaultRules()
_rules.Add(new DirectiveDefinitionIncludesLocationRule());
_rules.Add(new DirectiveDefinitionNoSelfReferenceRule());
_rules.Add(new DirectiveIsDefinedRule());
_rules.Add(new DirectiveIsInValidLocationRule());
_rules.Add(new DirectiveIsUniqueRule());
_rules.Add(new EnumValueIsDefinedRule());
_rules.Add(new NoInputObjectCycleRule());
Expand Down Expand Up @@ -96,12 +98,12 @@ private void PublishEvents(ValidationContext context)

PublishEvent(new InputObjectTypesEvent(schema.Types.OfType<IInputObjectTypeDefinition>()), context);

PublishDirectiveEvents(schema, context);
PublishDirectiveEvents(schema, DirectiveLocation.Schema, context);

foreach (var type in schema.Types)
{
PublishEvent(new TypeEvent(type), context);
PublishDirectiveEvents(type, context);
PublishDirectiveEvents(type, GetTypeDirectiveLocation(type), context);

if (type is INameProvider namedMember)
{
Expand Down Expand Up @@ -136,10 +138,13 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new NamedMemberEvent(argument), context);

PublishDefaultValueNodeEvents(argument, context);
PublishDirectiveEvents(argument, context);
PublishDirectiveEvents(
argument,
DirectiveLocation.ArgumentDefinition,
context);
}

PublishDirectiveEvents(field, context);
PublishDirectiveEvents(field, DirectiveLocation.FieldDefinition, context);
}

break;
Expand All @@ -152,7 +157,7 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new EnumValueEvent(value), context);
PublishEvent(new NamedMemberEvent(value), context);

PublishDirectiveEvents(value, context);
PublishDirectiveEvents(value, DirectiveLocation.EnumValue, context);
}

break;
Expand All @@ -168,7 +173,10 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new NamedMemberEvent(field), context);

PublishDefaultValueNodeEvents(field, context);
PublishDirectiveEvents(field, context);
PublishDirectiveEvents(
field,
DirectiveLocation.InputFieldDefinition,
context);
}

break;
Expand All @@ -184,7 +192,10 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new DirectiveDefinitionEvent(directiveDefinition), context);
PublishEvent(new NamedMemberEvent(directiveDefinition), context);

PublishDirectiveEvents(directiveDefinition, context);
PublishDirectiveEvents(
directiveDefinition,
DirectiveLocation.DirectiveDefinition,
context);

foreach (var argument in directiveDefinition.Arguments)
{
Expand All @@ -193,18 +204,19 @@ private void PublishEvents(ValidationContext context)
PublishEvent(new NamedMemberEvent(argument), context);

PublishDefaultValueNodeEvents(argument, context);
PublishDirectiveEvents(argument, context);
PublishDirectiveEvents(argument, DirectiveLocation.ArgumentDefinition, context);
}
}
}

private void PublishDirectiveEvents(
IDirectivesProvider member,
DirectiveLocation location,
ValidationContext context)
{
foreach (var directive in member.Directives)
{
PublishEvent(new DirectiveEvent(directive, member), context);
PublishEvent(new DirectiveEvent(directive, member, location), context);

foreach (var argumentAssignment in directive.Arguments)
{
Expand All @@ -226,6 +238,21 @@ private void PublishDirectiveEvents(
}
}

private static DirectiveLocation GetTypeDirectiveLocation(ITypeDefinition type)
{
return type.Kind switch
{
TypeKind.Enum => DirectiveLocation.Enum,
TypeKind.InputObject => DirectiveLocation.InputObject,
TypeKind.Interface => DirectiveLocation.Interface,
TypeKind.Object => DirectiveLocation.Object,
TypeKind.Scalar => DirectiveLocation.Scalar,
TypeKind.Union => DirectiveLocation.Union,
TypeKind.Directive or TypeKind.List or TypeKind.NonNull or _ =>
throw new InvalidOperationException()
};
}

private void PublishDefaultValueNodeEvents(IInputValueDefinition inputValue, ValidationContext context)
{
if (inputValue.DefaultValue is { } defaultValue)
Expand Down
Loading
Loading