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

How to extend or replace a rule? #81

Closed
chepe263 opened this issue Oct 4, 2023 · 1 comment
Closed

How to extend or replace a rule? #81

chepe263 opened this issue Oct 4, 2023 · 1 comment

Comments

@chepe263
Copy link

chepe263 commented Oct 4, 2023

In spanish, the string for mixed.notType is a little odd

[field] debe ser un "number" Tipo, Pero el valor final fue: NaN (Eche el valor "".)

To replace just one rule, I imported the es locale, de-estructured it, modified it and exported it again

// custom-es-locale.js
import * as yupLocales from 'yup-locales';
const {array, boolean, date, mixed, number, object, string} = yupLocales.es

const new_mixed = Object.assign({}, mixed, {
  notType: ({path}) => {
    return `${path} no es válido`
  }
})

export default {
  array, boolean, date, mixed: new_mixed, number, object, string
}

Then proceed as the example

// your-main.js
import { custom_es } from './custom-es-locale.js';
import { setLocale } from 'yup';
setLocale( custom_es );

Is there a better way?

@LoicMahieu
Copy link
Owner

I don't see any other way right now...

Maybe shortening custom-es-locale.js:

// custom-es-locale.js
import { es } from 'yup-locales';
es.mixed.notType = ({path}) => `${path} no es válido`
export default es

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

No branches or pull requests

2 participants