Comments (11)
Oh, I'm sorry, I just found that you can use v.variant('field', [Schema1, Schema2], 'Message')
that's what I want. I'm sorry for the misunderstood.
from valibot.
We support advanced errors too but they work a bit differently. Have a look at this guide: https://valibot.dev/guides/internationalization/
import * as v from 'valibot';
v.setGlobalMessage((issue) => ...);
from valibot.
Please reach out if you can’t achieve your goal. I am happy to further improve the library.
from valibot.
It's not the solution that I was looking for, because I would like to change the message for each case (if I have more), like "You need to choose a country" and in other cases "You need to choose a nationality". I would like to see an option to customize that message for the specific field.
Something like:
const PTFormSchema = v.object({
codConcelho: v.string([v.minLength(1, 'required.county')]),
codPais: v.literal('PT', 'required.country', {errorMap: () => 'My message'}), // It could be other format, like a cusom method ([v.errorMap(() => 'My message')])
codigoPostal2: v.string([v.minLength(1, 'required.postalCode')]),
concelhoEstrangeiro: v.unknown(),
});
from valibot.
I did not quite understand your use case. Why don't you use pass the message as a string, or define it globally a pass its variable. Another option would be to use an i18n library like Paraglide JS. Your code does not look secure to me. If you change a key, you never know if your code is broken. There is no secure mapping between an error map and the specified keys.
from valibot.
Well, I'm using react-hook-forms and valibot to validate, then, I'm using react-i18next to translate the error messages. For each literal
I would like to have an option to configure an error message in case of the value don't match that specific literal
.
For now, what I'm doing is this, but it would be nice to change this string created by valibot:
{
"Invalid type: Expected \"PT\" | string but received \"\"": "O campo é obrigatório",
"Invalid type: Expected true | false but received \"\"": "O campo é obrigatório"
}
from valibot.
If you only support one language I would not do the overhead of using an i18n library and just pass the error message directly to Valibot. If it is necessary to support multiple languages I would have a look at Paraglide JS and pass the error message as described here directly to Valibot.
Valibot is still in active development. I am happy to receive feedback to further improve custom error messages before we reach v1 in the next weeks.
import * as v from 'valibot';
// Option 1 (similar to an error map)
const messages = {
'required.county': 'Your message',
// ...
};
const Schema = v.object({
codConcelho: v.string([v.minLength(1, messages['required.county'])]),
// ...
});
// Option 2 (better for tree shaking)
const REQUIRED_COUNTRY = 'Your message';
const Schema = v.object({
codConcelho: v.string([v.minLength(1, REQUIRED_COUNTRY)]),
// ...
});
from valibot.
I need support for multiple languages and since we are using react-i18next already, we can't change.
I would like to just have an option to configure a message (directly on the literal
) in case of the value is other than the expected.
Imagine literal(false)
and receive ''
, instead of the default Invalid type: Expected false but received \"\"
, valibot could use the message that I define.
It could be something like (just an example, it could be different due to the current implementation):
foo: v.literal(false, 'required.foo', <invalid literal error message>)
from valibot.
literal
should not return the default message when the second argument is specified. In your case it should return 'required.foo'
. But maybe I'm misunderstanding you. I'm sorry, I answer many questions every day and can't keep track of everything.
Feel free to provide me with a minimal reproduction, e.g. on StackBlitz. Then I can try to investigate your problem to understand it better.
from valibot.
No problem, I can understand, I'll post here a MRE. Thank you.
from valibot.
Here it is, if you try to submit without changing anything or if you choose one options other then 'PT' or 'ES'.
from valibot.
Related Issues (20)
- Record with params (key, value, pipe) return error TS2769 HOT 4
- Invalid type error while trying to parse process.env object HOT 7
- Unable to use `lazy()` in `variant()` HOT 1
- New fields in existing schema HOT 6
- Type instantiation is excessively deep and possively Infinite HOT 4
- [i18n] Add Spanish language HOT 1
- Empty array in optional as a default value turns the type into union of the type or never HOT 6
- Build error HOT 4
- Add `items` action HOT 1
- Can't run test code using 0.31.0-rc HOT 6
- Add an advanced `refine` method (similar to Zod's `superRefine`)
- [0.31.0-rc] Enable to perform transform on the first argument of pipe method HOT 2
- EMOJI_REGEX throws Invalid regular expression flags on node v18 HOT 4
- TypeScript: InferInput always infers intersect as 'never' HOT 3
- [v0.31.0-rc.5] `Invalid type: Expected Object but received Object` because of `edge` runtime HOT 3
- abortPipeEarly influnces on abortEarly HOT 2
- Compiled type for actions missing typing for 'requirement' in ErrorMessage HOT 2
- [v0.31.0-rc.5] The internal function `_stringify` has a problem HOT 23
- cannot pipe a pipe HOT 2
- Enum error with React Hook Form HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valibot.