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
Validate array structure #350
Comments
It is possible to do something like:
but how do I ensure that there are no additional, unexpected keys while keeping the code relatively DRY? Edit: This does what I want, but it is neither DRY nor obvious: $validator = v::arr()
->key('user_id', v::int(), false)
->key('username', v::string(), false)
->each(null, v::oneOf(v::equals('user_id'),
v::equals('username'))); |
I'm sorry by my late. In our feature guide (http://respect.li/Validation/docs/) there is the Custom Rules section that might be useful for you if you want to create custom rules. I'm not sure why you want a specific array structure, can you elaborate the reason why? Maybe we can implement on Validation if that's a common need. |
Hmn, maybe @oschwald wants to validate that the array has only those keys and nothing more. I can't think on using anything different from what he came up with. He can create a custom rule for |
My use case is to validate a data structure matches the required schema for a web service, ensuring that there are no invalid or misspelled keys before the request is sent. |
How about: $validator = v::arr()
->key('user_id', v::int(), false)
->key('username', v::string(), false)
->length(2); //only specified ammount of keys by count Or a more specific approach with the call validator: $validator = v::arr()
->key('user_id', v::int(), false)
->key('username', v::string(), false)
->call('array_keys', v::equals(['user_id', 'username'])); The |
Not sure that would work for my use case as most, but not all of the keys are optional. The solution I posted does work, but it would be nice to have a solution that is both simpler and does not require the key names to be repeated several times. Perhaps some tool in the spirit of JSON Schema would be a better fit, but I was otherwise very happy with this library. |
Maybe we could provide |
I understood the request but I think it's unnecessary, with the However, I created #374 since looks like there is some interest on that rule, which I will probably not use. |
That looks great to me. 👍 |
I am sure this must be possible, but I am having a hard time figuring out how to validate array structure with this library. By this, I mean that the keys of the array are within a set of key values and that each value associated with each key is of the right type. I initially thought this might be possible with each, but I don't see a way to ensure that the value is the right type for the key. For instance, if I have arrays of the form
['username' => 'name', 'user_id' => 102]
, how do I validate that the array only containsusername
anduser_id
and thatusername
always points to a string whileuser_id
points to an integer?The text was updated successfully, but these errors were encountered: