Skip to content

Commit 9807335

Browse files
authored
Support for required and partial (#14)
* partial and required support
1 parent 3d6b1ce commit 9807335

File tree

10 files changed

+624
-246
lines changed

10 files changed

+624
-246
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,36 @@ If the value is valid, return `0`, otherwise `1`.
117117
])
118118
```
119119

120+
### The `required` property
121+
You can mark any field as `required`, and if the value is not provided, then an automatic validation will happen for you (thus removing the need for you to weaken your validation callback with `null` types). You can set it to `true`, or you can provide an error array similar to the one returned by your validate callback:
122+
123+
```php
124+
//...
125+
'updateThing' => new ValidatedFieldDefinition([
126+
'type' => Types::thing(),
127+
'args' => [
128+
'foo' => [
129+
'required' => true, // if not provided, then an error of the form [1, 'foo is required'] will be returned.
130+
'validate' => function(string $foo) {
131+
if(Foo::find($foo)) {
132+
return 0;
133+
}
134+
return 1;
135+
}
136+
],
137+
'bar' => [
138+
'required' => [1, 'Oh, where is the bar?!'],
139+
'validate' => function(string $bar) {
140+
if(Bar::find($bar)) {
141+
return 0;
142+
}
143+
return 1;
144+
}
145+
]
146+
]
147+
])
148+
```
149+
120150
If you want to return an error message, return an array with the message in the second bucket:
121151
```php
122152
//...
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +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-
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-
}
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+
}
3838
```
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +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-
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-
}
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+
}
4444
```
Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
1-
# Validation of InputObjects (or Objects)
2-
3-
You can add `validate` properties to the fields of nested Objects or InputObjects (and those fields can themselves be of complex types with their own fields, and so on). This library will sniff them out and recursively build up a result type with a similarly nested structure.
4-
5-
6-
### Run locally
7-
```
8-
php -S localhost:8000 ./index.php
9-
```
10-
11-
### Install ChromeiQL plug-in for Chrome
12-
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
13-
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
14-
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
15-
16-
### Try mutation with valid input
17-
```
18-
mutation {
19-
updateAuthor(
20-
authorId: 1
21-
attributes: {
22-
name: "Stephen King",
23-
age: 47
24-
}
25-
) {
26-
result {
27-
id
28-
name
29-
}
30-
suberrors {
31-
attributes {
32-
code
33-
msg
34-
suberrors {
35-
age {
36-
code
37-
msg
38-
}
39-
name {
40-
code
41-
msg
42-
}
43-
}
44-
}
45-
}
46-
}
47-
}
48-
```
49-
50-
### Try mutation with invalid input
51-
```
52-
mutation {
53-
updateAuthor(
54-
authorId: 1
55-
attributes: {
56-
name: "Edward John Moreton Drax Plunkett, 18th Baron of Dunsany",
57-
age: -3
58-
}
59-
) {
60-
result {
61-
id
62-
name
63-
}
64-
suberrors {
65-
attributes {
66-
code
67-
msg
68-
suberrors {
69-
age {
70-
code
71-
msg
72-
}
73-
name {
74-
code
75-
msg
76-
}
77-
}
78-
}
79-
}
80-
}
81-
}
1+
# Validation of InputObjects (or Objects)
2+
3+
You can add `validate` properties to the fields of nested Objects or InputObjects (and those fields can themselves be of complex types with their own fields, and so on). This library will sniff them out and recursively build up a result type with a similarly nested structure.
4+
5+
6+
### Run locally
7+
```
8+
php -S localhost:8000 ./index.php
9+
```
10+
11+
### Install ChromeiQL plug-in for Chrome
12+
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
13+
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
14+
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
15+
16+
### Try mutation with valid input
17+
```
18+
mutation {
19+
updateAuthor(
20+
authorId: 1
21+
attributes: {
22+
name: "Stephen King",
23+
age: 47
24+
}
25+
) {
26+
result {
27+
id
28+
name
29+
}
30+
suberrors {
31+
attributes {
32+
code
33+
msg
34+
suberrors {
35+
age {
36+
code
37+
msg
38+
}
39+
name {
40+
code
41+
msg
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
```
49+
50+
### Try mutation with invalid input
51+
```
52+
mutation {
53+
updateAuthor(
54+
authorId: 1
55+
attributes: {
56+
name: "Edward John Moreton Drax Plunkett, 18th Baron of Dunsany",
57+
age: -3
58+
}
59+
) {
60+
result {
61+
id
62+
name
63+
}
64+
suberrors {
65+
attributes {
66+
code
67+
msg
68+
suberrors {
69+
age {
70+
code
71+
msg
72+
}
73+
name {
74+
code
75+
msg
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}
8282
```

0 commit comments

Comments
 (0)