renanhangai / validator
Validate any php variable using a very simple sintax.
Requires
- marcj/topsort: ^1.1
Requires (Dev)
- phpunit/phpunit: ^6.4
- rtlopez/decimal: ^1.0
Suggests
- rtlopez/decimal: Allows decimal validation using v::decimal($digits, $decimal)
README
Validate objects against easily defined rules (See API Reference)
Installation
composer require renanhangai/validator
Getting Started
Simply validate some data
use libweb\Validator as v; // Data to be validated $data = array( "name" => "John Doe", "age" => "45", ); // Validate the data against the rule $data = v::validate( $data, array( "name" => v::s(), //< String validator "age" => v::i(), //< Integer validator "address?" => v::s(), //< Optional string validator )); $data->name === "John Doe"; $data->age === 45; $data->address === null;
Another Example
Simply validate some data
use libweb\Validator as v; $data = array( "name" => "John Doe", "age" => "45", "password" => "123456", "children" => array( array( "name" => "John Doe Jr", "age" => 15 ), array( "name" => "Mary Doe", "age" => 17 ), ), ); $data = v::validate( $data, array( "name" => v::s(), //< String validator "age" => v::i(), //< Integer validator "password" => v::s()->minlen( 6 ), "children" => v::arrayOf(array( "name" => v::s(), "age" => v::i(), )), ));
Chainability
Every method can be chained by using ->
v::s()->str_replace( "foo", "bar" )->regex('/testbar$/')->minlen(10) // Will pass on "mytestfoo", "another_testfoo", "testbar", "mytestbar"
API Reference
Summary
- Type validators
any()
arrayOf($rule)
boolval()
b()
date($format = null, $out = null)
decimal($digits, $decimal, $decimalSeparator = null, $thousandsSeparator = null)
floatval($decimal = null, $thousands = null, $asString = false)
f($decimal = null, $thousands = null, $asString = false)
instanceOf($type)
intval()
i()
obj($definition)
strval($trim = true)
s($trim = true)
- String rules
- Mixed
- Locale rules
- Meta
- Obligatoriness
- Numeric rules
Methods
-
Validate all objects
-
Validate every element on the array against the $rule
-
Remove any char found in $chars from the string
-
Convert the value to a boolean
-
Call the function $fn to validate the value
-
Brazilian CNPJ validator
-
Brazilian CPF validator
-
date($format = null, $out = null)
Convert the value to a date using the format (or keep if alredy a \DateTime)
-
decimal($digits, $decimal, $decimalSeparator = null, $thousandsSeparator = null)
Convert the value to a decimal with $digits and $decimal (needs rtlopes\Decimal)
-
Add a rule dependency. (Only works for objects)
-
floatval($decimal = null, $thousands = null, $asString = false)
Convert the value to a float
-
Check if the object is an instance of the given type
-
Convert the value to an int and fails if cannot be safely convertible
-
Check for string or array length
-
Map a value to another
-
Check if string has at least $min length
-
Convert the value to an object and validate its fields
-
Optional validator (Bypass if null or '')
-
preg_replace($search, $replace)
Replace the $search pattern on the string using $replace (Callback or string)
-
Range validator
-
Validate the value against the $pattern
-
Required validator (Fails if null or '')
-
Check if value is in set
-
Skippable validator (Bypass if null or '' and does not set the property)
-
str_replace($search, $replace)
Replace the characters on the string
-
Convert the object to a string value
-
Remove any char NOT found in $chars from the string