{"id":20553875,"url":"https://github.com/foxtacles/rj_schema","last_synced_at":"2025-04-14T11:56:20.848Z","repository":{"id":46763798,"uuid":"121954276","full_name":"foxtacles/rj_schema","owner":"foxtacles","description":"Fast JSON schema validation with RapidJSON in Ruby","archived":false,"fork":false,"pushed_at":"2024-02-26T16:42:45.000Z","size":1120,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-14T11:56:02.591Z","etag":null,"topics":["json-schema","rapidjson","ruby"],"latest_commit_sha":null,"homepage":"","language":"C++","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/foxtacles.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":"2018-02-18T13:27:19.000Z","updated_at":"2024-04-07T20:09:44.000Z","dependencies_parsed_at":"2022-09-10T17:02:22.443Z","dependency_job_id":null,"html_url":"https://github.com/foxtacles/rj_schema","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxtacles%2Frj_schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxtacles%2Frj_schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxtacles%2Frj_schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxtacles%2Frj_schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foxtacles","download_url":"https://codeload.github.com/foxtacles/rj_schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248878020,"owners_count":21176242,"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","rapidjson","ruby"],"created_at":"2024-11-16T02:45:31.024Z","updated_at":"2025-04-14T11:56:20.818Z","avatar_url":"https://github.com/foxtacles.png","language":"C++","readme":"# rj_schema ![Ruby](https://github.com/foxtacles/rj_schema/workflows/Ruby/badge.svg) [![Gem Version](https://badge.fury.io/rb/rj_schema.svg)](https://badge.fury.io/rb/rj_schema)\nFast JSON schema validation with RapidJSON (https://github.com/Tencent/rapidjson)\n# Usage\n```\nrequire 'rj_schema'\n```\nCreate an instance of `RjSchema::Validator` and provide a JSON schema and a JSON document. If you pass a `File`, it will be read and parsed as JSON. Otherwise, `to_json` will be called on the arguments internally:\n```\nRjSchema::Validator.new.validate(File.new(\"schema/my_schema.json\"), '{\"stuff\": 1}')\n```\nIt is possible to resolve remote schemas by specifying them in the initializer. For example, if your schema contains `\"$ref\": \"/path/to/generic#/definitions/something\"`:\n```\nRjSchema::Validator.new(\n  '/path/to/generic' =\u003e File.new(\"definitions/generic.json\")\n).validate(File.new(\"schema/my_schema.json\"), '{\"stuff\": 1}')\n```\n`validate` will return a hash containing various descriptions of the errors (for details, see Options below). An `ArgumentError` exception will be raised if any of the inputs are malformed or missing.\n\nYou can also call `valid?`, which returns a boolean value indicating success/failure instead.\n\n## SAX parser validation \n\nIf you prefer SAX validation with a simple boolean value, which also dramatically reduces memory requirements, then you can also call `sax_valid?` and pass the filepath instead of the file reference for the document you want to validate, eg:\n\n```\nRjSchema::Validator.new(\n  '/path/to/generic' =\u003e File.new(\"definitions/generic.json\")\n).sax_valid?(File.new(\"schema/my_schema.json\"), '\u003c\u003cFILEPATH to Doc\u003e\u003e')\n```\n\n# Options\n\n`validate` currently offers three options to customize the validation process. They can be specified as keyword arguments:\n\n```\nRjSchema::Validator.new.validate(\n  File.new(\"schema/my_schema.json\"),\n  '{\"stuff\": 1}', \n  continue_on_error: true, \n  machine_errors: false, \n  human_errors: true\n)\n```\n\n### `continue_on_error` (default: `false`). \n\nWhen set to `true`, validation will not stop upon the first error. Instead, an attempt will be made to determine all errors in the document based on the provided schema.\n\n### `machine_errors` (default: `true`). \n\nWhen set to `true`, the return value of `validate` will contain a symbol key called `machine_errors`, which is a structured hash describing the encountered errors. The hash will be empty if no errors were found. The documentation for the error codes can be [found here](https://github.com/Tencent/rapidjson/blob/05e7b3397758bd31032aa66620e15fd8ab2869f5/include/rapidjson/error/error.h#L162). Example:\n\n`{:machine_errors=\u003e{\"maximum\"=\u003e{\"actual\"=\u003e31, \"expected\"=\u003e20, \"errorCode\"=\u003e2, \"instanceRef\"=\u003e\"#/aaaa\", \"schemaRef\"=\u003e\"#/patternProperties/aaa%2A\"}}}`\n\n### `human_errors` (default: `false`). \n\nWhen set to `true`, the return value of `validate` will contain a symbol key called `human_errors`, which is a printable and human readable string describing the encountered errors. The string will be empty if no errors were found. Example:\n\n`{:human_errors=\u003e\"Error Name: maximum\\nMessage: Number '31' is greater than the 'maximum' value '20'.\\nInstance: #/aaaa\\nSchema: #/patternProperties/aaa%2A\\n\\n\"}`\n\n# Caching\nAnother feature of `rj_schema` is the ability to preload schemas. This can boost performance by a lot, especially in applications that routinely perform validations against static schemas, i.e. validations of client inputs inside the endpoints of a web app. Add the schemas to preload into the initializer and pass a `Symbol` to the validation function:\n```\nRjSchema::Validator.new(\n  '/path/to/generic' =\u003e File.new(\"definitions/generic.json\"),\n  '/schema/my_schema.json' =\u003e File.new(\"schema/my_schema.json\")\n).validate(:\"/schema/my_schema.json\", '{\"stuff\": 1}')\n```\n# Limitations\n\nSome limitations apply due to RapidJSON:\n\n- only JSON schema draft-04 is supported\n- the `format` keyword is not supported\n\n# Benchmark\nThe main motivation for this gem was that we needed a faster JSON schema validation for our Ruby apps. We have been using Ruby JSON Schema Validator for a while (https://github.com/ruby-json-schema/json-schema) but some of our endpoints became unacceptably slow.\n\nA benchmark to compare various gem performances can be run with: `rake benchmark`. These are the results collected on my machine (with `g++ (Debian 7.3.0-3) 7.3.0`, `ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]`).\n\nreport | i/s | x\n--- | --- | ---\nrj_schema (valid?) (cached) | 370.9 | 1\nrj_schema (validate) (cached) | 187.5 | 1.98x slower\n[json_schemer](https://github.com/davishmcclurg/json_schemer) (valid?) (cached) | 135.6 | 2.73x slower\nrj_schema (valid?) | 132.7 | 2.79x slower\nrj_schema (validate) | 96.9 | 3.83x slower\n[json-schema](https://github.com/ruby-json-schema/json-schema) | 10.6 | 34.92x slower\n[json_schema](https://github.com/brandur/json_schema) | 3.7 | 101.26x slower\n\nThe error reporting of `rj_schema` is implemented inefficiently at the time of writing, so in this benchmark environment (based on JSON Schema test suite which includes many failing validations) `validate` performs significantly worse than `valid?`. This may not be an issue in production environments though, where failing validations are usually the exception (the overhead is only incurred in case of an error).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoxtacles%2Frj_schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoxtacles%2Frj_schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoxtacles%2Frj_schema/lists"}