Yup Validation conditionally add custom made method

Viewed 447

I am new to yup validations and reactjs. I have defined a method using yup.addMethod()

    yup.addMethod(yup.array, "unique", function (message, path) {
    
    
      return this.test("unique", message, function (list) {
        
       /// impelementation here
      });
    });

I want to use this to validate an array conditionally using this unique function

let singleSchema = yup.object().shape({
  format: yup.string(),
  fields: yup
    .array()
    .of(
      yup.object().nullable().shape(
        {
   
          mpaKey: yup.string(),
          mpaFieldName: yup.string(),
          mapFieldId: yup.string()
        })
    ).unique("Id is not unique")
  
});

I want to add this unique function only if the format is equal to "AN" otherwise I don't want to use the unique function is there a way to do it?

0 Answers
Related