1

I have the following JSON:

{
    "articles":[
        {"amount": 3, "vat": 19},
        {"amount": 2, "vat": 7}
    ]
}

I want to validate that articles is an array and has the keys amount as a number and vat as a number. I'm trying the following:

'articles' => v::arrayType()->keySet(
                        v::key('amount', v::intVal()),
                        v::key('vat', v::intVal())
                    )

As result I get:

{"articles":["Must have keys { \"amount\", \"vat\" }"]}}

What am I doing wrong here? I think it should do it's job: https://github.com/Respect/Validation/blob/master/docs/KeySet.md

Thanks!

EDIT

Here's the validation code I'm using and the way i retrieve my JSON (in the request object):

public function validate($request, array $rules)
    {
      foreach ($rules as $field => $rule) {
        try{
          $rule->setName(ucfirst($field))->assert($request->getParam($field));
        } catch(NestedValidationException $e){
          $this->errors[$field] = $e->getMessages();
        }
      }
      $this->ret['error'] = "Validation Error";
      $this->ret['errornr'] = 600;
      $this->ret['fields'] = $this->errors;
      return $this;
    }


$validation = $this->validator->validate($request, [
      'paymentmethod' => v::notEmpty(),
      'memberid' => v::notEmpty()->intVal(),
      'articles' => v::arrayVal()
    ]);
Florian Sauerwein
  • 445
  • 1
  • 5
  • 19

0 Answers0