Skip to content

Commit 2edab8b

Browse files
committed
Check for valid field arg names
1 parent 5e9c79a commit 2edab8b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/type/__tests__/validation.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,47 @@ describe('Type System: Objects must have fields', () => {
353353
});
354354

355355

356+
describe('Type System: Fields args must be properly named', () => {
357+
358+
it('accepts field args with valid names', () => {
359+
expect(
360+
() => schemaWithFieldType(new GraphQLObjectType({
361+
name: 'SomeObject',
362+
fields: {
363+
goodField: {
364+
type: GraphQLString,
365+
args: {
366+
goodArg: { type: GraphQLString }
367+
}
368+
}
369+
}
370+
}))
371+
).not.to.throw();
372+
});
373+
374+
it('rejects field arg with invalid names', () => {
375+
expect(
376+
() => {
377+
var QueryType = new GraphQLObjectType({
378+
name: 'SomeObject',
379+
fields: {
380+
badField: {
381+
type: GraphQLString,
382+
args: {
383+
'bad-name-with-dashes': { type: GraphQLString }
384+
}
385+
}
386+
}
387+
});
388+
return new GraphQLSchema({ query: QueryType });
389+
}).to.throw(
390+
'Field arg names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "bad-name-with-dashes" does not.'
391+
);
392+
});
393+
394+
});
395+
396+
356397
describe('Type System: Fields args must be objects', () => {
357398

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

src/type/schema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
149149
var field = fieldMap[fieldName];
150150
if (field.args) {
151151
var fieldArgTypes = field.args.map(arg => arg.type);
152+
153+
field.args.forEach(arg =>
154+
invariant(
155+
/^[_a-zA-Z][_a-zA-Z0-9]*$/.test(arg.name),
156+
`Field arg names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ ` +
157+
`but "${arg.name}" does not.`
158+
));
159+
152160
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
153161
}
154162
reducedMap = typeMapReducer(reducedMap, field.type);

0 commit comments

Comments
 (0)