I'm using Slim PHP Framework and I need to validate POST request payload and I'm using this PHP package Respect-Validation. The idea that I have array ob objects and I need to validate the data inside each object. I used this code for validation
$validator = Validator::attribute("name", Validator::stringType()->notEmpty())
->attribute("sites", Validator::arrayVal()->notEmpty()
->each(
Validator::keySet(
Validator::key("id", Validator::intVal()->notEmpty()),
Validator::key("value", Validator::intVal()->notEmpty()),
)
))
->attribute('modules', Validator::arrayType()->notEmpty()
->each(
Validator::keySet(
Validator::key('id', Validator::intType()->notEmpty()),
Validator::key('title', Validator::stringType()->notEmpty())
)
))
return $validator->assert($body);
but when I sent the data via postman I received this error. although required data sent in the payload but it still returns error?
{
"statusCode": 400,
"error": {
"type": "BAD_REQUEST",
"description": "validations errors.",
"fields": {
"sites": "Must have keys `{ \"id\", \"value\" }`",
"modules": "Must have keys `{ \"id\", \"title\" }`"
}
}
}