Indicative: required_if

Am I using this correctly?

            "options": "required|array",
            "options.*.id": "string",
            "options.*.name": "required|string",
            "options.*.active": "boolean",
            "options.*.options": "array",
            "options.*.options.*.id": "required_if:options.*.options|string",
            "options.*.options.*.name": "required_if:options.*.options|string",
            "options.*.options.*.active": "required_if:options.*.options|boolean",

I want that the options..options fields to be required only of the options..options exists, but it’s not working… If I pass and object without the active property, for example, it will pass the validator without errors

That’s because options.*.active is missing required
Should work fine if you add it

1 Like

but it already has the required alternative: “required_if”… Do I have to put the required anyway? if I put the options..options..active: “required” it will be always required and I don’t want that, I want it required only when the options.*.options exists

Can you give 2 sets of example input data, one that should fail and one that should pass?

Should pass:

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true
    ]
}

Should pass:

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true,
        "options": [
            {
                 "id": "desyv6ry",
                 "name": "suboption name",
                 "active": true
            }
        ]
    ]
}

Should not pass

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true,
        "options": [
            {
                "id": "dsds",
                "name": "dsadsad",
                // no active properties
            }
        ]
}

Hello!

Sorry for real long delay… Busy times :sweat_smile:

Anyway, here’s the validation ruleset that you most likely want to use:

const rules = {
  "options": "required|object",
  "options.id": "string",
  "options.name": "required|string",
  "options.active": "boolean",
  "options.options": "array",
  "options.options.*.id": "required_if:options.options|string",
  "options.options.*.name": "required_if:options.options|string",
  "options.options.*.active": "required_if:options.options|boolean",
} 

But there is logic error in data that you are sending: It’s not valid JSON

{
    "options": [
        "id": "s5tvertf5",
        "name": "option name",
        "active": true
    ]
}

must be:

{
    "options": {
        "id": "s5tvertf5",
        "name": "option name",
        "active": true
    }
}

(Array can’t have set of key-value pairs, only objects can)

Yeah, you right. that was an error. I forgot the curly brackets in my reply… I’m sorry

Should pass:

{
    "options": [{
        "id": "s5tvertf5",
        "name": "option name",
        "active": true
    }]
}

Should pass:

{
    "options": [{
        "id": "s5tvertf5",
        "name": "option name",
        "active": true,
        "options": [
            {
                 "id": "desyv6ry",
                 "name": "suboption name",
                 "active": true
            }
        ]
    }]
}

Should not pass

{
    "options": [{
        "id": "s5tvertf5",
        "name": "option name",
        "active": true,
        "options": [
            {
                "id": "dsds",
                "name": "dsadsad",
                // no active properties
            }
        }]
}