How can I provide a validation error if some of the object properties are invalid?

29 Views Asked by At

I wrote the following validation configuration:

    const presenceTrue = {
      presence: { allowEmpty: false },
    };

    const constraints = {
      "opportunity.name": presenceTrue,
      "opportunity.id": presenceTrue,
    };

I could not find a way to create a custom validator for the opportunity object value but instead I followed validate.js advice on how to validate nested properties within an object property.

My default property value for the opportunity field is { name: null, id: null }, However when I execute validate() and opportunity is still { name: null, id: null }, the return value is the following:

{
  "opportunity.id": "Opportunity id can't be blank",
  "opportunity.name": "Opportunity name can't be blank"
}

I would like the validation result to be either:

{
  "opportunity": "Opportunity can't be blank" (because either id or name properties are blank)
  "opportunity.id": "Opportunity id can't be blank",
  "opportunity.name": "Opportunity name can't be blank"
}

or:

{
  "opportunity": "Opportunity can't be blank"
}
0

There are 0 best solutions below