Skip to content

Adds isValidValue and isValidLiteral to Scalar and Enum types. #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import invariant from '../jsutils/invariant';
import isNullish from '../jsutils/isNullish';
import { ENUM } from '../language/kinds';
import { assertValidName } from '../utilities/assertValidName';
import type {
Expand Down Expand Up @@ -323,16 +324,28 @@ export class GraphQLScalarType {
return serializer(value);
}

// Determines if an internal value is valid for this type.
// Equivalent to checking for if the parsedValue is nullish.
isValidValue(value: mixed): boolean {
return !isNullish(this.parseValue(value));
}

// Parses an externally provided value to use as an input.
parseValue(value: mixed): mixed {
const parser = this._scalarConfig.parseValue;
return parser ? parser(value) : null;
return parser && !isNullish(value) ? parser(value) : undefined;
}

// Determines if an internal value is valid for this type.
// Equivalent to checking for if the parsedLiteral is nullish.
isValidLiteral(valueNode: ValueNode): boolean {
return !isNullish(this.parseLiteral(valueNode));
}

// Parses an externally provided literal value to use as an input.
parseLiteral(valueNode: ValueNode): mixed {
const parser = this._scalarConfig.parseLiteral;
return parser ? parser(valueNode) : null;
return parser ? parser(valueNode) : undefined;
}

toString(): string {
Expand Down Expand Up @@ -896,6 +909,11 @@ export class GraphQLEnumType/* <T> */ {
return enumValue ? enumValue.name : null;
}

isValidValue(value: mixed): boolean {
return typeof value === 'string' &&
this._getNameLookup()[value] !== undefined;
}

parseValue(value: mixed): ?any/* T */ {
if (typeof value === 'string') {
const enumValue = this._getNameLookup()[value];
Expand All @@ -905,6 +923,11 @@ export class GraphQLEnumType/* <T> */ {
}
}

isValidLiteral(valueNode: ValueNode): boolean {
return valueNode.kind === ENUM &&
this._getNameLookup()[valueNode.value] !== undefined;
}

parseLiteral(valueNode: ValueNode): ?any/* T */ {
if (valueNode.kind === ENUM) {
const enumValue = this._getNameLookup()[valueNode.value];
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/isValidJSValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function isValidJSValue(
// a non-null value.
try {
const parseResult = type.parseValue(value);
if (isNullish(parseResult)) {
if (isNullish(parseResult) && !type.isValidValue(value)) {
return [
`Expected type "${type.name}", found ${JSON.stringify(value)}.`
];
Expand Down
8 changes: 2 additions & 6 deletions src/utilities/isValidLiteralValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
import type { GraphQLInputType } from '../type/definition';
import invariant from '../jsutils/invariant';
import keyMap from '../jsutils/keyMap';
import isNullish from '../jsutils/isNullish';


/**
Expand Down Expand Up @@ -115,11 +114,8 @@ export function isValidLiteralValue(
'Must be input type'
);

// Scalars must parse to a non-null value, Enums may be null but must
// serialize back to a named value.
const parseResult = type.parseLiteral(valueNode);
if (type instanceof GraphQLEnumType ?
!type.serialize(parseResult) : isNullish(parseResult)) {
// Scalars determine if a literal values is valid.
if (!type.isValidLiteral(valueNode)) {
return [ `Expected type "${type.name}", found ${print(valueNode)}.` ];
}

Expand Down
8 changes: 3 additions & 5 deletions src/utilities/valueFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,9 @@ export function valueFromAST(
);

const parsed = type.parseLiteral(valueNode);
if (type instanceof GraphQLEnumType ?
!type.serialize(parsed) : isNullish(parsed)) {
// null or invalid values represent a failure to parse correctly (unless
// we have a legitimately null-valued Enum), in which case no value is
// returned.
if (isNullish(parsed) && !type.isValidLiteral(valueNode)) {
// Invalid values represent a failure to parse correctly, in which case
// no value is returned.
return;
}

Expand Down