validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

problems when using `required` rule in nested field that parent is not required

Open mshlz opened this issue 4 years ago • 2 comments

Situation

I'm coding a wrapper to validate some group of fields (like address) that appears in more than one place in the system (to avoid repeated rules) but I am having problem with fields that are required but only when the parent is present.

Example:

let data = {
  item: {
    value: 123,
    name: "sample"
  }
}

let rule = {
  item: {
    value: "required|integer",
    name: "required|string"
  }
}

The problem is the item is optional, but if try to pass an empty object as data, the validator throw this errors:

"The item.value field is required."
"The item.name field is required."

The way I've found to solve this, is using the required_with rule passing the path of the parent item, but it is impractical as the depth of the field increase.

// three level depth
let data = {
  foo: {
    bar: {
      item: {
        value: 1,
        name: 'sample'
      }
    }
  }
}

let rule = {
  foo: {
    bar: {
      item: {
        value: "required_with:foo.bar.item|integer",
        name: "required_with:foo.bar.item|string"
      }
    }
  }
}

I think the validator should check if the parent is required before execute the child's required rule, so this will simplify the way to validate children properties.

There are any possibility to this be implemented?

mshlz avatar Mar 31 '21 16:03 mshlz

The problem is the item is optional, but if try to pass an empty object as data, the validator throw this errors:

"The item.value field is required."
"The item.name field is required."

If you are not supply item, then the validation is correctly failing? Under what circumstances are you expecting this to pass with the provided rule?

mikeerickson avatar Apr 24 '21 17:04 mikeerickson

If you are not supply item, then the validation is correctly failing? Under what circumstances are you expecting this to pass with the provided rule?

Yes, if item is not present in the input the validation fail, that's correct. But I'm think required rule (or a new rule) could be improved to internally check if it's inside in a field that is optional, then only fails the validation if its parent is present.

For example, an endpoint to add product to database expect to receive this schema:

{
    product: {
        name: 'required|string',
        value: 'required|numeric',
        reference?: { // this is optional field
            id: 'required|string',
            type: 'string'
        }
    }
}

We have some fields that are required: name and value; but reference is an optional field, and in this way the user cannot insert because the validation fails in "product.reference.id" required rule. So, this is a circumstance that I think the required (or a new rule) should pass. But if reference field be present, then the validation should fails.


I dont forgotten the required_with rule, but I've commented above, as depth increase, more complicated is to reference the field (and it's static, if you need to change some field name, you will need to replace everywhere)

mshlz avatar Apr 24 '21 22:04 mshlz