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

Validating an array of objects #39

Open
craig-cogdill opened this issue Sep 4, 2018 · 2 comments · May be fixed by #40
Open

Validating an array of objects #39

craig-cogdill opened this issue Sep 4, 2018 · 2 comments · May be fixed by #40

Comments

@craig-cogdill
Copy link

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.

@DavidePastore
Copy link
Owner

Hi @craig-cogdill . Your question makes a lot of sense. At the current state Slim-Validation won't validate payloads that start with an array. This is a limitation and I think that I'll try to find a way to fix the issue.

@DavidePastore
Copy link
Owner

I created the branch add-array-validation-at-first-position with the fix. Please let me know if you like the solution and if it works as expected. You can also see the code changes here: #40.

@DavidePastore DavidePastore linked a pull request Sep 6, 2018 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants