DavidePastore / Slim-Validation

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

Support Nested Object Validation #32

Closed kdaker closed 6 years ago

kdaker commented 6 years ago

Assume we have the following list of rules to be validated as a application/json post:

 $rules = [
    'message' => [
        'notification' => [
            'title' => v::stringType()->length(1, null)->setName("notificationTitle"),
            'body' => v::stringType()->length(1, null)->setName("notificationBody"),
            'actionName' => v::optional(v::stringType()->length(1, null))->setName("notificationAction")
        ]
    ]
];

If I were to post something like this:

{ 
    'message' :   {
        'notification' : 1
     }
}

an error exception is thrown instead of validating that notification should be an object.

Respect seems to have a way to solve with using the key method but we don't have access to that using the middleware. https://github.com/Respect/Validation/issues/317

exception:

Type: ErrorException
Message: array_key_exists() expects parameter 2 to be array, integer given
File: /path/vendor/davidepastore/slim-validation/src/Validation.php
Line: 163
DavidePastore commented 6 years ago

Hi @kdaker, thanks for opening this issue. You can try and use the 0.5.1 release that includes this bug fix.

kdaker commented 6 years ago

will do @DavidePastore , thanks for the fix.