Skip to content

Commit 21d7d4b

Browse files
committed
Validate name
1 parent 1e815bd commit 21d7d4b

13 files changed

+37
-13
lines changed

src/main/java/graphql/Assert.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,18 @@ public static <T> Collection<T> assertNotEmpty(Collection<T> c, String errorMess
1515
return c;
1616
}
1717

18+
private static final String invalidNameErrorMessage = "Name must be non-null, non-empty and match [_A-Za-z][_0-9A-Za-z]*";
19+
/**
20+
* Validates that the Lexical token name matches the current spec.
21+
* currently non null, non empty,
22+
* @param name - the name to be validated.
23+
* @return the name if valid, or AssertException if invalid.
24+
*/
25+
public static String assertValidName(String name) {
26+
if (name != null && !name.isEmpty() && name.matches("[_A-Za-z][_0-9A-Za-z]*")) {
27+
return name;
28+
}
29+
throw new AssertException(invalidNameErrorMessage);
30+
}
31+
1832
}

src/main/java/graphql/schema/GraphQLArgument.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55

66
import static graphql.Assert.assertNotNull;
7+
import static graphql.Assert.assertValidName;
78

89
public class GraphQLArgument {
910

@@ -13,7 +14,7 @@ public class GraphQLArgument {
1314
private final Object defaultValue;
1415

1516
public GraphQLArgument(String name, String description, GraphQLInputType type, Object defaultValue) {
16-
assertNotNull(name, "name can't be null");
17+
assertValidName(name);
1718
assertNotNull(type, "type can't be null");
1819
this.name = name;
1920
this.description = description;

src/main/java/graphql/schema/GraphQLDirective.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77

88
import static graphql.Assert.assertNotNull;
9+
import static graphql.Assert.assertValidName;
910
import static graphql.introspection.Introspection.DirectiveLocation;
1011

1112
public class GraphQLDirective {
@@ -20,7 +21,7 @@ public class GraphQLDirective {
2021

2122
public GraphQLDirective(String name, String description, EnumSet<DirectiveLocation> locations,
2223
List<GraphQLArgument> arguments, boolean onOperation, boolean onFragment, boolean onField) {
23-
assertNotNull(name, "name can't be null");
24+
assertValidName(name);
2425
assertNotNull(arguments, "arguments can't be null");
2526
this.name = name;
2627
this.description = description;

src/main/java/graphql/schema/GraphQLEnumType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010
import java.util.Map;
1111

12-
import static graphql.Assert.assertNotNull;
12+
import static graphql.Assert.assertValidName;
1313

1414
public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLUnmodifiedType {
1515

@@ -63,7 +63,7 @@ public List<GraphQLEnumValueDefinition> getValues() {
6363

6464

6565
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values) {
66-
assertNotNull(name, "name can't be null");
66+
assertValidName(name);
6767
this.name = name;
6868
this.description = description;
6969
buildMap(values);

src/main/java/graphql/schema/GraphQLEnumValueDefinition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import static graphql.Assert.assertNotNull;
5+
import static graphql.Assert.assertValidName;
56

67
public class GraphQLEnumValueDefinition {
78

@@ -11,7 +12,7 @@ public class GraphQLEnumValueDefinition {
1112
private final String deprecationReason;
1213

1314
public GraphQLEnumValueDefinition(String name, String description, Object value, String deprecationReason) {
14-
assertNotNull(name, "name can't be null");
15+
assertValidName(name);
1516
this.name = name;
1617
this.description = description;
1718
this.value = value;

src/main/java/graphql/schema/GraphQLFieldDefinition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77

88
import static graphql.Assert.assertNotNull;
9+
import static graphql.Assert.assertValidName;
910

1011
public class GraphQLFieldDefinition {
1112

@@ -18,7 +19,7 @@ public class GraphQLFieldDefinition {
1819

1920

2021
public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, DataFetcher dataFetcher, List<GraphQLArgument> arguments, String deprecationReason) {
21-
assertNotNull(name, "name can't be null");
22+
assertValidName(name);
2223
assertNotNull(dataFetcher, "dataFetcher can't be null");
2324
assertNotNull(type, "type can't be null");
2425
assertNotNull(arguments, "arguments can't be null");

src/main/java/graphql/schema/GraphQLInputObjectField.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55

66
import static graphql.Assert.assertNotNull;
7+
import static graphql.Assert.assertValidName;
78

89
public class GraphQLInputObjectField {
910

@@ -17,7 +18,7 @@ public GraphQLInputObjectField(String name, GraphQLInputType type) {
1718
}
1819

1920
public GraphQLInputObjectField(String name, String description, GraphQLInputType type, Object defaultValue) {
20-
assertNotNull(name, "name can't be null");
21+
assertValidName(name);
2122
assertNotNull(type, "type can't be null");
2223
this.name = name;
2324
this.type = type;

src/main/java/graphql/schema/GraphQLInputObjectType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import graphql.AssertException;
1010

1111
import static graphql.Assert.assertNotNull;
12+
import static graphql.Assert.assertValidName;
1213

1314
public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, GraphQLUnmodifiedType, GraphQLNullableType, GraphQLInputFieldsContainer {
1415

@@ -19,7 +20,7 @@ public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, Gr
1920
private final Map<String, GraphQLInputObjectField> fieldMap = new LinkedHashMap<String, GraphQLInputObjectField>();
2021

2122
public GraphQLInputObjectType(String name, String description, List<GraphQLInputObjectField> fields) {
22-
assertNotNull(name, "name can't be null");
23+
assertValidName(name);
2324
assertNotNull(fields, "fields can't be null");
2425
this.name = name;
2526
this.description = description;

src/main/java/graphql/schema/GraphQLInterfaceType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import graphql.AssertException;
1010

1111
import static graphql.Assert.assertNotNull;
12+
import static graphql.Assert.assertValidName;
1213

1314
public class GraphQLInterfaceType implements GraphQLType, GraphQLOutputType, GraphQLFieldsContainer, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType {
1415

@@ -18,7 +19,7 @@ public class GraphQLInterfaceType implements GraphQLType, GraphQLOutputType, Gra
1819
private final TypeResolver typeResolver;
1920

2021
public GraphQLInterfaceType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions, TypeResolver typeResolver) {
21-
assertNotNull(name, "name can't null");
22+
assertValidName(name);
2223
assertNotNull(typeResolver, "typeResolver can't null");
2324
assertNotNull(fieldDefinitions, "fieldDefinitions can't null");
2425
this.name = name;

src/main/java/graphql/schema/GraphQLObjectType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import graphql.AssertException;
1010

1111
import static graphql.Assert.assertNotNull;
12+
import static graphql.Assert.assertValidName;
1213

1314
public class GraphQLObjectType implements GraphQLType, GraphQLOutputType, GraphQLFieldsContainer, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType {
1415

@@ -19,7 +20,7 @@ public class GraphQLObjectType implements GraphQLType, GraphQLOutputType, GraphQ
1920

2021
public GraphQLObjectType(String name, String description, List<GraphQLFieldDefinition> fieldDefinitions,
2122
List<GraphQLInterfaceType> interfaces) {
22-
assertNotNull(name, "name can't be null");
23+
assertValidName(name);
2324
assertNotNull(fieldDefinitions, "fieldDefinitions can't be null");
2425
assertNotNull(interfaces, "interfaces can't be null");
2526
assertNotNull(interfaces, "unresolvedInterfaces can't be null");

0 commit comments

Comments
 (0)