PHP Classes
elePHPant
Icontem

JSON Schema Validator: Validate JSON data using a structure specification

Recommend this page to a friend!
  Info   View files Example   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-03-14 (6 months ago) RSS 2.0 feedNot enough user ratingsTotal: 174 This week: 1All time: 8,374 This week: 438Up
Version License PHP version Categories
jsonschema-validator 1.0.0Freely Distributable5.4PHP 5, Data types, Validation
Description Author

This package can validate JSON data using a structure specification.

It takes as parameters a string with a value in JSON format and an array with a schema definition that provides details of the types and constraints expected in the JSON data to determine if it is valid.

The package validates the JSON data structure and returns whether it matches the provided structure specification, as well any error messages in case it was considered invalid.

Innovation Award
PHP Programming Innovation award nominee
March 2018
Number 2
JSON is a well known structured data format that modern applications use to exchange data.

Even though the JSON format and existing parsers are very robust, they do not validate the consistency of the data types and relationships that applications expect.

This package can validate a JSON data structure to determine if it matches a given specification that an application requires.

Manuel Lemos
Name: Muhammad Arfeen is available for providing paid consulting. Contact Muhammad Arfeen .
Classes: 9 packages by
Country: Pakistan Pakistan
Age: 40
All time rank: 4644 in Pakistan Pakistan
Week rank: 234 Up5 in Pakistan Pakistan Down
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Details

JSON Schema Validator

This class validates JSON payloads by using predefined schema based on basic necessary data types. Data types validation can be extended by creating new classes and new constraints can be added by writing new functions in Constraints class.

This schema validator works on following types:

  • text
  • numeric
  • date
  • array
  • boolean
  • choices

Constraints:

  • max_length (string length)
  • required (for mandatory fiels)
  • value_range min_value max_value (for numeric field)
  • choices choices array
  • min_item_count
  • max_item_count (for array data type, to restrict number of items)
  • sub_nodes (for nested objects)

Example Schema:

$_payLoadSchema = [
    [
        'node' => [
            'name' => 'title',
            'type' => 'text',
            'required' => true,
            'max_length' => 20
        ]
    ],
    [
        'node' => [
            'name' => 'action',
            'type' => 'text',
            'required' => true,
            'max_length' => 20,
            'choices' => [
                "CUSTOMER_DATA",
                "CUSTOMER_CREATE"
            ]
        ]
    ],
    [
        'node' => [
            'name' => 'transaction_amount',
            'type' => 'numeric',
            'required' => true,
            'value_range' => [
                'min_value' => 10,
                'max_value' => 100
            ]
        ]
    ],
    [
        'node' => [
            'name' => 'transaction_date',
            'type' => 'choices',
            'required' => true,
            'choices' => [
                "2018-01-01",
                "2018-01-02"
            ]
        ]
    ],
    [
        'node' => [
            'name' => 'customer_ids',
            'type' => 'array',
            'required' => true,
            'max_item_count' => 2
        ]
    ],
    [
        'node' => [
            'name' => 'is_active',
            'type' => 'boolean',
            'required' => false
        ]
    ],
    [
        'node' => [
            'name' => 'additional_data',
            'type' => 'object',
            'sub_nodes' => [
                ['node' => [
                        'name' => 'additional_field_1',
                        'type' => 'text',
                        'max_length' => "100"
                    ]],
                ['node' => [
                        'name' => 'additional_field_2',
                        'type' => 'numeric'
                    ]]
            ]
        ]
    ]
];

Sample JSON Payload to Validate
$json_raw = '
{
  "title": "This is titlle",
  "action": "CUSTOMER_CREATE",
  "transaction_amount": "44",
  "transaction_date": "2018-01-01",
  "customer_ids": [
    1001,
    1002,
    1003
  ],
  "is_active": true,
  "additional_data": {
    "additional_field_2": 10
  }
}    
';

Usage:

<?php

	$jsonschemavalidator = new JsonSchemaValidator();
	$jsonschemavalidator->validateSchema($_payLoadSchema, $json_raw);
	
	if($validated){
		echo "JSON Validated";
	} else {
		echo "JSON not validated";
		print_r($jsonschemavalidator->getErrors());
	}

  Files folder image Files  
File Role Description
Files folder imageJSONSchema (2 files, 1 directory)
Plain text file Autoloader.php Class Class source
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  JSONSchema  
File Role Description
Files folder imageTypeClasses (9 files)
  Plain text file JsonSchemaValidator.php Class Class source
  Plain text file SchemaException.php Class Class source

  Files folder image Files  /  JSONSchema  /  TypeClasses  
File Role Description
  Plain text file Constraints.php Class Class source
  Plain text file TypeArray.php Class Class source
  Plain text file TypeBool.php Class Class source
  Plain text file TypeCheck.php Class Class source
  Plain text file TypeChoices.php Class Class source
  Plain text file TypeDate.php Class Class source
  Plain text file TypeNumeric.php Class Class source
  Plain text file TypeText.php Class Class source
  Plain text file TypeValidation.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:174
This week:1
All time:8,374
This week:438Up