How to access a json child in php respect validation?

192 Views Asked by At

I'm trying to validate the following json file but I can't find the way to access the "Address" child, how should I do it? Everything goes fine until it tries to access the "address" field.

Json:

{
"first_name": "Test",
"last_name": "Test",
"email": "[email protected]",
"mobile_phone": 123456789,
"address": {
    "country": "Test",
    "postal_code": "Test",
    "city": "Test",
    "street": "Test",
    "number": "Test",
    "floor": "Test",
    "door": "Test"
},
"password": "Test",
"birth_date": "2005-12-30"
}

Code:

$data = $this->validator->validate($request, [
        "first_name" => v::stringVal()->notEmpty(),
        "last_name" => v::stringVal()->notEmpty(),
        "email" => v::notEmpty()->email(),
        "mobile_phone" => v::intVal()->notEmpty(),
        "address" =>[
            "country" => v::stringVal()->notEmpty(),
            "postal_code" => v::stringVal()->notEmpty(),
            "city" => v::stringVal()->notEmpty(),
            "street" => v::stringVal()->notEmpty(),
            "number" => v::stringVal()->notEmpty(),
            "floor" => v::stringVal()->notEmpty(),
            "door" => v::stringVal()->notEmpty(),
        ],
        "password" => v::base64()->notEmpty(),
        "birth_date" => v::date()->notEmpty()
    ]);
2

There are 2 best solutions below

0
On BEST ANSWER

That is the way to access the child of the json:

$this->validator->validate($request, [
        "first_name" => v::stringVal()->notEmpty(),
        "last_name" => v::stringVal()->notEmpty(),
        "email" => v::notEmpty()->email(),
        "mobile_phone" => v::intVal()->notEmpty(),
        "address" => v::notEmpty()
            ->key("country", v::stringVal()->notEmpty())->key("postal_code", v::stringVal())->key("city", v::stringVal()->notEmpty())->key("street", v::stringVal()->notEmpty())->key("number", v::stringVal())->key("floor", v::stringVal())->key("door", v::stringVal()),
        "password" => v::base64()->notEmpty(),
        "birth_date" => v::date('d/m/Y')->notEmpty()
    ]);
0
On

Looks like you are using Respect\Validation\Validator. We can make use of chained validation and the Key function to validate sub-arrays as shown in the docs.

$data = [
    'parentKey' => [
        'field1' => 'value1',
        'field2' => 'value2'
        'field3' => true,
    ]
];

Using the next combination of rules, we can validate child keys.

v::key(
    'parentKey',
    v::key('field1', v::stringType())
        ->key('field2', v::stringType())
        ->key('field3', v::boolType())
    )
    ->assert($data); // You can also use check() or validate()

Applied to your data, the validation could look like this:

use Respect\Validation\Validator as v;

$json = json_decode('{
    "first_name": "Test",
    "last_name": "Test",
    "email": "[email protected]",
    "mobile_phone": 123456789,
    "address": {
        "country": "Test",
        "postal_code": "Test",
        "city": "Test",
        "street": "Test",
        "number": "Test",
        "floor": "Test",
        "door": "Test"
    },
    "password": "Test",
    "birth_date": "2005-12-30"
    }', true);
    
v::create()
    ->key('first_name', v::stringType()->length(1, 32))
    ->key('last_name', v::stringVal()->notEmpty())
    //->..
    ->key('address',
        v::key('country', v::stringType()->length(1, 32))
        ->key('postal_code', v::stringType()->length(1, 32))
        ->key('city', v::stringType()->length(1, 32))
        //->..
    )
    ->key('password', v::stringVal()->notEmpty())
    ->key('birth_date', v::stringVal()->notEmpty())
    ->assert($json); // You can also use check() or validate()