renanhangai/validator

Validate any php variable using a very simple sintax.

2.3.4 2022-09-05 15:33 UTC

This package is auto-updated.

Last update: 2024-05-05 20:35:25 UTC


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

Methods

  • any()

    Validate all objects

  • arrayOf($rule)

    Validate every element on the array against the $rule

  • blacklist($chars)

    Remove any char found in $chars from the string

  • boolval()

    Convert the value to a boolean

  • call($fn)

    Call the function $fn to validate the value

  • cnpj()

    Brazilian CNPJ validator

  • cpf()

    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)

  • dependsOn()

    Add a rule dependency. (Only works for objects)

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

    Convert the value to a float

  • instanceOf($type)

    Check if the object is an instance of the given type

  • intval()

    Convert the value to an int and fails if cannot be safely convertible

  • len($min, $max = null)

    Check for string or array length

  • map($map)

    Map a value to another

  • minlen($min)

    Check if string has at least $min length

  • obj($definition)

    Convert the value to an object and validate its fields

  • optional()

    Optional validator (Bypass if null or '')

  • preg_replace($search, $replace)

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

  • range($min, $max)

    Range validator

  • regex($pattern)

    Validate the value against the $pattern

  • required()

    Required validator (Fails if null or '')

  • set($items)

    Check if value is in set

  • skippable()

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

  • str_replace($search, $replace)

    Replace the characters on the string

  • strval($trim = true)

    Convert the object to a string value

  • whitelist($chars)

    Remove any char NOT found in $chars from the string