Skip to content

Commit 253bf65

Browse files
glen-84michaelstaib
authored andcommitted
Updated tag name validation to allow hyphens and relax requirements (#8304)
1 parent 26549ff commit 253bf65

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/HotChocolate/Core/src/Types/Properties/TypeResources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ Type: `{0}`</value>
977977
<value>The specified type `{0}` is expected to be an output type.</value>
978978
</data>
979979
<data name="TagDirective_Name_NotValid" xml:space="preserve">
980-
<value>The tag name must follow the GraphQL type name rules.</value>
980+
<value>Tag names may only include alphanumeric characters (a-z, A-Z, 0-9), hyphens, and underscores.</value>
981981
</data>
982982
<data name="TagDirective_Descriptor_NotSupported" xml:space="preserve">
983983
<value>Tag is not supported on the specified descriptor.</value>

src/HotChocolate/Core/src/Types/Types/Directives/Tag.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#nullable enable
22

3+
using System.Text.RegularExpressions;
34
using HotChocolate.Properties;
4-
using HotChocolate.Utilities;
55

66
namespace HotChocolate.Types;
77

@@ -47,7 +47,7 @@ interface Book {
4747
author: String!
4848
}
4949
""")]
50-
public sealed class Tag
50+
public sealed partial class Tag
5151
{
5252
/// <summary>
5353
/// Creates a new instance of <see cref="Tag"/>.
@@ -60,7 +60,9 @@ public sealed class Tag
6060
/// </exception>
6161
public Tag(string name)
6262
{
63-
if (!name.IsValidGraphQLName())
63+
ArgumentNullException.ThrowIfNull(name);
64+
65+
if (!ValidNameRegex().IsMatch(name))
6466
{
6567
throw new ArgumentException(
6668
TypeResources.TagDirective_Name_NotValid,
@@ -75,4 +77,7 @@ public Tag(string name)
7577
/// </summary>
7678
[GraphQLDescription("The name of the tag.")]
7779
public string Name { get; }
80+
81+
[GeneratedRegex("^[a-zA-Z0-9_-]+$")]
82+
private static partial Regex ValidNameRegex();
7883
}

src/HotChocolate/Core/test/Types.Tests/Types/Directives/TagDirectiveTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,65 @@ repeatable on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION |
4343
schema.MatchSnapshot();
4444
}
4545

46+
[Fact]
47+
public async Task ValidNames()
48+
{
49+
var exception = await Record.ExceptionAsync(
50+
async () => await new ServiceCollection()
51+
.AddGraphQL()
52+
.AddDocumentFromString(
53+
"""
54+
type Query {
55+
field: String
56+
@tag(name: "tag")
57+
@tag(name: "TAG")
58+
@tag(name: "TAG_123")
59+
@tag(name: "tag_123")
60+
@tag(name: "tag-123")
61+
}
62+
63+
directive @tag("The name of the tag." name: String!)
64+
repeatable on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION |
65+
ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE |
66+
INPUT_OBJECT | INPUT_FIELD_DEFINITION
67+
""")
68+
.UseField(_ => _ => default)
69+
.BuildSchemaAsync());
70+
71+
Assert.Null(exception);
72+
}
73+
74+
[Theory]
75+
[InlineData("tag name")]
76+
[InlineData("tag*")]
77+
[InlineData("@TAG")]
78+
[InlineData("tag=name")]
79+
[InlineData("tagK")] // K = Kelvin Sign (U+212A)
80+
public async Task InvalidNames(string tagName)
81+
{
82+
async Task Act() =>
83+
await new ServiceCollection()
84+
.AddGraphQL()
85+
.AddDocumentFromString(
86+
$$"""
87+
type Query {
88+
field: String @tag(name: "{{tagName}}")
89+
}
90+
91+
directive @tag("The name of the tag." name: String!)
92+
repeatable on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION |
93+
ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE |
94+
INPUT_OBJECT | INPUT_FIELD_DEFINITION
95+
""")
96+
.UseField(_ => _ => default)
97+
.BuildSchemaAsync();
98+
99+
Assert.Equal(
100+
"Tag names may only include alphanumeric characters (a-z, A-Z, 0-9), hyphens, and " +
101+
"underscores. (Parameter 'name')",
102+
(await Assert.ThrowsAsync<ArgumentException>(Act)).Message);
103+
}
104+
46105
[Tag("OnObjectType")]
47106
public class Query
48107
{

0 commit comments

Comments
 (0)