File tree Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Original file line number Diff line number Diff line change @@ -296,6 +296,17 @@ describe('Type System: Objects must have fields', () => {
296
296
) ;
297
297
} ) ;
298
298
299
+ it ( 'rejects an Object type with incorrectly named fields' , ( ) => {
300
+ expect (
301
+ ( ) => schemaWithFieldType ( new GraphQLObjectType ( {
302
+ name : 'SomeObject' ,
303
+ fields : { 'bad-name-with-dashes' : { type : GraphQLString } }
304
+ } ) )
305
+ ) . to . throw (
306
+ 'Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "bad-name-with-dashes" does not.'
307
+ ) ;
308
+ } ) ;
309
+
299
310
it ( 'rejects an Object type with incorrectly typed fields' , ( ) => {
300
311
expect (
301
312
( ) => schemaWithFieldType ( new GraphQLObjectType ( {
@@ -387,7 +398,7 @@ describe('Type System: Fields args must be properly named', () => {
387
398
} ) ;
388
399
return new GraphQLSchema ( { query : QueryType } ) ;
389
400
} ) . to . throw (
390
- 'Field arg names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "bad-name-with-dashes" does not.'
401
+ 'Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "bad-name-with-dashes" does not.'
391
402
) ;
392
403
} ) ;
393
404
Original file line number Diff line number Diff line change @@ -147,15 +147,12 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
147
147
var fieldMap = type . getFields ( ) ;
148
148
Object . keys ( fieldMap ) . forEach ( fieldName => {
149
149
var field = fieldMap [ fieldName ] ;
150
+ assertValidName ( fieldName ) ;
151
+
150
152
if ( field . args ) {
151
153
var fieldArgTypes = field . args . map ( arg => arg . type ) ;
152
154
153
- field . args . forEach ( arg =>
154
- invariant (
155
- / ^ [ _ a - z A - Z ] [ _ a - z A - Z 0 - 9 ] * $ / . test ( arg . name ) ,
156
- `Field arg names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ ` +
157
- `but "${ arg . name } " does not.`
158
- ) ) ;
155
+ field . args . forEach ( arg => assertValidName ( arg . name ) ) ;
159
156
160
157
reducedMap = fieldArgTypes . reduce ( typeMapReducer , reducedMap ) ;
161
158
}
@@ -166,6 +163,13 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
166
163
return reducedMap ;
167
164
}
168
165
166
+ function assertValidName ( name : string ) : void {
167
+ invariant (
168
+ / ^ [ _ a - z A - Z ] [ _ a - z A - Z 0 - 9 ] * $ / . test ( name ) ,
169
+ `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ ` +
170
+ `but "${ name } " does not.` ) ;
171
+ }
172
+
169
173
function assertObjectImplementsInterface (
170
174
object : GraphQLObjectType ,
171
175
iface : GraphQLInterfaceType
You can’t perform that action at this time.
0 commit comments