Skip to content

Commit

Permalink
Change addFieldValidationError to addValidationError (#5514)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Apr 22, 2021
1 parent 18ca7b3 commit 8577eb3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .changeset/healthy-pumas-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-next/fields': patch
'@keystone-next/keystone': patch
---

The field hooks API has deprecated the `addFieldValidationError` argument. It has been replaced with the argument `addValidationError`, and will be removed in a future release.
1 change: 0 additions & 1 deletion docs-next/pages/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ Options:
- `storagePath`: The path local images are uploaded to.
- `baseUrl`: The base of the URL local images will be served from, outside of keystone.


## experimental

The following flags allow you to enable features which are still in preview.
Expand Down
1 change: 1 addition & 0 deletions docs-next/pages/apis/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export default config({
/* ... */
});
```

## File types

File types allow you to upload different types of files to your Keystone system.
Expand Down
46 changes: 22 additions & 24 deletions docs-next/pages/apis/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,19 @@ The `validateInput` function is used to validate the [`resolvedData`](#resolved-

It is invoked after the `resolveInput` hooks have been run.

If the `resolvedData` is invalid then the function should report validation errors with `addValidationError(msg)` for list hooks, or `addFieldValidationError(msg)` for field hooks.
If the `resolvedData` is invalid then the function should report validation errors with `addValidationError(msg)`.
These error messages will be returned as a `ValidationFailureError` from the GraphQL API, and the operation will not be completed.

| Argument | Description |
| :----------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `listKey` | The key of the list being operated on. |
| `fieldPath` | The path of the field being operated on (field hooks only). |
| `operation` | The operation being performed (`'create'` or `'update'`). |
| `originalInput` | The value of `data` passed into the mutation. |
| `existingItem` | The current value of the item being updated (`undefined` for `create` operations). This object is an unresolved list item. See the [list item API](./list-items) for more details on unresolved list items. |
| `resolvedData` | A [`resolved data`](#resolved-data-stages) object. The resolved data value after all data resolver stages have been completed. |
| `context` | The [`KeystoneContext`](./context) object of the originating GraphQL operation. |
| `addFieldValidationError(msg)` | Used to set a field validation error (field hooks only). |
| `addValidationError(msg)` | Used to set a validation error (list hooks only). |
| Argument | Description |
| :------------------------ | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `listKey` | The key of the list being operated on. |
| `fieldPath` | The path of the field being operated on (field hooks only). |
| `operation` | The operation being performed (`'create'` or `'update'`). |
| `originalInput` | The value of `data` passed into the mutation. |
| `existingItem` | The current value of the item being updated (`undefined` for `create` operations). This object is an unresolved list item. See the [list item API](./list-items) for more details on unresolved list items. |
| `resolvedData` | A [`resolved data`](#resolved-data-stages) object. The resolved data value after all data resolver stages have been completed. |
| `context` | The [`KeystoneContext`](./context) object of the originating GraphQL operation. |
| `addValidationError(msg)` | Used to set a validation error. |

```typescript
import { config, createSchema, list } from '@keystone-next/keystone/schema';
Expand Down Expand Up @@ -170,7 +169,7 @@ export default config({
existingItem,
resolvedData,
context,
addFieldValidationError,
addValidationError,
}) => { /* ... */ },
},
}),
Expand Down Expand Up @@ -295,18 +294,17 @@ The `validateDelete` function is used to validate that deleting the selected ite

It is invoked after access control has been applied.

If the delete operation is invalid then the function should report validation errors with `addValidationError(msg)` for list hooks, or `addFieldValidationError(msg)` for field hooks.
If the delete operation is invalid then the function should report validation errors with `addValidationError(msg)`.
These error messages will be returned as a `ValidationFailureError` from the GraphQL API.

| Argument | Description |
| :----------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `listKey` | The key of the list being operated on. |
| `fieldPath` | The path of the field being operated on (field hooks only). |
| `operation` | The operation being performed (`'delete'`). |
| `existingItem` | The value of the item to be deleted. This object is an unresolved list item. See the [list item API](./list-items) for more details on unresolved list items. |
| `context` | The [`KeystoneContext`](./context) object of the originating GraphQL operation. |
| `addValidationError(msg)` | Used to set a validation error (list hooks only). |
| `addFieldValidationError(msg)` | Used to set a field validation error (field hooks only). |
| Argument | Description |
| :------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `listKey` | The key of the list being operated on. |
| `fieldPath` | The path of the field being operated on (field hooks only). |
| `operation` | The operation being performed (`'delete'`). |
| `existingItem` | The value of the item to be deleted. This object is an unresolved list item. See the [list item API](./list-items) for more details on unresolved list items. |
| `context` | The [`KeystoneContext`](./context) object of the originating GraphQL operation. |
| `addValidationError(msg)` | Used to set a validation error. |

```typescript
import { config, createSchema, list } from '@keystone-next/keystone/schema';
Expand All @@ -333,7 +331,7 @@ export default config({
operation,
existingItem,
context,
addFieldValidationError,
addValidationError,
}) => { /* ... */ },
},
}),
Expand Down
2 changes: 1 addition & 1 deletion packages-next/fields/src/Implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Field<P extends string> {
listKey: string;
fieldPath: P;
operation: 'create' | 'update';
addFieldValidationError: (msg: string) => void;
addValidationError: (msg: string) => void;
}) {}

async beforeChange() {}
Expand Down
15 changes: 12 additions & 3 deletions packages-next/keystone/src/lib/core/ListTypes/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,18 @@ export class HookManager {
const { originalInput } = args;
const fieldValidationErrors: ValidationError[] = [];
// FIXME: Can we do this in a way where we simply return validation errors instead?
const addFieldValidationError = (msg: string, _data = {}, internalData = {}) =>
fieldValidationErrors.push({ msg, data: _data, internalData });
const fieldArgs = { addFieldValidationError, ...args };
const fieldArgs = {
...args,
addValidationError: (msg: string, _data = {}, internalData = {}) =>
fieldValidationErrors.push({ msg, data: _data, internalData }),
// deprecated: Will be removed in a future release.
addFieldValidationError: (msg: string, _data = {}, internalData = {}) => {
console.log(
'addFieldValidationError is deprecated. Please use addValidationError instead.'
);
return fieldValidationErrors.push({ msg, data: _data, internalData });
},
};
await mapToFields(fields, (field: Implementation<any>) =>
field[hookName]({ fieldPath: field.path, ...fieldArgs })
);
Expand Down

1 comment on commit 8577eb3

@vercel
Copy link

@vercel vercel bot commented on 8577eb3 Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.