Giter VIP home page Giter VIP logo

Comments (11)

ruiaraujo012 avatar ruiaraujo012 commented on May 29, 2024 1

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.

fabian-hiller avatar fabian-hiller commented on May 29, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 29, 2024

Please reach out if you can’t achieve your goal. I am happy to further improve the library.

from valibot.

ruiaraujo012 avatar ruiaraujo012 commented on May 29, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 29, 2024

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.

ruiaraujo012 avatar ruiaraujo012 commented on May 29, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 29, 2024

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.

ruiaraujo012 avatar ruiaraujo012 commented on May 29, 2024

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.

fabian-hiller avatar fabian-hiller commented on May 29, 2024

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.

ruiaraujo012 avatar ruiaraujo012 commented on May 29, 2024

No problem, I can understand, I'll post here a MRE. Thank you.

from valibot.

ruiaraujo012 avatar ruiaraujo012 commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.