rust_json_schema 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Cargo.lock +1 -0
- data/README.md +44 -12
- data/Rakefile +7 -1
- data/ext/rust_json_schema/Cargo.toml +1 -0
- data/ext/rust_json_schema/src/lib.rs +34 -9
- data/lib/rust_json_schema/version.rb +1 -1
- data/lib/rust_json_schema.rb +9 -1
- metadata +32 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29e46fa7734afd93281656bfbd1380e07ac6a9d810e619ed0091967f74dc2394
|
4
|
+
data.tar.gz: 806994df60e5857e1c5d29d672a9098e22c4d29dd17d59dde52d2ca54c499858
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fca158592848060388e817a9b350b222f422df3ef7eb01f9b22785429889ff8b8d1f7770bcc68589030c7e02aecd0266b3f0cae2de391da853f83a007c8105d8
|
7
|
+
data.tar.gz: 00c3f526a81a7c837e195d1b81ded85e3cf6ade49bf2aedaa591b67636aa1ee1f72cf2ce393ad81dc8b226fef20a9a08ba48bd04da596713e18b1b89832686e7
|
data/Cargo.lock
CHANGED
data/README.md
CHANGED
@@ -1,34 +1,66 @@
|
|
1
|
-
#
|
1
|
+
# `rust_json_schema`
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/rust_json_schema.svg)](https://rubygems.org/gems/rust_json_schema)
|
4
4
|
|
5
|
-
|
5
|
+
`rust_json_schema` is a Ruby wrapper gem for Rust's [jsonschema-rs crate](https://github.com/Stranger6667/jsonschema-rs).
|
6
6
|
|
7
|
-
|
7
|
+
This gem ships with precompiled binaries for Linux and macOS. Check the available gems on [Rubygems](https://rubygems.org/gems/rust_json_schema).
|
8
|
+
|
9
|
+
## Warning
|
10
|
+
|
11
|
+
I do not have any significant Rust programming experience, but this gem satisifies a need for a performant JSON Schema validation tool in Ruby land. While I intend to use this gem in a production environment, consider this code and library entirely experimental, at least until a 1.0 release, if it ever comes to that.
|
8
12
|
|
9
|
-
|
13
|
+
[rusty_json_schema](https://github.com/driv3r/rusty_json_schema) is a direct source of inspiration (and in some cases, literal copy and paste, like some fixtures/specs). 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).
|
14
|
+
|
15
|
+
## Installation
|
10
16
|
|
11
17
|
Install the gem and add to the application's Gemfile by executing:
|
12
18
|
|
13
|
-
$ bundle add
|
19
|
+
$ bundle add rust_json_schema
|
14
20
|
|
15
21
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
22
|
|
17
|
-
$ gem install
|
23
|
+
$ gem install rust_json_schema
|
18
24
|
|
19
25
|
## Usage
|
20
26
|
|
21
|
-
|
27
|
+
```ruby
|
28
|
+
validator = RustJSONSchema::Validator.new(<<~JSON)
|
29
|
+
{
|
30
|
+
"properties": {
|
31
|
+
"foo": { "type": "string" },
|
32
|
+
"bar": { "type": "integer" },
|
33
|
+
"baz": {}
|
34
|
+
},
|
35
|
+
"required": ["foo", "baz"]
|
36
|
+
}
|
37
|
+
JSON
|
22
38
|
|
23
|
-
|
39
|
+
errors = validator.validate('{ "foo": 1, "bar": "wadus" }')
|
40
|
+
# => [
|
41
|
+
# 'path "/bar": "wadus" is not of type "number"',
|
42
|
+
# 'path "/foo": 1 is not of type "string"',
|
43
|
+
# 'path "/": "baz" is a required property'
|
44
|
+
# ]
|
45
|
+
```
|
24
46
|
|
25
|
-
|
47
|
+
### Errors
|
48
|
+
|
49
|
+
- All errors are subclasses of `RustJSONSchema::Error`.
|
50
|
+
- Calling `RustJSONSchema::Validator#new`, `#validate` or `#valid?` with a string which is not valid JSON will raise `RustJSONSchema::JSONParseError`.
|
51
|
+
- Calling `RustJSONSchema::Validator#new` with an invalid schema will raise `RustJSONSchema::SchemaParseError`.
|
52
|
+
|
53
|
+
## TODO
|
54
|
+
|
55
|
+
- Support passing options as `jsonschema-rs` does
|
56
|
+
|
57
|
+
## Development
|
26
58
|
|
27
|
-
|
59
|
+
TODO
|
28
60
|
|
29
61
|
## Contributing
|
30
62
|
|
31
|
-
Bug reports and pull requests are welcome
|
63
|
+
Bug reports and pull requests are welcome.
|
32
64
|
|
33
65
|
## License
|
34
66
|
|
data/Rakefile
CHANGED
@@ -11,8 +11,14 @@ require "rb_sys/extensiontask"
|
|
11
11
|
|
12
12
|
task build: :compile
|
13
13
|
|
14
|
-
|
14
|
+
rubies = ["3.3.0", "3.2.0", "3.1.0", "3.0.0"]
|
15
|
+
ENV["RUBY_CC_VERSION"] ||= rubies.join(":")
|
16
|
+
|
17
|
+
spec = Bundler::GemHelper.gemspec
|
18
|
+
RbSys::ExtensionTask.new("rust_json_schema", spec) do |ext|
|
15
19
|
ext.lib_dir = "lib/rust_json_schema"
|
20
|
+
|
21
|
+
ext.cross_compile = true
|
16
22
|
end
|
17
23
|
|
18
24
|
task default: %i[compile spec standard]
|
@@ -12,12 +12,12 @@ struct Validator {
|
|
12
12
|
}
|
13
13
|
|
14
14
|
impl Validator {
|
15
|
-
fn new(
|
15
|
+
fn new(json: String) -> Result<Validator, magnus::Error> {
|
16
16
|
let value: serde_json::Value = match serde_json::from_str(&json) {
|
17
17
|
Ok(value) => value,
|
18
18
|
Err(error) => {
|
19
19
|
return Err(magnus::Error::new(
|
20
|
-
ruby.get_inner(&JSON_PARSE_ERROR),
|
20
|
+
Self::ruby().get_inner(&JSON_PARSE_ERROR),
|
21
21
|
error.to_string(),
|
22
22
|
))
|
23
23
|
}
|
@@ -27,7 +27,7 @@ impl Validator {
|
|
27
27
|
Ok(schema) => schema,
|
28
28
|
Err(error) => {
|
29
29
|
return Err(magnus::Error::new(
|
30
|
-
ruby.get_inner(&SCHEMA_PARSE_ERROR),
|
30
|
+
Self::ruby().get_inner(&SCHEMA_PARSE_ERROR),
|
31
31
|
error.to_string(),
|
32
32
|
))
|
33
33
|
}
|
@@ -36,14 +36,31 @@ impl Validator {
|
|
36
36
|
Ok(Validator { schema })
|
37
37
|
}
|
38
38
|
|
39
|
-
fn is_valid(&self, json: String) -> bool {
|
40
|
-
let value: serde_json::Value = serde_json::from_str(&json)
|
39
|
+
fn is_valid(&self, json: String) -> Result<bool, magnus::Error> {
|
40
|
+
let value: serde_json::Value = match serde_json::from_str(&json) {
|
41
|
+
Ok(value) => value,
|
42
|
+
Err(error) => {
|
43
|
+
return Err(magnus::Error::new(
|
44
|
+
Self::ruby().get_inner(&JSON_PARSE_ERROR),
|
45
|
+
error.to_string(),
|
46
|
+
));
|
47
|
+
}
|
48
|
+
};
|
41
49
|
|
42
|
-
self.schema.is_valid(&value)
|
50
|
+
Ok(self.schema.is_valid(&value))
|
43
51
|
}
|
44
52
|
|
45
|
-
fn validate(&self, json: String) -> Vec<String> {
|
46
|
-
let value: serde_json::Value = serde_json::from_str(&json)
|
53
|
+
fn validate(&self, json: String) -> Result<Vec<String>, magnus::Error> {
|
54
|
+
let value: serde_json::Value = match serde_json::from_str(&json) {
|
55
|
+
Ok(value) => value,
|
56
|
+
Err(error) => {
|
57
|
+
return Err(magnus::Error::new(
|
58
|
+
Self::ruby().get_inner(&JSON_PARSE_ERROR),
|
59
|
+
error.to_string(),
|
60
|
+
))
|
61
|
+
}
|
62
|
+
};
|
63
|
+
|
47
64
|
let mut errors: Vec<String> = vec![];
|
48
65
|
|
49
66
|
if let Err(validation_errors) = self.schema.validate(&value) {
|
@@ -57,7 +74,11 @@ impl Validator {
|
|
57
74
|
}
|
58
75
|
}
|
59
76
|
|
60
|
-
errors
|
77
|
+
Ok(errors)
|
78
|
+
}
|
79
|
+
|
80
|
+
fn ruby() -> Ruby {
|
81
|
+
Ruby::get().unwrap()
|
61
82
|
}
|
62
83
|
}
|
63
84
|
|
@@ -97,5 +118,9 @@ fn init(ruby: &Ruby) -> Result<(), magnus::Error> {
|
|
97
118
|
class.define_method("valid?", method!(Validator::is_valid, 1))?;
|
98
119
|
class.define_method("validate", method!(Validator::validate, 1))?;
|
99
120
|
|
121
|
+
// Ensure defined at load time
|
122
|
+
Lazy::force(&JSON_PARSE_ERROR, ruby);
|
123
|
+
Lazy::force(&SCHEMA_PARSE_ERROR, ruby);
|
124
|
+
|
100
125
|
Ok(())
|
101
126
|
}
|
data/lib/rust_json_schema.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "rust_json_schema/version"
|
4
|
-
require_relative "rust_json_schema/rust_json_schema"
|
5
4
|
|
6
5
|
module RustJSONSchema
|
7
6
|
class Error < StandardError; end
|
8
7
|
|
9
8
|
class JSONParseError < Error; end
|
9
|
+
|
10
10
|
class SchemaParseError < Error; end
|
11
11
|
end
|
12
|
+
|
13
|
+
# Tries to require the extension for the given Ruby version first
|
14
|
+
begin
|
15
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
16
|
+
require "rust_json_schema/#{Regexp.last_match(1)}/rust_json_schema"
|
17
|
+
rescue LoadError
|
18
|
+
require_relative "rust_json_schema/rust_json_schema"
|
19
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rust_json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taylor Thurlow
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
12
|
-
dependencies:
|
11
|
+
date: 2024-01-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake-compiler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake-compiler-dock
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
description: Ruby wrapper for jsonschema-rs
|
14
42
|
email:
|
15
43
|
- thurlow@hey.com
|
@@ -53,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
81
|
- !ruby/object:Gem::Version
|
54
82
|
version: 3.3.11
|
55
83
|
requirements: []
|
56
|
-
rubygems_version: 3.4
|
84
|
+
rubygems_version: 3.5.4
|
57
85
|
signing_key:
|
58
86
|
specification_version: 4
|
59
87
|
summary: Ruby wrapper for jsonschema-rs
|