Skip to content

Commit

Permalink
addressed comments in PR: move file, update comment, filter out more …
Browse files Browse the repository at this point in the history
…options, remove redundant warning
  • Loading branch information
yuzhi committed Apr 13, 2016
1 parent 10997c5 commit 8982f88
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ describe('suggestionList', () => {

it('Returns options sorted based on simularity', () => {
expect(suggestionList('abc', [ 'a', 'ab', 'abc' ]))
.to.deep.equal([ 'abc', 'ab', 'a' ]);
.to.deep.equal([ 'abc', 'ab' ]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
*/

/**
* Given a JavaScript value and a GraphQL type, determine if the value will be
* accepted for that type. This is primarily useful for validating the
* runtime values of query variables.
* Given an invalid input string and a list of valid options, returns a filtered
* list of valid options sorted based on their simularity with the input.
*/
export function suggestionList(
input: string,
Expand All @@ -20,10 +19,15 @@ export function suggestionList(
let i;
const d = {};
const oLength = options.length;
const inputThreshold = input.length / 2;
for (i = 0; i < oLength; i++) {
d[options[i]] = lexicalDistance(input, options[i]);
const distance = lexicalDistance(input, options[i]);
const threshold = Math.max(inputThreshold, options[i].length / 2, 1);
if (distance <= threshold) {
d[options[i]] = distance;
}
}
const result = options.slice();
const result = Object.keys(d);
return result.sort((a , b) => d[a] - d[b]);
}

Expand Down
53 changes: 9 additions & 44 deletions src/validation/__tests__/FieldsOnCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ describe('Validate: Fields on correct type', () => {
}
}
}`,
[ undefinedField('unknown_pet_field', 'Pet', [], [ 'name' ], 3, 9),
[ undefinedField('unknown_pet_field', 'Pet', [], [], 3, 9),
undefinedField(
'unknown_cat_field',
'Cat',
[],
[ 'nickname', 'name', 'meowVolume', 'meows', 'furColor' ],
[],
5,
13
)
Expand All @@ -119,14 +119,7 @@ describe('Validate: Fields on correct type', () => {
'meowVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'doesKnowCommand',
'isAtLocation',
'isHousetrained',
],
[ 'barkVolume' ],
3,
9
)
Expand All @@ -145,14 +138,7 @@ describe('Validate: Fields on correct type', () => {
'unknown_field',
'Dog',
[],
[ 'nickname',
'name',
'barkVolume',
'doesKnowCommand',
'isHousetrained',
'isAtLocation',
'barks',
],
[],
3,
9
)
Expand All @@ -167,7 +153,7 @@ describe('Validate: Fields on correct type', () => {
unknown_field
}
}`,
[ undefinedField('unknown_field', 'Pet', [], [ 'name' ], 4, 11) ]
[ undefinedField('unknown_field', 'Pet', [], [], 4, 11) ]
);
});

Expand All @@ -182,14 +168,7 @@ describe('Validate: Fields on correct type', () => {
'meowVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'doesKnowCommand',
'isAtLocation',
'isHousetrained',
],
[ 'barkVolume' ],
4,
11
)
Expand All @@ -206,14 +185,7 @@ describe('Validate: Fields on correct type', () => {
'mooVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'isAtLocation',
'doesKnowCommand',
'isHousetrained',
],
[ 'barkVolume' ],
3,
9
)
Expand All @@ -230,14 +202,7 @@ describe('Validate: Fields on correct type', () => {
'kawVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'isAtLocation',
'doesKnowCommand',
'isHousetrained',
],
[ 'barkVolume' ],
3,
9
)
Expand All @@ -250,7 +215,7 @@ describe('Validate: Fields on correct type', () => {
fragment notDefinedOnInterface on Pet {
tailLength
}`,
[ undefinedField('tailLength', 'Pet', [], [ 'name' ], 3, 9) ]
[ undefinedField('tailLength', 'Pet', [], [], 3, 9) ]
);
});

Expand Down
12 changes: 6 additions & 6 deletions src/validation/__tests__/KnownArgumentNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('Validate: Known argument names', () => {
dog @skip(unless: true)
}
`, [
unknownDirectiveArg('unless', 'skip', [ 'if' ], 3, 19),
unknownDirectiveArg('unless', 'skip', [], 3, 19),
]);
});

Expand All @@ -119,7 +119,7 @@ describe('Validate: Known argument names', () => {
doesKnowCommand(unknown: true)
}
`, [
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 3, 25),
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 25),
]);
});

Expand All @@ -129,8 +129,8 @@ describe('Validate: Known argument names', () => {
doesKnowCommand(whoknows: 1, dogCommand: SIT, unknown: true)
}
`, [
unknownArg('whoknows', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 3, 25),
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 3, 55),
unknownArg('whoknows', 'doesKnowCommand', 'Dog', [], 3, 25),
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 55),
]);
});

Expand All @@ -149,8 +149,8 @@ describe('Validate: Known argument names', () => {
}
}
`, [
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 4, 27),
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 9, 31),
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 4, 27),
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 9, 31),
]);
});

Expand Down
28 changes: 4 additions & 24 deletions src/validation/__tests__/KnownTypeNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,19 @@ describe('Validate: Known type names', () => {
`, [
unknownType(
'JumbledUpLetters',
[ 'ComplexInput',
'ComplicatedArgs',
'QueryRoot',
'Intelligent',
'HumanOrAlien'
],
[],
2,
23
),
unknownType(
'Badger',
[ 'Dog',
'Boolean',
'Pet',
'Alien',
'Being'
],
[],
5,
25
),
unknownType(
'Peettt',
[ 'Pet',
'Float',
'Being',
'Cat',
'Int'
],
[ 'Pet' ],
8,
29
)
Expand All @@ -105,12 +90,7 @@ describe('Validate: Known type names', () => {
`, [
unknownType(
'NotInTheSchema',
[ '__Schema',
'Intelligent',
'HumanOrAlien',
'Int',
'Canine'
],
[],
12,
23
),
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/FieldsOnCorrectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import type { ValidationContext } from '../index';
import { GraphQLError } from '../../error';
import { suggestionList } from '../../jsutils/suggestionList';
import type { Field } from '../../language/ast';
import type { GraphQLSchema } from '../../type/schema';
import type { GraphQLAbstractType } from '../../type/definition';
Expand All @@ -19,7 +20,6 @@ import {
GraphQLInputObjectType,
GraphQLInterfaceType
} from '../../type/definition';
import { suggestionList } from '../../utilities/suggestionList';


export function undefinedFieldMessage(
Expand Down Expand Up @@ -88,7 +88,7 @@ export function FieldsOnCorrectType(context: ValidationContext): any {
type instanceof GraphQLInputObjectType) {
suggestedFields = suggestionList(
node.name.value,
Object.keys(schema.getType(type.name).getFields())
Object.keys(type.getFields())
);
}
context.reportError(new GraphQLError(
Expand Down
7 changes: 2 additions & 5 deletions src/validation/rules/KnownArgumentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import type { ValidationContext } from '../index';
import { GraphQLError } from '../../error';
import find from '../../jsutils/find';
import invariant from '../../jsutils/invariant';
import { suggestionList } from '../../jsutils/suggestionList';
import {
FIELD,
DIRECTIVE
} from '../../language/kinds';
import type { GraphQLType } from '../../type/definition';
import { suggestionList } from '../../utilities/suggestionList';


export function unknownArgMessage(
argName: string,
Expand All @@ -32,8 +33,6 @@ export function unknownArgMessage(
.map(t => `"${t}"`)
.join(', ');
message += ` Perhaps you meant ${suggestions}?`;
} else {
message += ' There is no known argument for this field.';
}
return message;
}
Expand All @@ -50,8 +49,6 @@ export function unknownDirectiveArgMessage(
.map(t => `"${t}"`)
.join(', ');
message += ` Perhaps you meant ${suggestions}?`;
} else {
message += ' There is no known argument for this directive.';
}
return message;
}
Expand Down
3 changes: 2 additions & 1 deletion src/validation/rules/KnownTypeNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

import type { ValidationContext } from '../index';
import { GraphQLError } from '../../error';
import { suggestionList } from '../../jsutils/suggestionList';
import type { GraphQLType } from '../../type/definition';
import { suggestionList } from '../../utilities/suggestionList';


export function unknownTypeMessage(
type: GraphQLType,
Expand Down

0 comments on commit 8982f88

Please sign in to comment.