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

Validate subkey of a XML request #11

Merged
merged 4 commits into from
Oct 6, 2016
Merged

Conversation

DavidePastore
Copy link
Owner

@DavidePastore DavidePastore commented Oct 3, 2016

Let's say you have a POST request with a XML in its body:

<person>
   <type>emails</type>
   <objectid>1</objectid>
   <email>
     <id>1</id>
     <enable_mapping>1</enable_mapping>
     <name>rq3r</name>
     <created_at>2016-08-23 13:36:29</created_at>
     <updated_at>2016-08-23 14:36:47</updated_at>
    </email>
</person>

and you want to validate the email.name key. You can do it in this way:

use Respect\Validation\Validator as v;

$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();
$container['apiValidation'] = function () {
  //Create the validators
  $typeValidator = v::alnum()->noWhitespace()->length(3, 5);
  $emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
  $validators = array(
    'type' => $typeValidator,
    'email' => array(
      'name' => $emailNameValidator,
    ),
  );

  return new \DavidePastore\Slim\Validation\Validation($validators);
};

If you'll have an error, the result would be:

//In your route
$errors = $this->apiValidation->getErrors();

print_r($errors); 
/*
Array
(
    [email.name] => Array
        (
            [0] => "rq3r" must have a length between 1 and 2
        )

)
*/

Things to do:

  • implementation;
  • add tests;
  • improve this PR text;
  • improve documentation.

Related to #10.

@DavidePastore DavidePastore added this to the v0.2.0 milestone Oct 6, 2016
@DavidePastore DavidePastore merged commit b44f94d into master Oct 6, 2016
@DavidePastore DavidePastore deleted the add-xml-request-support branch October 6, 2016 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant