{"id":13623926,"url":"https://github.com/jsonrainbow/json-schema","last_synced_at":"2026-02-13T18:05:09.653Z","repository":{"id":37978007,"uuid":"1485595","full_name":"jsonrainbow/json-schema","owner":"jsonrainbow","description":"A PHP implementation of JSON Schema validation","archived":false,"fork":false,"pushed_at":"2025-05-07T19:56:30.000Z","size":1082,"stargazers_count":3579,"open_issues_count":7,"forks_count":357,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-05-11T13:03:37.257Z","etag":null,"topics":["json","schema"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsonrainbow.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2011-03-16T02:15:33.000Z","updated_at":"2025-05-09T03:00:45.000Z","dependencies_parsed_at":"2022-07-18T05:21:44.809Z","dependency_job_id":"d4c50c3f-e33c-4904-9d86-b6b0e0fd2554","html_url":"https://github.com/jsonrainbow/json-schema","commit_stats":{"total_commits":451,"total_committers":122,"mean_commits":3.69672131147541,"dds":0.9046563192904656,"last_synced_commit":"5174319bd0f45fe90bb88ee879b79ad36f551504"},"previous_names":["jsonrainbow/json-schema"],"tags_count":61,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonrainbow%2Fjson-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonrainbow%2Fjson-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonrainbow%2Fjson-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonrainbow%2Fjson-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsonrainbow","download_url":"https://codeload.github.com/jsonrainbow/json-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253671710,"owners_count":21945463,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","schema"],"created_at":"2024-08-01T21:01:37.035Z","updated_at":"2026-02-13T18:05:09.589Z","avatar_url":"https://github.com/jsonrainbow.png","language":"PHP","readme":"# JSON Schema for PHP\n\n[![Build Status](https://github.com/jsonrainbow/json-schema/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/jsonrainbow/json-schema/actions)\n[![Latest Stable Version](https://poser.pugx.org/justinrainbow/json-schema/v/stable)](https://packagist.org/packages/justinrainbow/json-schema)\n[![Total Downloads](https://poser.pugx.org/justinrainbow/json-schema/downloads)](https://packagist.org/packages/justinrainbow/json-schema/stats)\n\nA PHP Implementation for validating `JSON` Structures against a given `Schema` with support for `Schemas` of Draft-3 or Draft-4. Features of newer Drafts might not be supported. See [Table of All Versions of Everything](https://json-schema.org/specification-links.html#table-of-all-versions-of-everything) to get an overview of all existing Drafts.\n\nSee [json-schema](http://json-schema.org/) for more details.\n\n## Installation\n\n### Library\n\n```bash\ngit clone https://github.com/jsonrainbow/json-schema.git\n```\n\n### Composer\n\n[Install PHP Composer](https://getcomposer.org/doc/00-intro.md)\n\n```bash\ncomposer require justinrainbow/json-schema\n```\n\n## Usage\n\nFor a complete reference see [Understanding JSON Schema](https://json-schema.org/understanding-json-schema/).\n\n__Note:__ features of Drafts newer than Draft-4 might not be supported!\n\n### Basic usage\n\n```php\n\u003c?php\n\n$data = json_decode(file_get_contents('data.json'));\n\n// Validate\n$validator = new JsonSchema\\Validator;\n$validator-\u003evalidate($data, (object)['$ref' =\u003e 'file://' . realpath('schema.json')]);\n\nif ($validator-\u003eisValid()) {\n    echo \"The supplied JSON validates against the schema.\\n\";\n} else {\n    echo \"JSON does not validate. Violations:\\n\";\n    foreach ($validator-\u003egetErrors() as $error) {\n        printf(\"[%s] %s\\n\", $error['property'], $error['message']);\n    }\n}\n```\n\n### Type coercion\n\nIf you're validating data passed to your application via HTTP, you can cast strings and booleans to\nthe expected types defined by your schema:\n\n```php\n\u003c?php\n\nuse JsonSchema\\SchemaStorage;\nuse JsonSchema\\Validator;\nuse JsonSchema\\Constraints\\Factory;\nuse JsonSchema\\Constraints\\Constraint;\n\n$request = (object)[\n    'processRefund'=\u003e\"true\",\n    'refundAmount'=\u003e\"17\"\n];\n\n$validator-\u003evalidate(\n    $request, (object) [\n        \"type\"=\u003e\"object\",\n        \"properties\"=\u003e(object)[\n            \"processRefund\"=\u003e(object)[\n                \"type\"=\u003e\"boolean\"\n            ],\n            \"refundAmount\"=\u003e(object)[\n                \"type\"=\u003e\"number\"\n            ]\n        ]\n    ],\n    Constraint::CHECK_MODE_COERCE_TYPES\n); // validates!\n\nis_bool($request-\u003eprocessRefund); // true\nis_int($request-\u003erefundAmount); // true\n```\n\nA shorthand method is also available:\n```PHP\n$validator-\u003ecoerce($request, $schema);\n// equivalent to $validator-\u003evalidate($data, $schema, Constraint::CHECK_MODE_COERCE_TYPES);\n```\n\n### Default values\n\nIf your schema contains default values, you can have these automatically applied during validation:\n\n```php\n\u003c?php\n\nuse JsonSchema\\Validator;\nuse JsonSchema\\Constraints\\Constraint;\n\n$request = (object)[\n    'refundAmount'=\u003e17\n];\n\n$validator = new Validator();\n\n$validator-\u003evalidate(\n    $request,\n    (object)[\n        \"type\"=\u003e\"object\",\n        \"properties\"=\u003e(object)[\n            \"processRefund\"=\u003e(object)[\n                \"type\"=\u003e\"boolean\",\n                \"default\"=\u003etrue\n            ]\n        ]\n    ],\n    Constraint::CHECK_MODE_APPLY_DEFAULTS\n); //validates, and sets defaults for missing properties\n\nis_bool($request-\u003eprocessRefund); // true\n$request-\u003eprocessRefund; // true\n```\n\n### With inline references\n\n```php\n\u003c?php\n\nuse JsonSchema\\SchemaStorage;\nuse JsonSchema\\Validator;\nuse JsonSchema\\Constraints\\Factory;\n\n$jsonSchema = \u003c\u003c\u003c'JSON'\n{\n    \"type\": \"object\",\n    \"properties\": {\n        \"data\": {\n            \"oneOf\": [\n                { \"$ref\": \"#/definitions/integerData\" },\n                { \"$ref\": \"#/definitions/stringData\" }\n            ]\n        }\n    },\n    \"required\": [\"data\"],\n    \"definitions\": {\n        \"integerData\" : {\n            \"type\": \"integer\",\n            \"minimum\" : 0\n        },\n        \"stringData\" : {\n            \"type\": \"string\"\n        }\n    }\n}\nJSON;\n\n// Schema must be decoded before it can be used for validation\n$jsonSchemaObject = json_decode($jsonSchema);\n\n// The SchemaStorage can resolve references, loading additional schemas from file as needed, etc.\n$schemaStorage = new SchemaStorage();\n\n// This does two things:\n// 1) Mutates $jsonSchemaObject to normalize the references (to file://mySchema#/definitions/integerData, etc)\n// 2) Tells $schemaStorage that references to file://mySchema... should be resolved by looking in $jsonSchemaObject\n$schemaStorage-\u003eaddSchema('file://mySchema', $jsonSchemaObject);\n\n// Provide $schemaStorage to the Validator so that references can be resolved during validation\n$jsonValidator = new Validator(new Factory($schemaStorage));\n\n// JSON must be decoded before it can be validated\n$jsonToValidateObject = json_decode('{\"data\":123}');\n\n// Do validation (use isValid() and getErrors() to check the result)\n$jsonValidator-\u003evalidate($jsonToValidateObject, $jsonSchemaObject);\n```\n\n### Configuration Options\nA number of flags are available to alter the behavior of the validator. These can be passed as the\nthird argument to `Validator::validate()`, or can be provided as the third argument to\n`Factory::__construct()` if you wish to persist them across multiple `validate()` calls.\n\n| Flag | Description |\n|------|-------------|\n| `Constraint::CHECK_MODE_NORMAL` | Validate in 'normal' mode - this is the default |\n| `Constraint::CHECK_MODE_TYPE_CAST` | Enable fuzzy type checking for associative arrays and objects |\n| `Constraint::CHECK_MODE_COERCE_TYPES` | Convert data types to match the schema where possible |\n| `Constraint::CHECK_MODE_EARLY_COERCE` | Apply type coercion as soon as possible |\n| `Constraint::CHECK_MODE_APPLY_DEFAULTS` | Apply default values from the schema if not set |\n| `Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS` | When applying defaults, only set values that are required |\n| `Constraint::CHECK_MODE_EXCEPTIONS` | Throw an exception immediately if validation fails |\n| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate \"format\" constraints |\n| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | Validate the schema as well as the provided document |\n\nPlease note that using `CHECK_MODE_COERCE_TYPES` or `CHECK_MODE_APPLY_DEFAULTS` will modify your\noriginal data.\n\n`CHECK_MODE_EARLY_COERCE` has no effect unless used in combination with `CHECK_MODE_COERCE_TYPES`. If\nenabled, the validator will use (and coerce) the first compatible type it encounters, even if the\nschema defines another type that matches directly and does not require coercion.\n\n## Running the tests\n\n```bash\ncomposer test                            # run all unit tests\ncomposer testOnly TestClass              # run specific unit test class\ncomposer testOnly TestClass::testMethod  # run specific unit test method\ncomposer style-check                     # check code style for errors\ncomposer style-fix                       # automatically fix code style errors\n```\n\n# Contributors  ✨\nThanks go to these wonderful people, without their effort this project wasn't possible.\n\n\u003ca href=\"https://github.com/jsonrainbow/json-schema/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=jsonrainbow/json-schema\" /\u003e\n\u003c/a\u003e","funding_links":[],"categories":["Table of Contents","PHP"],"sub_categories":["Filtering and Validation","Filtering, Sanitizing and Validation"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonrainbow%2Fjson-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsonrainbow%2Fjson-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonrainbow%2Fjson-schema/lists"}