Skip to content

Validating an array of objects #39

@craig-cogdill

Description

@craig-cogdill

More of a help request than an issue...

I have a POST route that takes an array of objects as the body, like this:

[
   {
      "id": 1234,
      "key": "value"
   },
   {
      "id": 5678
      "key": "value2"   
   }
]

I want to validate that only those two keys exist in each object (no extra keys), and that their values are correct (int and string).

Is it possible to validate this with the library? I have tried this:

$validator = v::each(
   v::keySet(
      v::key("id", v::int()),
      v::key("key", v::string())   
   )
);

// ... at the end of the route ...

})->add(new \DavidePastore\Slim\Validation\Validation($validator));

So far I have not been successful and I am not sure what I am missing.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions