Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional but to be validated if not empty in a keyNested case #713

Closed
phaberest opened this issue Aug 11, 2016 · 0 comments
Closed

Optional but to be validated if not empty in a keyNested case #713

phaberest opened this issue Aug 11, 2016 · 0 comments

Comments

@phaberest
Copy link

phaberest commented Aug 11, 2016

I'm trying to write a set of classes to validate the json payload of some restful APIs.

So far I'm able to validate against rules for any of the data values, but I'm becoming crazy when trying to validate optional fields that are in fact required to be of a specified type if they are present. In addition if what I'm checking is an optional array, every child attribute has to be required whenever the parent array is included in the payload.

What I have done so far:

Master validator class

namespace App\Validators;

use Respect\Validation\Validator as Respect;

class Validator
{
    public static function prepare($to_be_checked)
    {
        $ruler = explode('.', $to_be_checked);
        $class = 'App\\Validators\\'.ucfirst($ruler[0]).'Checks';
        $funct = $ruler[1].'Rules';
        $rules = (new $class)->{$funct}();

        $validator = new Respect;
        foreach ($rules as $key => $rule) {
            call_user_func_array(
                [$validator, 'keyNested'],
                [$key, $rule[0], empty($rule[1])]
            );
        }

        return $validator;
    }
}

Example of rules sub-class

namespace App\Validators;

use Respect\Validation\Validator as v;

class ShipmentsChecks
{
    public function createRules()
    {
        return [
            'external_order_id' => [v::intType()],
            'courier_alias'     => [v::stringType()],
            'sender'            => [v::optional(v::arrayType()), true],
            'sender.name'       => [v::when(v::notEmpty(), v::stringType(), v::positive())],
            'sender.address'    => [v::when(v::notEmpty(), v::stringType())],
            'receiver'          => [v::arrayType()],
            'receiver.name'     => [v::stringType()],
            'receiver.address'  => [v::stringType()],
        ];
    }

    public function updateRules()
    {
        return $this->createRules();
    }
}

In the above example I want sender to be an optional array, required to be an array when it is filled. If sender is null, sender.name and sender.address would not be required, otherwise they need to be validated.

Every rule is a keyNested check chained to the object $validator in the master class and virtually becomes something like

$validator->keyNested('external_order_id', v::intType(), true)
    ->keyNested('courier_alias', v::stringType(), true)
    ->keyNested('sender', v::optional(v::arrayType()), false)
    // ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant