PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Muhammad Arfeen   JSON Schema Validator   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: JSON Schema Validator
Validate JSON data using a structure specification
Author: By
Last change:
Date: 6 years ago
Size: 2,640 bytes
 

Contents

Class file image Download
<?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": "This is titlle",
  "action": "CUSTOMER_CREATE",
  "transaction_amount": "44",
  "transaction_date": "2018-01-01",
  "customer_ids": [
    1001,
    1002
  ],
  "is_active": true,
  "additional_data": {
    "additional_field_2": 10,
     "additional_field_1": "this is text"
  }
}
'
;

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


if(
$validated){
    echo
"JSON Validated";
} else {
    echo
"JSON not validated";
}
print_r($jsonschemavalidator->getErrors());


?>