Download the PHP package powar/json-validator without Composer

On this page you can find all versions of the php package powar/json-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package json-validator

Introduction

EasyJsonValidator - simple Json validator for PHP. Very faster, easy to configuration. This validator work without JsonSchema(http://json-schema.org/).

Capability

  1. Simple type check, like field name - string
  2. Range check, like filed number - form 1 to 10, or field date -> from 2019-01-01 to 2019-02-01
  3. Require check, like field id is require
  4. Nested objects check, like multidimensional array with all available validation
  5. DateTime check in any date format
  6. Check by pattern (preg_match), like field email - /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i
  7. Check by callback, like field email - function($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); }
  8. Customisation error message for any rule

Installation

composer require powar/json-validator

Usage

get any json

$json = json_encode(['id' => 11, 'name' => 'JohnDoe', 'balance' => null, 'date' => date(DateTime::ISO8601)]);

create rules

$rules = [
    'id' => [
        Validator::KEY_TYPE => Validator::TYPE_INTEGER,
        Validator::KEY_MAX_VAL => 10,
    ],
    'name' => Validator::TYPE_STRING,
    'balance' => Validator::TYPE_NULL,
    'date' => Validator::TYPE_DATETIME,
];

validate

$validator = new \Powar\JsonValidator\Validator();
$validator->validate($json, $rules);

check errors exist

if ($validator->hasErrors()) {
    print_r($validator->getErrors());
}

array(1) {
   'id' =>
   string(20) "must be less than 10"
 }

Documentation

Make rules

The validate method takes a second parameter with array of rules for validation

simple example:

$rules = ['id' => 'int']
$validator->validate($json, $rules);

This rule will only check the incoming json for matching the id:integer field.

*Note, you can write explicitly 'int' or another type, but I recommend using Validator::TYPE constants.**

example with constant:

$rules = ['id' => Validator::TYPE_INTEGER];

List of constants for rules:

type constant available configuration
string Validator::TYPE_STRING KEY_REQUIRE, KEY_MAX_STR, KEY_MIN_STR, KEY_PATTERN, KEY_CALLBACK, KEY_LABEL
int Validator::TYPE_INTEGER KEY_REQUIRE, KEY_MAX_STR, KEY_MIN_STR, KEY_MIN_VAL, KEY_MAX_VAL,KEY_LABEL
number Validator::TYPE_NUMBER KEY_REQUIRE, KEY_MAX_VAL, KEY_MIN_VAL, KEY_LABEL
bool Validator::TYPE_BOOLEAN KEY_REQUIRE, KEY_BOOL_VAL, KEY_LABEL
array Validator::TYPE_ANY KEY_REQUIRE, KEY_LABEL
any Validator::TYPE_BOOLEAN KEY_REQUIRE, KEY_CALLBACK, KEY_LABEL
datetime Validator::TYPE_DATETIME KEY_REQUIRE, KEY_MIN_VAL, KEY_MAX_VAL, KEY_FORMAT, KEY_LABEL
null Validator::TYPE_NULL KEY_REQUIRE, KEY_LABEL

example with configure:

$rules = ['id' => [
            Validator::KEY_TYPE => Validator::TYPE_INTEGER,
            Validator::KEY_MAX_VAL => 10,
            ]
         ];

_Note that now id contains an array where the required key is Validator :: KEY_TYPE, which must contain the type as in the simple case_

List of constants for configure:

constant string reqire description
Validator::KEY_TYPE type + required key if you use array with configuration rule
Validator::KEY_LABEL label - for customization error message
Validator::KEY_REQUIRE require - just checks whether json contains element. Default true
Validator::KEY_MIN_STR min-str - Minimum number of characters (php strlen)
Validator::KEY_MAX_STR max-str - Maximum number of characters (php strlen)
Validator::KEY_MIN_VAL min-val - Minimum value (php <)
Validator::KEY_MAX_VAL max-val - Maximum value (php >)
Validator::KEY_PATTERN pattern - check by regexp (php preg_match)
Validator::KEY_FORMAT format - create date object from json, by format (php DateTime::createFromFormat)
Validator::KEY_BOOL format - create date object from json, by format (php DateTime::createFromFormat)
Validator::KEY_CALLBACK callback - check by callback (php is_callable -> call)
Validator::KEY_RULE rule - include rule

Some examples

You can use existence rule if you json contains included object

$someRule = [
    'some_field' => [
        Validator::KEY_TYPE => Validator::TYPE_INTEGER,
        Validator::KEY_MAX_VAL => 10,
        Validator::KEY_MAX_STR => 2,
    ],
    'some_date' => [
        Validator::KEY_TYPE => Validator::TYPE_DATETIME,
        Validator::KEY_FORMAT => 'Y-m-d\TH:i:sP',
        Validator::KEY_REQUIRE => false,
    ]
];

$rules = [
    'some_id' => Validator::TYPE_INTEGER,
    'some_clildren_element' => [
        Validator::KEY_TYPE => Validator::TYPE_ARRAY,
        Validator::KEY_RULE => $someRule,
    ]
];

Or you can create some small rules like this:

$idRule = [
        Validator::KEY_TYPE => Validator::TYPE_INTEGER,
        Validator::KEY_MIN_STR => 1,
        Validator::KEY_MAX_STR => 11,
];

$dateRule = [
    Validator::KEY_TYPE => Validator::TYPE_DATETIME,
    Validator::KEY_FORMAT => DateTime::ISO8601,
    Validator::KEY_REQUIRE => false,
];

And use it for any rules:

$rules = [
    'some_id' => $idRule,
    'some_date' => $dateRule,
];

NOTE! In case when you create rule for TYPE_DATETIME, you need be careful, because KEY_MIN_VAL and KEY_MAX_VAL can work is not obvious

For Example, you need to create check for day of birth (Over 18 years of age, you know ;) )

$minDate = new DateTime();
$minDate->modify('-18 year');
$format = 'Y-m-d';

$rule = [
    'day_of_birth' => [
        Validator::KEY_TYPE => Validator::TYPE_DATETIME,
        Validator::KEY_FORMAT => $format,
        Validator::KEY_MIN_VAL => $minDate->format($format), //<< WRONG
    ]
];

Should be with Validator::KEY_MAX_VAL :

$minDate = new DateTime();
$minDate->modify('-18 year');
$format = 'Y-m-d';

$rule = [
    'day_of_birth' => [
        Validator::KEY_TYPE => Validator::TYPE_DATETIME,
        Validator::KEY_FORMAT => $format,
        Validator::KEY_MAX_VAL => $minDate->format($format),
    ]
];

Callback example

 $rule = [
     'email' => [
         Validator::KEY_TYPE => Validator::TYPE_STRING,
         Validator::KEY_CALLBACK => function($email) {
            return filter_var($email, FILTER_VALIDATE_EMAIL);
         }
     ]
 ];

Custom error message

$someRule = [
    'some_field' => [
        Validator::KEY_TYPE => Validator::TYPE_INTEGER,
        Validator::KEY_MAX_VAL => 10,
        Validator::KEY_MAX_STR => 2,
        Validator::KEY_LABEL => 'validator.error.some_field',
    ],
];

getErrors() -> ['some_field' => 'validator.error.some_field']

More examples you can see in unit tests


All versions of json-validator with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package powar/json-validator contains the following files

Loading the files please wait ....