Skip to content

Commit 820d88c

Browse files
authored
Simplify ListOf (#7)
* fix non-null case * linting * linting * fix validate logic, add new test * add files * remove unused code * fix validation * exclude tests from analysis * ws * remove unused, use constants * rename fields back to suberrors
1 parent 3fd4717 commit 820d88c

File tree

23 files changed

+1619
-1041
lines changed

23 files changed

+1619
-1041
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
filter:
22
excluded_paths:
33
- 'examples/*'
4+
- 'tests/'
45
build:
56
nodes:
67
analysis:

README.md

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ If you want to return an error message, return an array with the message in the
135135
]
136136
])
137137
```
138-
Note that `ListOf` types are a special case, and support an additional `validateItem` callback for checking each item in their array. Its generated error type will have an array of suberror types, each with their own `path` field that you can query so you can know the exact address in the multidimensional array of each item that failed validation:
138+
139+
Generated `ListOf` error types also have a `path` field that you can query so you can know the exact address in the multidimensional array of each item that failed validation:
139140

140141
```php
141142
//...
@@ -144,13 +145,7 @@ Note that `ListOf` types are a special case, and support an additional `validate
144145
'args' => [
145146
'phoneNumbers' => [
146147
'type' => Type::listOf(Type::string()),
147-
'validate' => function(array $phoneNumbers) {
148-
if(!count($phoneNumbers)) {
149-
return [1, "At least one phone number is required"];
150-
}
151-
return 0;
152-
},
153-
'validateItem' => function(string $phoneNumber) {
148+
'validate' => function(string $phoneNumber) {
154149
$res = preg_match('/^[0-9\-]+$/', $phoneNumber) === 1;
155150
if (!$res) {
156151
return [1, 'That does not seem to be a valid phone number'];
@@ -182,39 +177,7 @@ If you would like to use custom error codes, add an `errorCodes` property at the
182177
]
183178
```
184179

185-
`ListOf` types are again a special case, and support an additional `suberrorCodes` property for validating their items:
186180

187-
```php
188-
//...
189-
'setPhoneNumbers' => new ValidatedFieldDefinition([
190-
'type' => Types::bool(),
191-
'args' => [
192-
'phoneNumbers' => [
193-
'errorCodes' => [
194-
'atLeastOneRequired`
195-
],
196-
'suberrorCodes' => [
197-
'invalidPhoneNumber'
198-
],
199-
'type' => Type::listOf(Type::string()),
200-
'validate' => function(array $phoneNumbers) {
201-
if(!count($phoneNumbers)) {
202-
return ['atLeastOneRequired', "At least one phone number is required"];
203-
}
204-
return 0;
205-
},
206-
'validateItem' => function(string $phoneNumber) {
207-
$res = preg_match('/^[0-9\-]+$/', $phoneNumber) === 1;
208-
if (!$res) {
209-
return ['invalidPhoneNumber', 'That does not seem to be a valid phone number'];
210-
}
211-
return 0;
212-
}
213-
]
214-
]
215-
])
216-
```
217-
218181
### Managing Created Types
219182
This library will create new types as needed. If you are using some kind of type manager to store and retrieve types, you can integrate it by providing a `typeSetter` callback:
220183

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
1-
# Simple scalar validation
2-
The simplest possible type of user input validation. Mutation expects an `authorId` arg, and will respond with a nicely formatted error code and message if an author by that id doesn't exist.
3-
4-
### Run locally
5-
```
6-
php -S localhost:8000 ./index.php
7-
```
8-
9-
### Install ChromeiQL plug-in for Chrome
10-
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
11-
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
12-
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
13-
14-
### Try mutation with valid input
15-
```
16-
mutation {
17-
updateAuthor(authorId:1) {
18-
suberrors {
19-
authorId {
20-
code
21-
msg
22-
}
23-
}
24-
result {
25-
id
26-
name
27-
}
28-
valid
29-
}
30-
}
31-
```
32-
33-
### Try mutation with invalid input
34-
```
35-
mutation {
36-
updateAuthor(authorId:999) {
37-
suberrors {
38-
authorId {
39-
code
40-
msg
41-
}
42-
}
43-
result {
44-
id
45-
name
46-
}
47-
valid
48-
}
49-
}
1+
# Simple scalar validation
2+
The simplest possible type of user input validation. Mutation expects an `authorId` arg, and will respond with a nicely formatted error code and message if an author by that id doesn't exist.
3+
4+
### Run locally
5+
```
6+
php -S localhost:8000 ./index.php
7+
```
8+
9+
### Install ChromeiQL plug-in for Chrome
10+
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
11+
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
12+
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
13+
14+
### Try mutation with valid input
15+
```
16+
mutation {
17+
deleteAuthor(id: 1) {
18+
valid
19+
result
20+
}
21+
}
22+
```
23+
24+
### Try mutation with invalid input
25+
```
26+
mutation {
27+
deleteAuthor(id: 3) {
28+
valid
29+
result
30+
suberrors {
31+
id {
32+
code
33+
msg
34+
}
35+
}
36+
}
37+
}
5038
```

examples/01-basic-scalar-validation/index.php

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,50 +47,49 @@ public function __construct()
4747
$mutationType = new ObjectType([
4848
'name' => 'Mutation',
4949
'fields' => [
50-
'updateAuthor' => new ValidatedFieldDefinition([
51-
'name' => 'updateAuthor',
52-
'type' => $authorType,
50+
'deleteAuthor' => new ValidatedFieldDefinition([
51+
'name' => 'deleteAuthor',
52+
'type' => Type::boolean(),
5353
'args' => [
54-
'authorId' => [
54+
'id' => [
5555
'type' => Type::id(),
5656
'validate' => function(string $authorId) use ($authors) {
5757
if (isset($authors[$authorId])) {
5858
return 0;
5959
}
6060

61-
return [0, "Unknown author"];
61+
return [1, "Unknown author"];
6262
}
6363
],
6464
],
6565
'resolve' => function ($value, $args) use ($authors) {
6666
// do your operation on the author
6767
// AuthorProvider::update($authorId);
68-
return $authors[$args['authorId']];
68+
unset($authors[$args['id']]);
69+
return true;
6970
},
7071
]),
7172
],
7273
]);
7374

74-
$queryType = new ObjectType([
75-
'name'=>'Query',
76-
'fields'=>[
77-
'author'=> [
78-
'type' => $authorType,
79-
"args" => [
80-
'authorId' => [
81-
'type' => Type::id()
82-
]
83-
],
84-
'resolve' => function($value, $args) use ($authors) {
85-
return $authors[$args['authorId']];
86-
}
87-
]
88-
]
89-
]);
90-
9175
$schema = new Schema([
9276
'mutation' => $mutationType,
93-
'query' => $queryType
77+
'query' => new ObjectType([
78+
'name'=>'Query',
79+
'fields'=>[
80+
'author'=> [
81+
'type' => $authorType,
82+
"args" => [
83+
'authorId' => [
84+
'type' => Type::id()
85+
]
86+
],
87+
'resolve' => function($value, $args) use ($authors) {
88+
return $authors[$args['authorId']];
89+
}
90+
]
91+
]
92+
])
9493
]);
9594

9695

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
1-
# Simple scalar validation
2-
If you supply an `errorCodes` property on your `fields` or `args` definitions, then a custom, unique error code type will be created.
3-
4-
### Run locally
5-
```
6-
php -S localhost:8000 ./index.php
7-
```
8-
9-
### Install ChromeiQL plug-in for Chrome
10-
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
11-
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
12-
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
13-
14-
### Try mutation with valid input
15-
```
16-
mutation {
17-
updateAuthor(authorId:1) {
18-
suberrors {
19-
authorId {
20-
code
21-
msg
22-
}
23-
}
24-
result {
25-
id
26-
name
27-
}
28-
valid
29-
}
30-
}
31-
```
32-
33-
### Try mutation with invalid input
34-
```
35-
mutation {
36-
updateAuthor(authorId:999) {
37-
suberrors {
38-
authorId {
39-
code
40-
msg
41-
}
42-
}
43-
result {
44-
id
45-
name
46-
}
47-
valid
48-
}
49-
}
1+
# Simple scalar validation
2+
If you supply an `errorCodes` property on your `fields` or `args` definitions, then a custom, unique error code type will be created.
3+
4+
### Run locally
5+
```
6+
php -S localhost:8000 ./index.php
7+
```
8+
9+
### Install ChromeiQL plug-in for Chrome
10+
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
11+
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
12+
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
13+
14+
### Try mutation with valid input
15+
```
16+
mutation {
17+
deleteAuthor(id: 1) {
18+
valid
19+
result
20+
suberrors {
21+
id {
22+
code
23+
msg
24+
}
25+
}
26+
}
27+
}
28+
```
29+
30+
### Try mutation with invalid input
31+
```
32+
mutation {
33+
deleteAuthor(id: 3) {
34+
valid
35+
result
36+
suberrors {
37+
id {
38+
code
39+
msg
40+
}
41+
}
42+
}
43+
}
5044
```

0 commit comments

Comments
 (0)