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

Validate a field depending on other (NOT equality) #771

Closed
damianrr opened this issue Feb 4, 2020 · 2 comments
Closed

Validate a field depending on other (NOT equality) #771

damianrr opened this issue Feb 4, 2020 · 2 comments

Comments

@damianrr
Copy link

damianrr commented Feb 4, 2020

Hello guys,

I've a schema with a type_of_account field and a account_number field. Depending on the type_of_account I've a regex to apply to the account_number field. Reading through this: jaredpalmer/formik#90 and this: #97 I was able to solve it like this:

const validations = {
  account_1: {
    regex: /\w{8}/,
    example: "00000000"
  },
  account_2: {
    regex: /\w{3}/,
    example: "000"
  }
};

const schema = Yup.object({
  type_of_account: Yup.string(),
  account_number: Yup.string().validateAccountNumberFormat(
    Yup.ref("type_of_account")
  )
});

function validateAccountNumberFormat(ref, msg) {
  return this.test({
    name: "validateAccountNumberFormat",
    exclusive: false,
    message: msg || "Invalid format",
    test: value => value.match(validations[this.resolve(ref)].regex)
  });
}
Yup.addMethod(
  Yup.string,
  "validateAccountNumberFormat",
  validateAccountNumberFormat
)

This works beautifully with the sole problem that, in case of an input error (for example, selecting account_1 and entering only 3 characters) the user will get the generic "Invalid format" error, I'd very much like to display something like "Invalid format, use something like this: 00000000". For that I have the property example in the validations object and tried add the ref to the parameters for displaying on the field, something like:

...
    return this.test({                                                                                                                  
      name: "validateAccountNumberFormat",                                                                                              
      exclusive: false,                                                                                                                 
      message: msg || "Invalid format, use something like this ${example}",
      params: {
        example: this.resolve(ref),
      },                                                                                                 
      test: value => value.match(validations[this.resolve(ref)].regex))                                                            
    }); 
}
...

Yet, this doesn't work, seems like this promise (ref) isn't available for resolve at this point. So ... how can I do something like this?

Thanks!

@jquense
Copy link
Owner

jquense commented Feb 4, 2020

use this.createError()inside your test function, you can pass it params, and it will override/extend the existing ones, but at that point resolve will work.

@damianrr
Copy link
Author

damianrr commented Feb 4, 2020

Thanks for the quick response @jquense; it works like a charm!

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