valibot: Custom error message of missing object entries changed in v1.0.0-beta.13
The error message returned for missing object entries has changed in v1.0.0-beta.13. Previously, the custom error message of the nested field was used. Now it uses the custom error message of the objectschema. With this issue we want to investigate if we should use the custom message of the nested field instead. Here is a playground and here is a code example:
import * as v from 'valibot';
const Schema = v.object({
field: v.string('My custom error'), // This error message if "ignored"
});
const result = v.safeParse(Schema, {});
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 30 (18 by maintainers)
I am still investigating this issue. Please provide a đ or đ if the following workaround is an option for you. I only ask because I am currently investigating several changes to the current behavior, but they all have other drawbacks.
A lot of cases where you might want to set a specific custom message for a missing object property is when working with forms. The reason for this is that you may not have control over how untouched but required form fields are handled. Some form libraries donât initialize them, resulting in a missing object property when representing the form state as an object.
A workaround could be to set the form field as optional in your Valibot schema, but provide an initial value, for example an empty string, as the default value to be used as input to the schema whenever the actual object property is missing or undefined. Now you can use a validation action like
nonEmptyto return the same custom error message regardless of whether the property is missing, undefined, or an empty string. Feel free to test the following code example in this playground.This is a horrible change in behavior. Before you could easily customize the error message for missing keys by passing a message to the field that is missing. Wrapping it in
optionaleven though it is semantically required is extremely unintuitive.Before:
After:
i prefer the old behavior, i think most agree with me
For now, I think it is best to keep the current behavior as it is objectively more correct. If you run into this problem when building forms, I recommend specifying the initial form field values as defaults, so that your form library will always provide a value when running schema validation. Optionally, you can use one of the few workarounds described above.
Hi, I use valibot with a vue js, a key can be missing , but required , that why optional directive isânt appropriate , a custom message is more suitable
Yes it is. Your problem is that your form library sends an empty string into the schema. The fix is to mark an empty string as a valid input with
v.literal('').You can also abstract it with a small custom function:
Here is a playground link to try it out.
but this feels less cleaner, âhackyâ
General note: If this issue is a problem for you when creating forms with a form library, in most cases you can simply add default values to your form to fix it.
Here is a Valibot schema with a similar behavior. You can try it out in this playground.
Also keep in mind that Valibot allows you to read the actual input when generating an error message.
Thanks @filipw01! Here is a slightly modified version where you can pass the schema without a function. You can try it out in our playground.
I think the new and current implementation should be kept. We should figure out how to let users customize the error message for each missing key in the object. In the last code block of your last message, the users wonât be able to customize the âmissing keyâ messages, as any custom message inside an entryâs schema would indicate âthe input has the key and validation failedâ instead of âthe input did not contain the keyâ. Maybe there are cases where the author of the schema wants to provide a different message for the âmissing keyâ and âvalidation failedâ cases easily. What if we add a way to customize each âmissing keyâ error message like shown in the example below?
What an insane change, can it be reverted or placed under a flag, next to
abortEarlyand rest?Using such hacks and wrappers is just madness. Additionally,
objectmessage is shown if field is missing đ¤Żresult:
Itâs not ideal, but for anybody looking for some helper function
example usage
I would recommend to take a look at
/library/CHANGELOG.mdor the release notes. But the change around objects and optional properties is probably the only bigger user facing breaking change. Feel free to join our Discord. We are happy to support you there.No, my comment was solely about the readability of the code rather than the implementation. I donât know if itâs possible to resolve this issue while still maintaining readability but thought Iâd mention it.
If Iâm a developer who is new to valibot then
v.optional(v.string(), ''), // <- Empty string as default valuemakes it look like the property is optional rather than the actual effect of setting the default value to an empty string. It works but itâs not very readable.EDIT: Having read https://valibot.dev/guides/mental-model/ I see this is likely not possible. ~Perhaps a more readable solution would be to add another validation that would check for undefined, e.g.
v.required("Email is required")orv.notUndefined("Email is required")if input is undefined then display error âx is requiredâ.~Thanks for the great library by the way, itâs awesome đ
This works nicely but I find the use of v.optional to not be very readable/clear. It indicates to the reader that this property is now optional. I realize it may not fit correctly with the rest of the API design but a better option for readability would be
v.required("Email is required"), orv.nonUndefined("Email is required").I will look again at ways to get the best of both worlds. I have an idea in mind. I will get back to all of you here as soon as possible.