2017 © Pedro Peláez
 

library validator

Validate any php variable using a very simple sintax.

image

renanhangai/validator

Validate any php variable using a very simple sintax.

  • Wednesday, February 7, 2018
  • by renanhangai
  • Repository
  • 1 Watchers
  • 0 Stars
  • 146 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 26 % Grown

The README.md

Validator

Validate objects against easily defined rules (See API Reference), (*1)

Installation

composer require renanhangai/validator

Getting Started

Simply validate some data, (*2)

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, (*3)

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 ->, (*4)

v::s()->str_replace( "foo", "bar" )->regex('/testbar$/')->minlen(10)
// Will pass on "mytestfoo", "another_testfoo", "testbar", "mytestbar"

, (*5)

API Reference

Summary

Methods

  • any(), (*6)

    Validate all objects, (*7)

  • arrayOf($rule), (*8)

    Validate every element on the array against the $rule, (*9)

  • blacklist($chars), (*10)

    Remove any char found in $chars from the string, (*11)

  • boolval(), (*12)

    Convert the value to a boolean, (*13)

  • call($fn), (*14)

    Call the function $fn to validate the value, (*15)

  • cnpj(), (*16)

    Brazilian CNPJ validator, (*17)

  • cpf(), (*18)

    Brazilian CPF validator, (*19)

  • date($format = null, $out = null), (*20)

    Convert the value to a date using the format (or keep if alredy a \DateTime), (*21)

  • decimal($digits, $decimal, $decimalSeparator = null, $thousandsSeparator = null), (*22)

    Convert the value to a decimal with $digits and $decimal (needs rtlopes\Decimal), (*23)

  • dependsOn(), (*24)

    Add a rule dependency. (Only works for objects), (*25)

  • floatval($decimal = null, $thousands = null, $asString = false), (*26)

    Convert the value to a float, (*27)

  • instanceOf($type), (*28)

    Check if the object is an instance of the given type, (*29)

  • intval(), (*30)

    Convert the value to an int and fails if cannot be safely convertible, (*31)

  • len($min, $max = null), (*32)

    Check for string or array length, (*33)

  • map($map), (*34)

    Map a value to another, (*35)

  • minlen($min), (*36)

    Check if string has at least $min length, (*37)

  • obj($definition), (*38)

    Convert the value to an object and validate its fields, (*39)

  • optional(), (*40)

    Optional validator (Bypass if null or ''), (*41)

  • preg_replace($search, $replace), (*42)

    Replace the $search pattern on the string using $replace (Callback or string), (*43)

  • range($min, $max), (*44)

    Range validator, (*45)

  • regex($pattern), (*46)

    Validate the value against the $pattern, (*47)

  • required(), (*48)

    Required validator (Fails if null or ''), (*49)

  • set($items), (*50)

    Check if value is in set, (*51)

  • skippable(), (*52)

    Skippable validator (Bypass if null or '' and does not set the property), (*53)

  • str_replace($search, $replace), (*54)

    Replace the characters on the string, (*55)

  • strval($trim = true), (*56)

    Convert the object to a string value, (*57)

  • whitelist($chars), (*58)

    Remove any char NOT found in $chars from the string, (*59)

The Versions

07/02 2018

dev-master

9999999-dev

Validate any php variable using a very simple sintax.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar renanhangai

validator validate simple data

07/02 2018

1.2.2

1.2.2.0

Validate any php variable using a very simple sintax.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar renanhangai

validator validate simple data

13/11 2017

1.2.1

1.2.1.0

Validate any php variable using a very simple sintax.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar renanhangai

validator validate simple data

13/11 2017

1.2.0

1.2.0.0

Validate any php variable using a very simple sintax.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar renanhangai

validator validate simple data

11/11 2017

1.1.0

1.1.0.0

Validate any php variable using a very simple sintax.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar renanhangai

validator validate simple data

11/11 2017

1.0.4

1.0.4.0

Validate any php variable using a very simple sintax.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar renanhangai

validator validate simple