Skip to content

Conversation

@henriquemoody
Copy link
Member

  • v::keySet(Key $rule...)

Validates a keys in a defined structure.

$dict = array('foo' => 42);

v::keySet(
    v::key('foo', v::int())
)->validate($dict); //true

Extra keys are not allowed:

$dict = array('foo' => 42, 'bar' => 'String');

v::keySet(
    v::key('foo', v::int())
)->validate($dict); //false

Missing required keys are not allowed:

$dict = array('foo' => 42, 'bar' => 'String');

v::keySet(
    v::key('foo', v::int()),
    v::key('bar', v::string()),
    v::key('baz', v::bool())
)->validate($dict); //false

Missing non-required keys are allowed:

$dict = array('foo' => 42, 'bar' => 'String');

v::keySet(
    v::key('foo', v::int()),
    v::key('bar', v::string()),
    v::key('baz', v::bool(), false)
)->validate($dict); //true

The keys' order is not considered in the validation.

Fix #350

@alganet
Copy link
Member

alganet commented Jul 22, 2015

I like it!

@henriquemoody henriquemoody merged commit 7d9d190 into Respect:master Jul 22, 2015
@vikyd
Copy link

vikyd commented Sep 28, 2017

Can Extra keys are not allowed optional ?
I have many params in my $_POST, I will only use some of them.

@henriquemoody
Copy link
Member Author

Then just use a chain of key() rules, @Viky-zhang :)

KeySet exists only to validate structures, or else, just use nested key().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants