Validate type/field/argument names

This commit is contained in:
vladar 2016-11-02 00:14:44 +07:00
parent 3b0e52f254
commit 22e41a3729
7 changed files with 17 additions and 9 deletions

View File

@ -19,6 +19,7 @@ class Config
const SCALAR = 32; const SCALAR = 32;
const CALLBACK = 64; const CALLBACK = 64;
const ANY = 128; const ANY = 128;
const NAME = 256;
const OUTPUT_TYPE = 2048; const OUTPUT_TYPE = 2048;
const INPUT_TYPE = 4096; const INPUT_TYPE = 4096;
@ -253,6 +254,13 @@ class Config
case $def & self::SCALAR: case $def & self::SCALAR:
Utils::invariant(is_scalar($value), $err, 'scalar'); Utils::invariant(is_scalar($value), $err, 'scalar');
break; break;
case $def & self::NAME:
Utils::invariant(
preg_match('~^[_a-zA-Z][_a-zA-Z0-9]*$~', $value),
'Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%s" does not.',
$value
);
break;
case $def & self::INPUT_TYPE: case $def & self::INPUT_TYPE:
Utils::invariant( Utils::invariant(
is_callable($value) || $value instanceof InputType, is_callable($value) || $value instanceof InputType,

View File

@ -32,9 +32,9 @@ class EnumType extends Type implements InputType, OutputType, LeafType
} }
Config::validate($config, [ Config::validate($config, [
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'values' => Config::arrayOf([ 'values' => Config::arrayOf([
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'value' => Config::ANY, 'value' => Config::ANY,
'deprecationReason' => Config::STRING, 'deprecationReason' => Config::STRING,
'description' => Config::STRING 'description' => Config::STRING

View File

@ -73,10 +73,10 @@ class FieldDefinition
public static function getDefinition() public static function getDefinition()
{ {
return self::$def ?: (self::$def = [ return self::$def ?: (self::$def = [
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'type' => Config::OUTPUT_TYPE | Config::REQUIRED, 'type' => Config::OUTPUT_TYPE | Config::REQUIRED,
'args' => Config::arrayOf([ 'args' => Config::arrayOf([
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'type' => Config::INPUT_TYPE | Config::REQUIRED, 'type' => Config::INPUT_TYPE | Config::REQUIRED,
'description' => Config::STRING, 'description' => Config::STRING,
'defaultValue' => Config::ANY 'defaultValue' => Config::ANY

View File

@ -30,9 +30,9 @@ class InputObjectType extends Type implements InputType
} }
Config::validate($config, [ Config::validate($config, [
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'fields' => Config::arrayOf([ 'fields' => Config::arrayOf([
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'type' => Config::INPUT_TYPE | Config::REQUIRED, 'type' => Config::INPUT_TYPE | Config::REQUIRED,
'defaultValue' => Config::ANY, 'defaultValue' => Config::ANY,
'description' => Config::STRING 'description' => Config::STRING

View File

@ -40,7 +40,7 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
} }
Config::validate($config, [ Config::validate($config, [
'name' => Config::STRING, 'name' => Config::NAME,
'fields' => Config::arrayOf( 'fields' => Config::arrayOf(
FieldDefinition::getDefinition(), FieldDefinition::getDefinition(),
Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE

View File

@ -88,7 +88,7 @@ class ObjectType extends Type implements OutputType, CompositeType
// Note: this validation is disabled by default, because it is resource-consuming // Note: this validation is disabled by default, because it is resource-consuming
// TODO: add bin/validate script to check if schema is valid during development // TODO: add bin/validate script to check if schema is valid during development
Config::validate($config, [ Config::validate($config, [
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'fields' => Config::arrayOf( 'fields' => Config::arrayOf(
FieldDefinition::getDefinition(), FieldDefinition::getDefinition(),
Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE

View File

@ -41,7 +41,7 @@ class UnionType extends Type implements AbstractType, OutputType, CompositeType
} }
Config::validate($config, [ Config::validate($config, [
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::NAME | Config::REQUIRED,
'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::MAYBE_THUNK | Config::REQUIRED), 'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::MAYBE_THUNK | Config::REQUIRED),
'resolveType' => Config::CALLBACK, // function($value, ResolveInfo $info) => ObjectType 'resolveType' => Config::CALLBACK, // function($value, ResolveInfo $info) => ObjectType
'description' => Config::STRING 'description' => Config::STRING