rust_json_schema 0.2.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +642 -849
- data/README.md +10 -8
- data/ext/rust_json_schema/Cargo.toml +2 -3
- data/ext/rust_json_schema/src/lib.rs +12 -42
- data/lib/rust_json_schema/version.rb +1 -1
- metadata +7 -10
data/README.md
CHANGED
@@ -4,6 +4,9 @@
|
|
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
|
+
> [!IMPORTANT]
|
8
|
+
> This gem is built with `json_schema` crate version `0.29.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.
|
9
|
+
|
7
10
|
The minimum Ruby version required by this gem is 3.0, due to the runtime Rust libraries that make the extensions possible (and easy).
|
8
11
|
|
9
12
|
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.
|
@@ -38,11 +41,7 @@ schema = <<~JSON
|
|
38
41
|
}
|
39
42
|
JSON
|
40
43
|
|
41
|
-
validator = RustJSONSchema::Validator.new(
|
42
|
-
schema,
|
43
|
-
draft: :draft7,
|
44
|
-
with_meta_schemas: false
|
45
|
-
)
|
44
|
+
validator = RustJSONSchema::Validator.new(schema, draft: :draft7)
|
46
45
|
|
47
46
|
errors = validator.validate('{ "foo": 1, "bar": "wadus" }')
|
48
47
|
# => [
|
@@ -54,8 +53,7 @@ errors = validator.validate('{ "foo": 1, "bar": "wadus" }')
|
|
54
53
|
|
55
54
|
### Options
|
56
55
|
|
57
|
-
- `: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.
|
58
|
-
- `:with_meta_schemas` - See [docs.rs/jsonschema CompilationOptions with_meta_schemas](https://docs.rs/jsonschema/0.17.1/jsonschema/struct.CompilationOptions.html#method.with_meta_schemas). `false` by default.
|
56
|
+
- `: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._
|
59
57
|
|
60
58
|
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.
|
61
59
|
|
@@ -70,7 +68,11 @@ Any additional options provided by the `jsonschema` crate are options I do not u
|
|
70
68
|
|
71
69
|
## Development
|
72
70
|
|
73
|
-
|
71
|
+
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`.
|
72
|
+
|
73
|
+
You can run the tests with `bin/rspec`, but ensure any changes to the rust extension are compiled first.
|
74
|
+
|
75
|
+
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.
|
74
76
|
|
75
77
|
## Contributing
|
76
78
|
|
@@ -10,8 +10,7 @@ publish = false
|
|
10
10
|
crate-type = ["cdylib"]
|
11
11
|
|
12
12
|
[dependencies]
|
13
|
-
jsonschema =
|
14
|
-
|
15
|
-
magnus = "0.6"
|
13
|
+
jsonschema = "0.29.1"
|
14
|
+
magnus = "0.7"
|
16
15
|
serde_json = "1.0"
|
17
16
|
rb-sys = { version = "*", default-features = false, features = ["ruby-static"] }
|
@@ -1,6 +1,6 @@
|
|
1
1
|
extern crate serde_json;
|
2
2
|
|
3
|
-
use jsonschema::
|
3
|
+
use jsonschema::Draft;
|
4
4
|
use magnus::{
|
5
5
|
exception::ExceptionClass,
|
6
6
|
function,
|
@@ -14,21 +14,16 @@ use magnus::{
|
|
14
14
|
|
15
15
|
#[wrap(class = "RustJSONSchema::Validator")]
|
16
16
|
struct Validator {
|
17
|
-
schema:
|
17
|
+
schema: jsonschema::Validator,
|
18
18
|
draft: Draft,
|
19
|
-
with_meta_schemas: bool,
|
20
19
|
}
|
21
20
|
|
22
21
|
impl Validator {
|
23
22
|
fn new(args: &[Value]) -> Result<Validator, Error> {
|
24
23
|
let args = scan_args::<_, (), (), (), _, ()>(args)?;
|
25
24
|
let (json,): (String,) = args.required;
|
26
|
-
let kwargs = get_kwargs::<_, (), (Option<Value>,
|
27
|
-
|
28
|
-
&[],
|
29
|
-
&["draft", "with_meta_schemas"],
|
30
|
-
)?;
|
31
|
-
let (draft_arg, with_meta_schemas_arg): (Option<Value>, Option<bool>) = kwargs.optional;
|
25
|
+
let kwargs = get_kwargs::<_, (), (Option<Value>,), ()>(args.keywords, &[], &["draft"])?;
|
26
|
+
let (draft_arg,): (Option<Value>,) = kwargs.optional;
|
32
27
|
|
33
28
|
let draft = match draft_arg {
|
34
29
|
Some(draft) => match draft.to_string().to_lowercase().as_str() {
|
@@ -47,11 +42,6 @@ impl Validator {
|
|
47
42
|
None => jsonschema::Draft::default(),
|
48
43
|
};
|
49
44
|
|
50
|
-
let with_meta_schemas = match with_meta_schemas_arg {
|
51
|
-
Some(with_meta_schemas) => with_meta_schemas,
|
52
|
-
None => false,
|
53
|
-
};
|
54
|
-
|
55
45
|
let value: serde_json::Value = match serde_json::from_str(&json) {
|
56
46
|
Ok(value) => value,
|
57
47
|
Err(error) => {
|
@@ -62,14 +52,7 @@ impl Validator {
|
|
62
52
|
}
|
63
53
|
};
|
64
54
|
|
65
|
-
let
|
66
|
-
schema.with_draft(draft);
|
67
|
-
|
68
|
-
if with_meta_schemas {
|
69
|
-
schema.with_meta_schemas();
|
70
|
-
}
|
71
|
-
|
72
|
-
let schema = match schema.compile(&value) {
|
55
|
+
let schema = match jsonschema::options().with_draft(draft).build(&value) {
|
73
56
|
Ok(schema) => schema,
|
74
57
|
Err(error) => {
|
75
58
|
return Err(Error::new(
|
@@ -79,11 +62,7 @@ impl Validator {
|
|
79
62
|
}
|
80
63
|
};
|
81
64
|
|
82
|
-
Ok(Validator {
|
83
|
-
schema,
|
84
|
-
draft,
|
85
|
-
with_meta_schemas,
|
86
|
-
})
|
65
|
+
Ok(Validator { schema, draft })
|
87
66
|
}
|
88
67
|
|
89
68
|
fn is_valid(&self, json: String) -> Result<bool, Error> {
|
@@ -110,13 +89,6 @@ impl Validator {
|
|
110
89
|
)
|
111
90
|
.unwrap();
|
112
91
|
|
113
|
-
options
|
114
|
-
.aset(
|
115
|
-
StaticSymbol::new("with_meta_schemas"),
|
116
|
-
self.with_meta_schemas,
|
117
|
-
)
|
118
|
-
.unwrap();
|
119
|
-
|
120
92
|
Ok(options)
|
121
93
|
}
|
122
94
|
|
@@ -133,15 +105,13 @@ impl Validator {
|
|
133
105
|
|
134
106
|
let mut errors: Vec<String> = vec![];
|
135
107
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
};
|
108
|
+
for error in self.schema.iter_errors(&value) {
|
109
|
+
let path = match format!("{}", error.instance_path).as_str() {
|
110
|
+
"" => "/".to_string(),
|
111
|
+
p => p.to_string(),
|
112
|
+
};
|
142
113
|
|
143
|
-
|
144
|
-
}
|
114
|
+
errors.push(format!("path \"{}\": {}", path, error));
|
145
115
|
}
|
146
116
|
|
147
117
|
Ok(errors)
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rust_json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taylor Thurlow
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: rake-compiler
|
@@ -59,14 +58,13 @@ files:
|
|
59
58
|
- lib/rust_json_schema.rb
|
60
59
|
- lib/rust_json_schema/version.rb
|
61
60
|
- sig/rust_json_schema.rbs
|
62
|
-
homepage: https://github.com/taylorthurlow/rust_json_schema
|
61
|
+
homepage: https://github.com/taylorthurlow/rust_json_schema-rb
|
63
62
|
licenses:
|
64
63
|
- MIT
|
65
64
|
metadata:
|
66
|
-
homepage_uri: https://github.com/taylorthurlow/rust_json_schema
|
67
|
-
source_code_uri: https://github.com/taylorthurlow/rust_json_schema
|
68
|
-
changelog_uri: https://github.com/taylorthurlow/rust_json_schema/releases
|
69
|
-
post_install_message:
|
65
|
+
homepage_uri: https://github.com/taylorthurlow/rust_json_schema-rb
|
66
|
+
source_code_uri: https://github.com/taylorthurlow/rust_json_schema-rb
|
67
|
+
changelog_uri: https://github.com/taylorthurlow/rust_json_schema-rb/releases
|
70
68
|
rdoc_options: []
|
71
69
|
require_paths:
|
72
70
|
- lib
|
@@ -81,8 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
79
|
- !ruby/object:Gem::Version
|
82
80
|
version: 3.3.11
|
83
81
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
85
|
-
signing_key:
|
82
|
+
rubygems_version: 3.6.8
|
86
83
|
specification_version: 4
|
87
84
|
summary: Ruby wrapper for jsonschema-rs
|
88
85
|
test_files: []
|