mrferos / comfort
Data validation library
Installs: 1 207
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 1
Open Issues: 0
Requires
Requires (Dev)
- phpunit/phpunit: ^4.0
README
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.
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:
- 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)
- if the string is exactly 2 characters, then:
- street_addr1, city:
Usage
Using Comfort entails defining your schema like we do above which returns a callable to be used like so:
$data = $registrationSchema($jsonData);
This will do two things:
- 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.
See more at our API reference: API.md
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:
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.
Note: validators must extend \Comfort\Validator\AbstractValidator