Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taylorthurlow/rust_json_schema-rb
Ruby wrapper for Rust's jsonschema-rs crate
https://github.com/taylorthurlow/rust_json_schema-rb
json json-schema ruby rust
Last synced: 3 months ago
JSON representation
Ruby wrapper for Rust's jsonschema-rs crate
- Host: GitHub
- URL: https://github.com/taylorthurlow/rust_json_schema-rb
- Owner: taylorthurlow
- License: mit
- Created: 2024-01-11T23:36:46.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-09-16T18:52:37.000Z (3 months ago)
- Last Synced: 2024-10-01T04:43:19.259Z (3 months ago)
- Topics: json, json-schema, ruby, rust
- Language: Ruby
- Homepage:
- Size: 807 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# `rust_json_schema`
[![Gem Version](https://badge.fury.io/rb/rust_json_schema.svg)](https://rubygems.org/gems/rust_json_schema)
`rust_json_schema` is a Ruby wrapper gem for Rust's [jsonschema-rs crate](https://github.com/Stranger6667/jsonschema-rs).
> [!IMPORTANT]
> This gem is built with `json_schema` crate version `0.19.1`, and therefore does not support any features for any potential future versions of the crate. I will review and accept PRs if you would like to work on adding support for newer versions of the crate. I generally am trying to keep things up to date but I am not cutting new releases for each new patch version of the crate.The minimum Ruby version required by this gem is 3.0, due to the runtime Rust libraries that make the extensions possible (and easy).
This gem ships with precompiled binaries for Linux and macOS. Check the available gems on [Rubygems](https://rubygems.org/gems/rust_json_schema). Precompiled binaries do not exist for non-standard rubies like JRuby or TruffleRuby, nor do they exist for Windows. I will review and accept PRs if you would like to work on adding these build targets.
[rusty_json_schema](https://github.com/driv3r/rusty_json_schema) is a direct source of inspiration. Now that [bundler has explicit support for rust-backed Ruby gems](https://bundler.io/blog/2023/01/31/rust-gem-skeleton.html) as of early 2023, the Rust library code is a lot simpler that it previously needed to be, largely thanks to [magnus crate](https://github.com/matsadler/magnus) and the [rb-sys gem](https://github.com/oxidize-rb/rb-sys/tree/main/gem), and by extension, the [oxidize-rb team](https://github.com/oxidize-rb).
## Warning
My experience with Rust is limited, but this gem does have tests, and it is deployed in production. Please confirm that the gem is working as expected before using it in any production-critical situation. If you are using this gem in a production environment, and have any comments or feedback, I would love to hear about it.
## Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add rust_json_schema
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install rust_json_schema
## Usage
```ruby
schema = <<~JSON
{
"properties": {
"foo": { "type": "string" },
"bar": { "type": "integer" },
"baz": {}
},
"required": ["foo", "baz"]
}
JSONvalidator = RustJSONSchema::Validator.new(schema, draft: :draft7)
errors = validator.validate('{ "foo": 1, "bar": "wadus" }')
# => [
# 'path "/bar": "wadus" is not of type "number"',
# 'path "/foo": 1 is not of type "string"',
# 'path "/": "baz" is a required property'
# ]
```### Options
- `:draft` - Select the JSON schema draft number to use. Valid options are `draft4`, `draft6`, `draft7`, `draft201909`, and `draft202012`. Supported drafts are entirely determined by the `jsonschema` crate. The default draft is also determined by the crate. If new versions of the crate support additional draft versions, a code change in this gem will be required. I'm open to PRs to solve this problem - I don't know enough Rust to tell if it's easily done. _Both `draft201909` and `draft202012` are reported to have "some keywords not implemented", so use them at your own risk._
Any additional options provided by the `jsonschema` crate are options I do not understand or may not make sense to implement in a wrapper library such as this.
`RustJSONSchema::Validator#options` is provided and will return a Hash containing configuration options from the underlying Rust library. While I make an effort for them to look similar, or identical, to the options passed into the `Validator` initializer, the initializer arguments and the returned Hash should not be considered one-to-one. It exists as a way to confirm the configuration of the underlying schema validator instance.
### Errors
- All errors are subclasses of `RustJSONSchema::Error`.
- Calling `RustJSONSchema::Validator#new`, `#validate` or `#valid?` with a string which is not valid JSON will raise `RustJSONSchema::JSONParseError`.
- Calling `RustJSONSchema::Validator#new` with an invalid schema will raise `RustJSONSchema::SchemaParseError`.
- Calling `RustJSONSchema::Validator#new` with an invalid draft version value will raise `RustJSONSchema::InvalidOptionsError`.## Development
The rust extension is located at `ext/rust_json_schema`, and can be compiled using the `rake-compiler` tasks, usually `bin/rake compile`. Don't attempt to `cargo build` the extension separately, that typically doesn't work. View other rake tasks with `bin/rake --tasks`.
You can run the tests with `bin/rspec`, but ensure any changes to the rust extension are compiled first.
To release a new version, update the version number in `version.rb`, tag the new commit with the version number, and push the tag to GitHub. The gem will be built and pushed to Rubygems automatically. This will also automatically build the platform-specific binaries and attach them to the release.
## Contributing
Bug reports and pull requests are welcome.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).