PHP Classes

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    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 64%Total: 282 This week: 2All time: 7,586 This week: 96Up
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


Prize: One big elePHPant Plush Mascott
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
Picture of Muhammad Arfeen
Name: Muhammad Arfeen is available for providing paid consulting. Contact Muhammad Arfeen .
Classes: 9 packages by
Country: Pakistan Pakistan
Age: 46
All time rank: 4614 in Pakistan Pakistan
Week rank: 106 Up5 in Pakistan Pakistan Down
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Example

<?php

require_once("Autoloader.php");

/**
 * Example schema to be accepted via JSON input
 */
$_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'
                   
]]
            ]
        ]
    ]
];


/**
 * example JSON input
 */
$json_raw = '
{
  "title": "",
  "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
  }
}
'
;

use
JSONSchema\JsonSchemaValidator;

$jsonschemavalidator = new JsonSchemaValidator();
try {
   
$jsonschemavalidator->validateSchema($_payLoadSchema, $json_raw);
} catch (
Exception $e) {
    echo
$e->getMessage();
}

print_r($jsonschemavalidator->getErrors());


?>


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:282
This week:2
All time:7,586
This week:96Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:66%StarStarStarStar
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:732