Skip to content
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

Remove unused addFieldValidationError arg from list validation hooks #4652

Merged
merged 2 commits into from
Jan 11, 2021
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
5 changes: 5 additions & 0 deletions .changeset/breezy-feet-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': patch
---

Removed `addFieldValidationError` from the args passed to list validation hooks, as it was unused and could lead to confusion. Updated docs to clarify the validation hook arguments. Thanks @pahaz for the fix.
26 changes: 15 additions & 11 deletions docs/api/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const resolveInput = ({
resolvedData,
context,
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Input resolution logic. Object returned is used in place of `resolvedData`.
return resolvedData;
Expand All @@ -198,7 +198,8 @@ Return values are ignored.
| `originalInput` | `Object` | The data received by the GraphQL mutation |
| `resolvedData` | `Object` | The data received by the GraphQL mutation plus defaults values |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `addFieldValidationError` | `Function` | Used to set a field validation error; accepts a `String` |
| `addFieldValidationError` | `Function` | Used to set a field validation error (applicable to field hooks only); accepts a `String` |
| `addValidationError` | `Function` | Used to set a validation error (applicable to list hooks only); accepts a `String` |
| `listKey` | `String` | The key for the list being operated on |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

Expand All @@ -213,9 +214,10 @@ const validateInput = ({
originalInput,
resolvedData,
context,
addFieldValidationError,
addFieldValidationError, // Field hooks only
addValidationError, // List hooks only
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Throw error objects or register validation errors with addFieldValidationError(<String>)
// Return values ignored
Expand Down Expand Up @@ -256,7 +258,7 @@ const beforeChange = ({
resolvedData,
context,
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Perform side effects
// Return values ignored
Expand Down Expand Up @@ -301,7 +303,7 @@ const afterChange = ({
updatedItem,
context,
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Perform side effects
// Return values ignored
Expand All @@ -324,7 +326,8 @@ Should throw or register errors with `addFieldValidationError(<String>)` if the
| `operation` | `String` | The operation being performed (`delete` in this case) |
| `existingItem` | `Object` | The current stored item |
| `context` | `Apollo Context` | The [Apollo `context` object](https://www.apollographql.com/docs/apollo-server/data/data/#context-argument) for this request |
| `addFieldValidationError` | `Function` | Used to set a field validation error; accepts a `String` |
| `addFieldValidationError` | `Function` | Used to set a field validation error (applicable to field hooks only); accepts a `String` |
| `addValidationError` | `Function` | Used to set a validation error (applicable to list hooks only); accepts a `String` |
| `listKey` | `String` | The key for the list being operated on |
| `fieldPath` | `String` | The path for the field being operated on (applicable to field hooks only) |

Expand All @@ -337,9 +340,10 @@ const validateDelete = ({
operation,
existingItem,
context,
addFieldValidationError,
addFieldValidationError, // Field hooks only
addValidationError, // List hooks only
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Throw error objects or register validation errors with addFieldValidationError(<String>)
// Return values ignored
Expand Down Expand Up @@ -376,7 +380,7 @@ const beforeDelete = ({
existingItem,
context,
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Perform side effects
// Return values ignored
Expand Down Expand Up @@ -415,7 +419,7 @@ const afterDelete = ({
existingItem,
context,
listKey,
fieldPath, // exists only for field hooks
fieldPath, // Field hooks only
}) => {
// Perform side effects
// Return values ignored
Expand Down
7 changes: 4 additions & 3 deletions packages/keystone/lib/ListTypes/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ class HookManager {
const { originalInput } = args;
const fieldValidationErrors = [];
// FIXME: Can we do this in a way where we simply return validation errors instead?
args.addFieldValidationError = (msg, _data = {}, internalData = {}) =>
const addFieldValidationError = (msg, _data = {}, internalData = {}) =>
fieldValidationErrors.push({ msg, data: _data, internalData });
await mapToFields(fields, field => field[hookName](args));
const fieldArgs = { addFieldValidationError, ...args };
await mapToFields(fields, field => field[hookName]({ fieldPath: field.path, ...fieldArgs }));
await mapToFields(
fields.filter(field => field.hooks[hookName]),
field => field.hooks[hookName]({ fieldPath: field.path, ...args })
field => field.hooks[hookName]({ fieldPath: field.path, ...fieldArgs })
);
if (fieldValidationErrors.length) {
this._throwValidationFailure({ errors: fieldValidationErrors, operation, originalInput });
Expand Down