2017 © Pedro Peláez
 

library comfort

Data validation library

image

mrferos/comfort

Data validation library

  • Sunday, October 16, 2016
  • by mrferos
  • Repository
  • 3 Watchers
  • 2 Stars
  • 247 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 5 % Grown

The README.md

Comfort

Travis-CI Scrutinizer Code Quality Coverage Status
, (*1)

Easy & Flexible validation for your data.

Comfort is an easy to use validation lib styled after Joi, an excellent (and so far, better featured) object validation lib., (*2)

Example

$json = <<<JSON
{
    "first_name": "Andres",
    "last_name": "Galindo",
    "email": "test@test.com",
    "address": {
        "street_addr1": "123 Joi lane",
        "city": "Miami",
        "state": "FL"
    }
}
JSON;

$stateArray = array(
    'AL'=>'ALABAMA',
    'AK'=>'ALASKA',
    'AS'=>'AMERICAN SAMOA',
    'AZ'=>'ARIZONA',
    'AR'=>'ARKANSAS',
    'CA'=>'CALIFORNIA',
    'CO'=>'COLORADO',
    'CT'=>'CONNECTICUT',
    'DE'=>'DELAWARE',
    'DC'=>'DISTRICT OF COLUMBIA',
    'FM'=>'FEDERATED STATES OF MICRONESIA',
    'FL'=>'FLORIDA',
    /** taken out for brevity */
);

$registrationSchema = cmf()->json()->keys([
    "first_name" => cmf()->string()->required()->alpha()->min(1),
    "last_name"  => cmf()->string()->required()->alpha()->min(1),
    "email" => cmf()->string()->email(),
    "address"    => cmf()->array()->keys([
        "street_addr1" => cmf()->string()->required()->min(2),
        "street_addr2" => cmf()->string()->optional()->min(2),
        "city"         => cmf()->string()->required()->min(2),
        "state"        => cmf()->string()->alternatives([
            [
                'is' => cmf()->string()->length(2),
                'then' => cmf()->string()->anyOf(array_keys($stateArray)),
                'else' => cmf()->string()->anyOf(array_values($stateArray))
            ],
        ])
    ])->required()
]);

The schema defined above validates the following:, (*3)

  • first_name, last_name:
    • must be a string
    • is required
    • contain only alpha (a-z) characters
    • a minimum of 1 character
  • email:
    • must be a string
    • must be an email
    • is optional
  • address:
    • must be an array
    • must have the following keys (also with their own validation)
      • street_addr1, city:
        • must be a string
        • is required
        • contains at least 2 characters
      • street_addr2:
        • must be a string
        • contains at least 2 characters
        • is _optional_ (everything is optional unless made explicitly required())
      • state:
        • if the string is exactly 2 characters, then:
          • the string may be any of the state short codes (e.g. FL)
        • else the string must be any of the state long forms (e.g. Florida)

Usage

Using Comfort entails defining your schema like we do above which returns a callable to be used like so:, (*4)

$data = $registrationSchema($jsonData);

This will do two things:, (*5)

  • Run the validation stack
  • Return the data as an array (see more on this below) or a ValidationError instance

Only the json() validator currently takes the liberty of automatically transforming your data, the other validators will currently return data in the type it was passed to., (*6)

See more at our API reference: API.md, (*7)

Custom Validators

When in need of business specific validations, the mechanism is simple. If for instance, the email validation must be changed to allow your custom validation schema it can be accomplished like so:, (*8)

class CustomerStringValidator extends \Comfort\Validator\StringValidator {        
        public function email()
        {
            return $this->add(function ($value, $nameKey) {
                if (strstr($value, 'test')) {
                    return $this->createError('string.email', $value, $nameKey);
                }
            });
        }
}

\Comfort\Comfort::registerValidator('string', CustomerStringValidator::class);

Subsequent calls to cmf()->string()->email('test@gmail.com') would run this new validator., (*9)

Note: validators must extend \Comfort\Validator\AbstractValidator, (*10)

The Versions

16/10 2016

dev-master

9999999-dev

Data validation library

  Sources   Download

MIT

The Development Requires

by Andres Galindo

20/05 2016

0.2.5

0.2.5.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo

19/05 2016

0.2.4

0.2.4.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo

19/05 2016

0.2.3

0.2.3.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo

19/05 2016

0.2.2

0.2.2.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo

16/05 2016

0.2.1

0.2.1.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo

16/05 2016

0.2.0

0.2.0.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo

16/05 2016

0.1.0

0.1.0.0

Data validation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andres Galindo