Skip to content

Commit 4ff563e

Browse files
committed
Merge pull request #145 from jmandel/check-arg-names
Check for valid field arg names
2 parents d05d751 + c8592d8 commit 4ff563e

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/type/__tests__/validation.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ describe('Type System: Objects must have fields', () => {
296296
);
297297
});
298298

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+
299310
it('rejects an Object type with incorrectly typed fields', () => {
300311
expect(
301312
() => schemaWithFieldType(new GraphQLObjectType({
@@ -353,6 +364,47 @@ describe('Type System: Objects must have fields', () => {
353364
});
354365

355366

367+
describe('Type System: Fields args must be properly named', () => {
368+
369+
it('accepts field args with valid names', () => {
370+
expect(
371+
() => schemaWithFieldType(new GraphQLObjectType({
372+
name: 'SomeObject',
373+
fields: {
374+
goodField: {
375+
type: GraphQLString,
376+
args: {
377+
goodArg: { type: GraphQLString }
378+
}
379+
}
380+
}
381+
}))
382+
).not.to.throw();
383+
});
384+
385+
it('rejects field arg with invalid names', () => {
386+
expect(
387+
() => {
388+
var QueryType = new GraphQLObjectType({
389+
name: 'SomeObject',
390+
fields: {
391+
badField: {
392+
type: GraphQLString,
393+
args: {
394+
'bad-name-with-dashes': { type: GraphQLString }
395+
}
396+
}
397+
}
398+
});
399+
return new GraphQLSchema({ query: QueryType });
400+
}).to.throw(
401+
'Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "bad-name-with-dashes" does not.'
402+
);
403+
});
404+
405+
});
406+
407+
356408
describe('Type System: Fields args must be objects', () => {
357409

358410
it('accepts an Object type with field args', () => {

src/type/schema.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,13 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
147147
var fieldMap = type.getFields();
148148
Object.keys(fieldMap).forEach(fieldName => {
149149
var field = fieldMap[fieldName];
150+
assertValidName(fieldName);
151+
150152
if (field.args) {
151153
var fieldArgTypes = field.args.map(arg => arg.type);
154+
155+
field.args.forEach(arg => assertValidName(arg.name));
156+
152157
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
153158
}
154159
reducedMap = typeMapReducer(reducedMap, field.type);
@@ -158,6 +163,13 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
158163
return reducedMap;
159164
}
160165

166+
function assertValidName(name: string): void {
167+
invariant(
168+
/^[_a-zA-Z][_a-zA-Z0-9]*$/.test(name),
169+
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ ` +
170+
`but "${name}" does not.`);
171+
}
172+
161173
function assertObjectImplementsInterface(
162174
object: GraphQLObjectType,
163175
iface: GraphQLInterfaceType

0 commit comments

Comments
 (0)