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

Update built-in fields to new validate hook syntax #9166

Merged
merged 16 commits into from
Jul 9, 2024

Conversation

acburdine
Copy link
Member

closes #9165

  • this updates the built-in fields using validateInput to use the new unified validation syntax, and handles merging with the old syntax to ensure user-land old syntax still works

Copy link

codesandbox-ci bot commented Jun 5, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ea10e02:

Sandbox Source
@keystone-6/sandbox Configuration

@dcousens
Copy link
Member

dcousens commented Jun 5, 2024

Amazing @acburdine, this is tedious and really appreciated work! 💚
I'll review, merge + release this as soon as I can.

@dcousens dcousens self-requested a review June 12, 2024 23:35
@dcousens dcousens self-assigned this Jun 12, 2024
@gautamsi gautamsi mentioned this pull request Jul 8, 2024
5 tasks
@gautamsi
Copy link
Member

gautamsi commented Jul 8, 2024

I am working on #9204 which removes the deprecated hooks and cleanups code for Fields as well as consuming code in a breaking changes. This PR will not be needed if that approach works for @dcousens

@dcousens
Copy link
Member

dcousens commented Jul 8, 2024

@gautamsi #9204 will be a breaking change, which is good, we want that, but, we should let users gracefully upgrade by releasing this first no?

@gautamsi
Copy link
Member

gautamsi commented Jul 8, 2024

we can do that, i guess the other one would be part of v-next branch and might take longer, I agree with you to get this going.

@dcousens dcousens force-pushed the fix/field-hooks branch 6 times, most recently from 4d56c41 to 2d2b8fc Compare July 8, 2024 10:57
@keystonejs keystonejs deleted a comment from codesandbox-ci bot Jul 8, 2024
@dcousens dcousens force-pushed the fix/field-hooks branch 4 times, most recently from d6cbbcc to 2cf6c8c Compare July 9, 2024 00:34
@@ -55,7 +55,7 @@ export const crudTests = (keystoneTestWrapper: any) => {
expect(result.errors).toHaveLength(1)
expect(result.errors![0].message).toMatchInlineSnapshot(`
"You provided invalid data for this operation.
- Test.price: Price must be greater than or equal to -300"
- Test.price: value must be greater than or equal to -300"
Copy link
Member

@dcousens dcousens Jul 9, 2024

Choose a reason for hiding this comment

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

Errors are already prefixed with {listKey}.{fieldKey}, this humanized part is redundant and is difficult to understand for names with a few words

max !== undefined &&
min > max
) {
throw new Error(`${meta.listKey}.${meta.fieldKey} specifies a validation.max that is less than the validation.min, and therefore has no valid options`)
}
Copy link
Member

@dcousens dcousens Jul 9, 2024

Choose a reason for hiding this comment

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

I used this opportunity to unify each of the numerical fields to share nearly the same code for min and max validation.

delete: merge(builtinValidate?.delete, hooksValidate?.delete)
} : undefined,
} satisfies FieldHooks<ListTypeInfo>
}
Copy link
Member

@dcousens dcousens Jul 9, 2024

Choose a reason for hiding this comment

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

@acburdine I tidied this up a little to re-use a new merge function, but fundamentally it's the same

@@ -4,6 +4,7 @@ export const name = 'Text'
export const typeFunction = text
export const exampleValue = () => 'foo'
export const exampleValue2 = () => 'bar'
export const nonNullableDefault = true
Copy link
Member

@dcousens dcousens Jul 9, 2024

Choose a reason for hiding this comment

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

By adding this for multiselect, and text supporting null in #9057, text is now less special in our tests

@@ -53,6 +52,11 @@ for (const modulePath of testModules) {
fields: {
name: text(),
testField: mod.typeFunction({
...(mod.nonNullableDefault ? {
db: {
isNullable: true
Copy link
Member

@dcousens dcousens Jul 9, 2024

Choose a reason for hiding this comment

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

These tests had been previously written assuming that db.isNullable is true by default, with an inline exception for text

@dcousens
Copy link
Member

dcousens commented Jul 9, 2024

Thanks for starting this work @acburdine, this has been a heavy lift that has been required for a while - I don't think I could have jumped in to update the fields to be aligned with validation.isRequired and db.isNullable without your help!

Thank you 💛

@dcousens dcousens merged commit ad45b05 into keystonejs:main Jul 9, 2024
42 of 43 checks passed
@acburdine acburdine deleted the fix/field-hooks branch July 9, 2024 12:20
@dcousens dcousens mentioned this pull request Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use new validate.{create|update|delete} field-level hooks with most built-in fields
3 participants