From b74a952c4ff1613524d42f52b9549d3450fffa29 Mon Sep 17 00:00:00 2001 From: Fred Zhang Date: Sat, 10 Jun 2023 08:18:46 -0700 Subject: [PATCH] Validate enum values directive are legit (#257) * validate enum values directive is legit * Add tests --- validator/schema.go | 3 +++ validator/schema_test.yml | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/validator/schema.go b/validator/schema.go index fdfabf0b..21eb51a5 100644 --- a/validator/schema.go +++ b/validator/schema.go @@ -284,6 +284,9 @@ func validateDefinition(schema *Schema, def *Definition) *gqlerror.Error { return gqlerror.ErrorPosf(def.Position, "%s %s: non-enum value %s.", def.Kind, def.Name, value.Name) } } + if err := validateDirectives(schema, value.Directives, LocationEnumValue, nil); err != nil { + return err + } } case InputObject: if len(def.Fields) == 0 { diff --git a/validator/schema_test.yml b/validator/schema_test.yml index 2a9feed3..c16caabb 100644 --- a/validator/schema_test.yml +++ b/validator/schema_test.yml @@ -528,7 +528,16 @@ directives: directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT - - name: must be declared + - name: must be declared (type) + input: | + type User @foo { + name: String + } + error: + message: "Undefined directive foo." + locations: [{line: 1, column: 12}] + + - name: must be declared (field) input: | type User { name: String @foo @@ -537,6 +546,15 @@ directives: message: "Undefined directive foo." locations: [{line: 2, column: 17}] + - name: must be declared (enum) + input: | + enum Unit { + METER @foo + } + error: + message: "Undefined directive foo." + locations: [{line: 2, column: 10}] + - name: cannot be self-referential input: | directive @A(foo: Int! @A) on FIELD_DEFINITION