All Projects → rethinkphp → json-validator

rethinkphp / json-validator

Licence: MIT license
A json validator in PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to json-validator

Minecraft-bedrock-json-schemas
The JSON validation schema files for Minecraft bedrock
Stars: ✭ 17 (-67.31%)
Mutual labels:  json-schema, json-validation
flowjv
Flow based approach to JSON validation.
Stars: ✭ 20 (-61.54%)
Mutual labels:  json-schema, json-validation
robotframework-jsonvalidator
Robot Framework library for JSON validation
Stars: ✭ 21 (-59.62%)
Mutual labels:  json-schema, json-validation
json-schema
Clojure library JSON Schema validation and generation - Draft-07 compatible
Stars: ✭ 52 (+0%)
Mutual labels:  json-schema, json-validation
jest-json-schema
✨ JSON schema matcher for Jest
Stars: ✭ 152 (+192.31%)
Mutual labels:  json-schema
restish
Restish is a CLI for interacting with REST-ish HTTP APIs with some nice features built-in
Stars: ✭ 453 (+771.15%)
Mutual labels:  json-schema
another-json-schema
Another JSON Schema validator, simple & flexible & intuitive.
Stars: ✭ 48 (-7.69%)
Mutual labels:  json-schema
json matcher
Library for simplifying data verification in functional tests for your JSON-based APIs
Stars: ✭ 24 (-53.85%)
Mutual labels:  json-schema
jsonresume-theme-caffeine
Caffeine theme for the JSON Resume project
Stars: ✭ 78 (+50%)
Mutual labels:  json-schema
api-data
Static JSON data from the API, plus a JSON Schema
Stars: ✭ 88 (+69.23%)
Mutual labels:  json-schema
jackson-json-reference
JSON Reference for Java with Jackson.
Stars: ✭ 21 (-59.62%)
Mutual labels:  json-schema
json-schema
JSON schema validation
Stars: ✭ 20 (-61.54%)
Mutual labels:  json-schema
MOSP
A collaborative platform for creating, editing and sharing JSON objects.
Stars: ✭ 72 (+38.46%)
Mutual labels:  json-schema
angular-schema-form-bootstrap
Bootstrap decorator for Angular Schema Form
Stars: ✭ 50 (-3.85%)
Mutual labels:  json-schema
express-objection-starter
an opinionated, production-ready, isomorphic express/knex/objection starter with centralized configuration
Stars: ✭ 19 (-63.46%)
Mutual labels:  json-schema
openshift-json-schema
A set of JSON schemas for various OpenShift versions, extracted from the OpenAPI definitions
Stars: ✭ 23 (-55.77%)
Mutual labels:  json-schema
finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-65.38%)
Mutual labels:  json-schema
ans-schema
JSON schema definition and supporting example/validation code for The Washington Post's ANS specification
Stars: ✭ 92 (+76.92%)
Mutual labels:  json-schema
as-typed
Ambient mapping from JSON schema to typescript
Stars: ✭ 97 (+86.54%)
Mutual labels:  json-schema
libgltf
glTF 2.0 parser/loader for C++11, supports many extensions likes `KHR_draco_mesh_compression`, `KHR_lights_punctual`, `KHR_materials_clearcoat`, and more.
Stars: ✭ 55 (+5.77%)
Mutual labels:  json-schema

JSON Validator

A JSON Validator that designed to be elegant and easy to use.

Motivation

JSON Validation is a common task in automated API testing, JSON-Schema is complex and not easy to use, so i created this library to simplify the JSON validation process and made JSON validation more elegant and fun.

Features

  • JSON Schema validation, useful for automated API testing
  • Custom type support, it is possible to define your custom types and reuse it everywhere
  • Nullable type support
  • More is coming...

Installation

You can install the latest version of JSON validator with the following command:

composer require  rethink/json-validator:dev-master 

Documentation

Types

By default, JSON Validator shipped with seven kinds of built-in types:

  • integer
  • double
  • boolean
  • string
  • number
  • array
  • object

Besides the built-in types, it is possible to define your custom type via defineType() method.

The following code snippets shows how we can define custom types through array or callable.

1. Define a composite type

$validator->defineType('User', [
    'name' => 'string',
    'gender' => 'string',
    'age' => '?integer',
    'rating' => '?integer|boolean',
]);

This example defines a custom type named User, which have four properties. name and gender require be a string, age requires be an integer but allows to be nullable, and rating required to integer or boolean and allows to be null.

2. Define a list type

$validator->defineType('UserCollection', ['User']);

This defines UserCollection to be an array of User. In order to define a list type, the definition of the type much contains only one element.

3. Define a type in callable

$validator->defineType('timestamp', function ($value) {
    if ((!is_string($value) && !is_numeric($value)) || strtotime($value) === false) {
        return false;
    }

    $date = date_parse($value);

    return checkdate($date['month'], $date['day'], $date['year']);
});

It is also possible to define a type using a callable, which is useful to perform some validation on the data. Such as the example above defined a timestamp type, that requires the data to be a valid datetime.

Validate a Type

We can validate a type by the following two steps:

1. Create a Validator instance

use rethink\jsv\Validator;

$validator = new Validator();
// $validator->defineType(...)  Add your custom type if necessary

2. Preform the validation

$matched = $validator->matches($data, 'User');
if ($matched) {
    // Validation passed
} else {
    $errors = $validator->getErrors();
}

This example will check whether the given $data matches the type User, if validation fails, we can get the error messages through getErrors() method.

Strict Mode

In some situations, we may want an object matches our type strictly, we can utilizing strict mode to achieve this, the following is the example:

$data = [
    'name' => 'Bob',
    'gender' => 'Male',
    'age' => 19,
    'phone' => null, // This property is unnecessary
];
$matched = $validator->matches($data, 'User', true); // strict mode is turned on
var_dump($matched); // false is returned

Related Projects

  • Blink Framework - A high performance web framework and application server in PHP. Edit
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].