2017 © Pedro Peláez
 

library aiv

api input validator (with support for Silex)

image

renegare/aiv

api input validator (with support for Silex)

  • Monday, September 15, 2014
  • by renegare
  • Repository
  • 0 Watchers
  • 0 Stars
  • 370 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

API Input Validator

Build Status Coverage Status, (*1)

Requirements

  • PHP 5.4
  • composer (preferably latest)

Installation

$ composer require renegare/aiv:dev-master

Usage Examples:

Silex Usage

<?php

$app = new \Silex\Application();
$app->register(new \AIV\Integration\SilexProvider, [
    'aiv.validators' => [
        'test-name' => [
            'options' => [
                'allow.extra.params' => true,
                'allow.missing.params' => true
            ],
            'params' => [
                'name' => [
                    'not.blank',
                    [
                        'type' => 'length',
                        'options' => ['min' => 2, 'max' => 20]]],
                'email' => ['not.blank', '%email.validator%'],
                'password' => ['not.blank']]]]]);

$app['email.validator'] = $app->share(function() {
    return new \Symfony\Component\Validator\Constraints\Email;
});

$app->post('/', function(Application $app) {

    $apiValidator = $app['validator'];

    if($apiValidator->hasErrors('test-name')) {

        $errors = [];
        foreach($apiValidator->getErrors('test-name') as $violation) {
            $path = preg_replace('/[\[\]]/', '', $violation->getPropertyPath());
            $errors[$path] = $violation->getMessage();
        }
        return sprintf('You have errors: <pre>%s</pre>', print_r($errors, true));

    } else {

        return sprintf('You sent me valid data:<br /><pre>%s</pre>', print_r($apiValidator->getData('test-name'), true));
    }
});

$app->run();

JSON Input

Typical JSON Rest API applications take input from the body in the form of json. There the default input handler will not work as it effectively looks in the $_POST array., (*2)

Simply add this code after registering the provider:, (*3)

$app['aiv.input'] = $this->share(function(){
    new \AIV\Input\SymfonyRequest\JSONInput;
});

Test

Check out the repo and from the top level directory run the following command (xdebug required for coverage):, (*4)

$ composer update && vendor/bin/phpunit --coverage-text

Road Map

  • [ ] "Modelesque" Classes that represent validation requirements ($instance_variables vs annotations)

Behind The Scene Lib

The actual validation of data is handled by: [Symfony/Validator Component][1], (*5)

The Versions

15/09 2014

dev-master

9999999-dev

api input validator (with support for Silex)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mudi Ugbowanko

validator silex renegare skip

12/06 2014

v0.0.2

0.0.2.0

api input validator (with support for Silex)

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mudi Ugbowanko

validator silex renegare skip

05/06 2014

v0.0.1

0.0.1.0

api input validator (with support for Silex)

  Sources   Download

BWL

The Requires

 

The Development Requires

by Mudi Ugbowanko

validator silex renegare skip