{"id":15022484,"url":"https://github.com/sgpinkus/jsonschema","last_synced_at":"2025-04-04T06:44:17.114Z","repository":{"id":35506948,"uuid":"39776944","full_name":"sgpinkus/JsonSchema","owner":"sgpinkus","description":"A PHP Json Schema validator.","archived":false,"fork":false,"pushed_at":"2022-10-08T01:28:42.000Z","size":260,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T06:44:13.511Z","etag":null,"topics":["json-schema","php","validation"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sgpinkus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-27T13:50:34.000Z","updated_at":"2025-01-10T13:22:28.000Z","dependencies_parsed_at":"2022-09-17T19:11:41.539Z","dependency_job_id":null,"html_url":"https://github.com/sgpinkus/JsonSchema","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgpinkus%2FJsonSchema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgpinkus%2FJsonSchema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgpinkus%2FJsonSchema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgpinkus%2FJsonSchema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sgpinkus","download_url":"https://codeload.github.com/sgpinkus/JsonSchema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135124,"owners_count":20889420,"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","php","validation"],"created_at":"2024-09-24T19:58:01.011Z","updated_at":"2025-04-04T06:44:17.085Z","avatar_url":"https://github.com/sgpinkus.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Overview [![Build Status](https://api.travis-ci.org/sgpinkus/JsonSchema.png)](https://travis-ci.org/sgpinkus/JsonSchema)\nDraft v6 compliant JSON Schema validator for PHP:\n\n  * Modular design.\n  * Simple interface for validation.\n  * JsonRef dereferencing is handled by an external PHP library [JsonRef](http://jsonref.org). You can easily replace it with a different one.\n  * Easily extensible with custom constraints.\n  * Draft v4 compatible.\n\n# Installation\n\n    composer install sgpinkus/jsonschema\n\n# Test\n\n    git clone ... \u0026\u0026 cd JsonSchema\n    git submodule update --init\n    composer test\n\n# Usage\nIn the simplest case, where you have a standalone JSON schema with no `$refs`:\n\n```php\n\u003c?php\nrequire_once './vendor/autoload.php';\nuse JsonSchema\\JsonSchema;\nuse JsonRef\\JsonDocs;\n\n$json = '{\n  \"users\": [\n    {\n     \"comment\": \"valid\",\n     \"firstName\": \"John\",\n     \"lastName\": \"Doe\",\n     \"email\": \"john.doe@nowhere.com\",\n     \"_id\": 1\n    },\n    {\n     \"comment\": \"invalid\",\n     \"firstName\": \"John\",\n     \"lastName\": \"Doe\",\n     \"email\": \"john.doe.nowhere.com\",\n     \"_id\": 2\n    }\n  ]\n}';\n$schema = '{\n  \"type\": \"object\",\n  \"properties\": {\n    \"firstName\": { \"type\": \"string\", \"minLength\": 2 },\n    \"lastName\": { \"type\": \"string\", \"minLength\": 2 },\n    \"email\": { \"type\": \"string\", \"format\": \"email\" },\n    \"_id\": { \"type\": \"integer\" }\n  },\n  \"required\": [\"firstName\", \"lastName\", \"email\", \"_id\"]\n}';\n\n$schema = new JsonSchema($schema);\n$doc = json_decode($json);\n\n// Validate whole doc.\n$valid = $schema-\u003evalidate($doc);\nif($valid === true)\n  print \"OK\\n\";\nelse\n  print $valid;\n\n// Addendum: use JsonDocs::getPointer() to get a sub doc then validate it.\n$valid = $schema-\u003evalidate(JsonDocs::getPointer($doc, '/users/1'));\nif($valid === true)\n  print \"OK\\n\";\nelse\n  print \"$valid\";\n```\n\nIf you have any `$refs` in your JSON schema, you need to use the `JsonRef` wrapper class to load and dereference the JSON schema documents:\n\n```php\n\u003c?php\nrequire_once './vendor/autoload.php';\nuse JsonRef\\JsonDocs;\nuse JsonSchema\\JsonSchema;\n\n$json = '{\n  \"comment\": \"valid\",\n  \"firstName\": \"John\",\n  \"lastName\": \"Doe\",\n  \"email\": \"john.doe@nowhere.com\",\n  \"_id\": 1\n}';\n$schema = '{\n  \"id\": \"file:///tmp/jsonschema/user\",\n  \"type\": \"object\",\n  \"definitions\" : {\n    \"_id\" : { \"type\": \"integer\", \"minimum\": 0, \"exclusiveMinimum\": true },\n    \"commonName\" : { \"type\": \"string\", \"minLength\": 2 }\n  },\n  \"properties\": {\n    \"firstName\": { \"$ref\": \"#/definitions/commonName\" },\n    \"lastName\": { \"$ref\": \"#/definitions/commonName\" },\n    \"email\": { \"type\": \"string\", \"format\": \"email\" },\n    \"_id\": { \"$ref\": \"#/definitions/_id\" }\n  },\n  \"required\": [\"firstName\", \"lastName\", \"email\", \"_id\"]\n}';\n\n// JsonDocs does the dereferencing, and also caches any loaded JSON docs. Without a loader it wont\n// try to loader external resources though.\n$jsonDocs = new JsonDocs();\n$schema = new JsonSchema($jsonDocs-\u003eloadDocStr($schema, 'file:///tmp/some-unique-fake-uri'));\n$valid = $schema-\u003evalidate(json_decode($json));\nif($valid === true)\n  print \"OK\\n\";\nelse\n  print $valid;\n```\n\nTo implement custom constraints extend the `Constraint` class and implement abstract methods, then\nregister the constraint when creating the `JsonSchema` instance:\n\n```php\n\u003c?php\nrequire_once './vendor/autoload.php';\nuse JsonSchema\\JsonSchema;\nuse JsonSchema\\Constraint\\Constraint;\nuse JsonSchema\\Constraint\\Exception\\ConstraintParseException;\nuse JsonSchema\\Constraint\\ValidationError;\n\nclass ModuloConstraint extends Constraint\n{\n  private $modulo;\n\n  private function __construct(int $modulo) {\n    $this-\u003emodulo = $modulo;\n  }\n\n  public static function getName() {\n    return 'modulo';\n  }\n\n  public function validate($doc, $context) {\n    if(is_int($doc) \u0026\u0026 $doc % $this-\u003emodulo !== 0) {\n      return new ValidationError($this, \"$doc is not modulo {$this-\u003emodulo}\", $context);\n    }\n    return true;\n  }\n\n  public static function build($context) {\n    if(!is_int($context-\u003emodulo)) {\n      throw new ConstraintParseException(\"The value of 'modulo' MUST be an integer.\");\n    }\n\n    return new static($context-\u003emodulo);\n  }\n}\n\n$doc = 7;\n$schema = '{\n  \"type\": \"integer\",\n  \"modulo\": 2\n}';\n$schema = new JsonSchema($schema, ['ModuloConstraint']);\n$valid = $schema-\u003evalidate($doc);\nif($valid === true)\n  print \"OK\\n\";\nelse\n  print $valid;\n```\n\nAlso see [cli-validator.php](cli-validator.php) for example code.\n\n# TODO\nSee [TODO](TODO.md).\n\n# CONFORMANCE\nSee [conformance notes](CONFORMANCE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgpinkus%2Fjsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsgpinkus%2Fjsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgpinkus%2Fjsonschema/lists"}