#Validate Function Issues

3 messages · Page 1 of 1 (latest)

latent veldt
#

I've noticed multiple times that validate functions REQUIRE optional chaining when accessing the value parameter. I believe this is due to the fact that it will run before all of the fields are actually created in the document, which results in an error. The situation I'm running into specifically is that I have a validate function on a group field in a block, and I can't add a new block of this type because I get a TypeError: Cannot read properties of undefined when trying to access those fields in the validate function unconditionally. If I use optional chaining instead it works fine.

The interesting thing is that this worked fine in local development and then broke for me in production. I believe I've had this issue with condition functions as well as validate functions, but I can't be sure.

I'd at least like there to be some documentation that recommends the best practice of using?.optional?.chaining for all properties accessed from the value property in validate functions. Ideally, the validate function would run at the proper time and/or not run if the value parameter is undefined, or automatically fail in this case, or some other solution that mitigates this issue.

I've also noticed again that when I return a string from this validation function, it doesn't get properly passed into the error toast or the field validation tooltip, and it just ends up giving me a generic "The following field is invalid:" error if it fails validation.

I'm on Payload v1.6.32

latent veldt
#

I feel like, if anything, the value property should be typed as unknown. For a TypeScript central CMS, I feel like either there should be a way to properly type that by default, or if we really don't know each validate function should force the user to check the typings

abstract comet
#

Hey @latent veldt is this type issue still around in 1.11.1?