PHPackages                             arfeen/json-schema-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. arfeen/json-schema-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

arfeen/json-schema-validator
============================

Validates JSON using predefined schema based on data types and constraints.

1.0.0(8y ago)2131PHPPHP ^5.3.0 || ^7.0

Since Mar 15Pushed 8y ago2 watchersCompare

[ Source](https://github.com/arfeen/json-schema-validator)[ Packagist](https://packagist.org/packages/arfeen/json-schema-validator)[ RSS](/packages/arfeen-json-schema-validator/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependenciesVersions (2)Used By (0)

JSON Schema Validator
=====================

[](#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:
------------

[](#constraints)

- max\_length (string length)
- required (for mandatory fiels)
- value\_range min\_value max\_value (for numeric field)
- choices \[choices array\] (for predefined choices)
- min\_item\_count
- max\_item\_count (for array data type, to restrict number of items)
- sub\_nodes (for nested objects)

Example Schema:
---------------

[](#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

[](#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:

[](#usage)

```
