{"id":17087835,"url":"https://github.com/geraintluff/jsv4-php","last_synced_at":"2025-04-06T02:10:43.360Z","repository":{"id":9442628,"uuid":"11319410","full_name":"geraintluff/jsv4-php","owner":"geraintluff","description":"A (coercive) JSON Schema v4 Validator for PHP","archived":false,"fork":false,"pushed_at":"2017-03-27T13:21:56.000Z","size":44,"stargazers_count":115,"open_issues_count":17,"forks_count":32,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-30T01:11:15.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/geraintluff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-10T17:51:39.000Z","updated_at":"2025-01-07T14:40:43.000Z","dependencies_parsed_at":"2022-09-07T22:11:31.031Z","dependency_job_id":null,"html_url":"https://github.com/geraintluff/jsv4-php","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fjsv4-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fjsv4-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fjsv4-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fjsv4-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/geraintluff","download_url":"https://codeload.github.com/geraintluff/jsv4-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423515,"owners_count":20936626,"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":[],"created_at":"2024-10-14T13:35:05.873Z","updated_at":"2025-04-06T02:10:43.322Z","avatar_url":"https://github.com/geraintluff.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsv4-php\n\n## A (coercive) JSON Schema v4 Validator for PHP\n\n`jsv4-php` is a data validator, using version 4 JSON Schemas.\n\nJust include `jsv4.php` from your code, and use the static methods on the `Jsv4` class it defines.\n\nUsage:\n\n### `Jsv4::validate($data, $schema)`\n\nThis usage returns an object of the following shape.\n```json\n{\n    \"valid\": true/false,\n    \"errors\": [\n        {...}\n    ]\n}\n```\n\nThe values in the `errors` array are similar to those for [tv4](https://github.com/geraintluff/tv4) (a similar project):\n\n```json\n{\n    \"code\": 0,\n    \"message\": \"Invalid type: string\",\n    \"dataPath\": \"/intKey\",\n    \"schemaKey\": \"/properties/intKey/type\"\n}\n```\n\nThe `code` property corresponds to a constant corresponding to the nature of the validation error, e.g. `JSV4_INVALID_TYPE`.  The names of these constants (and their values) match up exactly with the [constants from tv4](https://github.com/geraintluff/tv4/blob/master/source/api.js).\n\n### `Jsv4::isValid($data, $schema)`\n\nIf you just want to know the validation status, and don't care what the errors actually are, then this is a more concise way of getting it.\n\nIt returns a boolean indicating whether the data correctly followed the schema.\n\n### `Jsv4::coerce($data, $schema)`\n\nSometimes, the data is not quite the correct shape - but it could be *made* the correct shape by simple modifications.\n\nIf you call `Jsv4::coerce($data, $schema)`, then it will attempt to change the data.\n\nIf it is successful, then a modified version of the data can be found in `$result-\u003evalue`.\n\nIt's not psychic - in fact, it's quite limited.  What it currently does is:\n\n### Type-coercion for scalar types\n\nPerhaps you are using data from `$_GET`, so everything's a string, but the schema says certain values should be integers or booleans.\n\n`Jsv4::coerce()` will attempt to convert strings to numbers/booleans *only where the schema says*, leaving other numerically-value strings as strings.\n\n### Missing properties\n\nPerhaps the API needs a complete object (described using `\"required\"` in the schema), but only a partial one was supplied.\n\n`Jsv4::coerce()` will attempt to insert appropriate values for the missing properties, using a default (if it is defined in a nearby `\"properties\"` entry) or by creating a value if it knows the type.\n\n## The `SchemaStore` class\n\nThis class represents a collection of schemas.  You include it from `schema-store.php`, and use it like this:\n```php\n$store = new SchemaStore();\n$store-\u003eadd($url, $schema);\n$retrieved = $store-\u003eget($url);\n```\n\nIt can handle:\n\n* Fragments in URLs, using both JSON Pointer fragments and identification using `\"id\"`\n* Converts URIs in `\"id\"` and `\"$ref\"` to absolute (where possible)\n* Resolves `\"$ref\"`s, splicing the resulting value into the schema\n* Converts associative PHP arrays to objects - you can express your schema natively, but what you retrieve from the store is always an object.\n* Adds sub-schemas according to their `\"id\"` - by default, this only happens if `\"id\"` is a sub-path of the current schema URL.\n\nIf the schemas being added are \"trusted\", then an extra argument can be supplied: `$store-\u003eadd($url, $schema, TRUE)`.  In that case, the value of `\"id\"` is *always* believed for all sub-schemas.\n\nA list of \"missing\" schemas (unresolved `\"$ref\"`s) can be retrieved used `$store-\u003emissing()`.\n\nThis class does not depend on `jsv4.php` at all - it just deals with raw schema objects.  As such, it could (hopefully) be used with other validators with minimal fuss.\n\n## Tests\n\nThe tests can be run using `test.php` (from the command-line or via the web).\n\n## License\n\nThis code is released under a do-anything-you-like \"public domain\" license (see `LICENSE.txt`).\n\nIt is also released under an MIT-style license (see `LICENSE-MIT.txt`) because there is sometimes benefit in having a recognised open-source license.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeraintluff%2Fjsv4-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeraintluff%2Fjsv4-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeraintluff%2Fjsv4-php/lists"}