bartosz-maciaszek / validation

Validation library for PHP inspired by Joi
MIT License
33 stars 6 forks source link

Cannot validate data from an array of objects #8

Open jonathansilva opened 3 years ago

jonathansilva commented 3 years ago

I'm trying to use the same features as Joi... accept object array and objects with key : value

[{"key": "value"}, ...] {"key": "value", ...}

Postman

Captura de tela de 2021-07-15 18-09-24

private function sanitize($data)
{
    $schema = V::obj()->keys([
        'myArray' => V::arr()->keys([
            'test' => V::string()
        ])
    ]);

    V::assert($data, $schema);
}

...

class ArrayKeys extends AbstractAssertion
{
    public function process(InputValue $input)
    {
        echo '<pre>';
        var_dump($input->getValue());
        echo '</pre>';

        foreach ($this->getOption('keys') as $key => $schema) {
            if (!array_key_exists($key, $input->getValue())) {
                throw new ValidationException(sprintf('key "%s" is missing', $key));
            }

            $this->validateAndReplaceKey($input, $key, $schema);
        }
    }
}