Skip to content

Support for required and partial #14

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 7 commits into from
Aug 18, 2024
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ If the value is valid, return `0`, otherwise `1`.
])
```

### The `required` property
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:

```php
//...
'updateThing' => new ValidatedFieldDefinition([
'type' => Types::thing(),
'args' => [
'foo' => [
'required' => true, // if not provided, then an error of the form [1, 'foo is required'] will be returned.
'validate' => function(string $foo) {
if(Foo::find($foo)) {
return 0;
}
return 1;
}
],
'bar' => [
'required' => [1, 'Oh, where is the bar?!'],
'validate' => function(string $bar) {
if(Bar::find($bar)) {
return 0;
}
return 1;
}
]
]
])
```

If you want to return an error message, return an array with the message in the second bucket:
```php
//...
Expand Down
74 changes: 37 additions & 37 deletions examples/01-basic-scalar-validation/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# Simple scalar validation
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.
### Run locally
```
php -S localhost:8000 ./index.php
```
### Install ChromeiQL plug-in for Chrome
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
### Try mutation with valid input
```
mutation {
deleteAuthor(id: 1) {
valid
result
}
}
```
### Try mutation with invalid input
```
mutation {
deleteAuthor(id: 3) {
valid
result
suberrors {
id {
code
msg
}
}
}
}
# Simple scalar validation
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.

### Run locally
```
php -S localhost:8000 ./index.php
```

### Install ChromeiQL plug-in for Chrome
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types

### Try mutation with valid input
```
mutation {
deleteAuthor(id: 1) {
valid
result
}
}
```

### Try mutation with invalid input
```
mutation {
deleteAuthor(id: 3) {
valid
result
suberrors {
id {
code
msg
}
}
}
}
```
86 changes: 43 additions & 43 deletions examples/02-custom-error-codes/README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
# Simple scalar validation
If you supply an `errorCodes` property on your `fields` or `args` definitions, then a custom, unique error code type will be created.
### Run locally
```
php -S localhost:8000 ./index.php
```
### Install ChromeiQL plug-in for Chrome
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
### Try mutation with valid input
```
mutation {
deleteAuthor(id: 1) {
valid
result
suberrors {
id {
code
msg
}
}
}
}
```
### Try mutation with invalid input
```
mutation {
deleteAuthor(id: 3) {
valid
result
suberrors {
id {
code
msg
}
}
}
}
# Simple scalar validation
If you supply an `errorCodes` property on your `fields` or `args` definitions, then a custom, unique error code type will be created.

### Run locally
```
php -S localhost:8000 ./index.php
```

### Install ChromeiQL plug-in for Chrome
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types

### Try mutation with valid input
```
mutation {
deleteAuthor(id: 1) {
valid
result
suberrors {
id {
code
msg
}
}
}
}
```

### Try mutation with invalid input
```
mutation {
deleteAuthor(id: 3) {
valid
result
suberrors {
id {
code
msg
}
}
}
}
```
162 changes: 81 additions & 81 deletions examples/03-input-object-validation/README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
# Validation of InputObjects (or Objects)
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.
### Run locally
```
php -S localhost:8000 ./index.php
```
### Install ChromeiQL plug-in for Chrome
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types
### Try mutation with valid input
```
mutation {
updateAuthor(
authorId: 1
attributes: {
name: "Stephen King",
age: 47
}
) {
result {
id
name
}
suberrors {
attributes {
code
msg
suberrors {
age {
code
msg
}
name {
code
msg
}
}
}
}
}
}
```
### Try mutation with invalid input
```
mutation {
updateAuthor(
authorId: 1
attributes: {
name: "Edward John Moreton Drax Plunkett, 18th Baron of Dunsany",
age: -3
}
) {
result {
id
name
}
suberrors {
attributes {
code
msg
suberrors {
age {
code
msg
}
name {
code
msg
}
}
}
}
}
}
# Validation of InputObjects (or Objects)

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.


### Run locally
```
php -S localhost:8000 ./index.php
```

### Install ChromeiQL plug-in for Chrome
1. Install from [here](https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=en)
2. Enter "http://localhost:8000" in the text field at the top and press the "Set Endpoint" button
3. Be sure to inspect the "Docs" flyout to get familiar with the dynamically-generated types

### Try mutation with valid input
```
mutation {
updateAuthor(
authorId: 1
attributes: {
name: "Stephen King",
age: 47
}
) {
result {
id
name
}
suberrors {
attributes {
code
msg
suberrors {
age {
code
msg
}
name {
code
msg
}
}
}
}
}
}
```

### Try mutation with invalid input
```
mutation {
updateAuthor(
authorId: 1
attributes: {
name: "Edward John Moreton Drax Plunkett, 18th Baron of Dunsany",
age: -3
}
) {
result {
id
name
}
suberrors {
attributes {
code
msg
suberrors {
age {
code
msg
}
name {
code
msg
}
}
}
}
}
}
```
Loading
Loading