DavidePastore / Slim-Validation

A validation library for the Slim Framework. It internally uses Respect/Validation.
170 stars 30 forks source link

How can I validate that at least one of two or more optional parameters is present #47

Open gnl1 opened 4 years ago

gnl1 commented 4 years ago

Hi,

I would like to know if there is a way to validate a json payload in such a way that it checks if there exists only one of two or more optional keys as well as validate its contents? i.e. in the following payloads the first two should fail since the first one does not include "telephone1" key, the second one should fail since it includes both "telephone1" and "telephone2" keys, the third should fail because has less characters than 8 as well as it contains alpha characters and the fourth one should pass since it includes a single key i.e "telephone2" and it consists of only numbers and has a length of 8

Payload 1: (Should fail) { "name": "george" }

Payload 2: (Should fail) { "name": "george", "telephone1": "12345678", "telephone2": "87654321" }

Payload 3: (Should fail cause less than 8 characters and it contains alpha characters) { "name": "george", "telephone2": "ABC123" }

Payload 4: (Should pass) { "name": "george", "telephone2": "87654321" }

Thanks.