rust_json_schema 0.3.0 → 0.5.2

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.
data/README.md CHANGED
@@ -5,7 +5,7 @@
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
  > [!IMPORTANT]
8
- > 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.
8
+ > This gem is built with `json_schema` crate version `0.32.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 am trying to keep things up to date occasionally, but I am not regularly cutting new releases for each new version of the crate.
9
9
 
10
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).
11
11
 
@@ -55,7 +55,9 @@ errors = validator.validate('{ "foo": 1, "bar": "wadus" }')
55
55
 
56
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._
57
57
 
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.
58
+ - `:with_base_uri` - Sets a base URI to use when resolving relative schema references during validation. Relative URIs within the schema will be evaluated relative to this base URI. This is especially useful when validating schemas loaded from sources without an inherent base URL.
59
+
60
+ Any additional options provided by the `jsonschema` crate are either options I do not understand as a whole, have not figured out how to (or tried to) implement, or may not make sense to implement in a wrapper library such as this.
59
61
 
60
62
  `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.
61
63
 
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ require "rb_sys/extensiontask"
11
11
 
12
12
  task build: :compile
13
13
 
14
- rubies = ["3.3.0", "3.2.0", "3.1.0", "3.0.0"]
14
+ rubies = ["4.0.0", "3.4.0", "3.3.0", "3.2.0", "3.1.0", "3.0.0"]
15
15
  ENV["RUBY_CC_VERSION"] ||= rubies.join(":")
16
16
 
17
17
  spec = Bundler::GemHelper.gemspec
@@ -10,7 +10,7 @@ publish = false
10
10
  crate-type = ["cdylib"]
11
11
 
12
12
  [dependencies]
13
- jsonschema = "0.19"
14
- magnus = "0.7"
13
+ jsonschema = "0.32.1"
14
+ magnus = "0.8"
15
15
  serde_json = "1.0"
16
- rb-sys = { version = "*", default-features = false, features = ["ruby-static"] }
16
+ rb-sys = { version = "*", default-features = false }
@@ -1,6 +1,6 @@
1
1
  extern crate serde_json;
2
2
 
3
- use jsonschema::{Draft, JSONSchema};
3
+ use jsonschema::Draft;
4
4
  use magnus::{
5
5
  exception::ExceptionClass,
6
6
  function,
@@ -9,21 +9,37 @@ use magnus::{
9
9
  prelude::*,
10
10
  scan_args::{get_kwargs, scan_args},
11
11
  value::Lazy,
12
- wrap, Error, RHash, RModule, Ruby, StaticSymbol, Value,
12
+ wrap, Error, RHash, RModule, Ruby, Value,
13
13
  };
14
14
 
15
15
  #[wrap(class = "RustJSONSchema::Validator")]
16
16
  struct Validator {
17
- schema: JSONSchema,
17
+ schema: jsonschema::Validator,
18
18
  draft: Draft,
19
+ base_uri: Option<String>,
19
20
  }
20
21
 
21
22
  impl Validator {
22
23
  fn new(args: &[Value]) -> Result<Validator, Error> {
23
24
  let args = scan_args::<_, (), (), (), _, ()>(args)?;
24
25
  let (json,): (String,) = args.required;
25
- let kwargs = get_kwargs::<_, (), (Option<Value>,), ()>(args.keywords, &[], &["draft"])?;
26
- let (draft_arg,): (Option<Value>,) = kwargs.optional;
26
+
27
+ let value: serde_json::Value = match serde_json::from_str(&json) {
28
+ Ok(value) => value,
29
+ Err(error) => {
30
+ return Err(Error::new(
31
+ Self::ruby().get_inner(&JSON_PARSE_ERROR),
32
+ error.to_string(),
33
+ ))
34
+ }
35
+ };
36
+
37
+ let kwargs = get_kwargs::<_, (), (Option<Value>, Option<Value>), ()>(
38
+ args.keywords,
39
+ &[],
40
+ &["draft", "with_base_uri"],
41
+ )?;
42
+ let (draft_arg, with_base_uri_arg): (Option<Value>, Option<Value>) = kwargs.optional;
27
43
 
28
44
  let draft = match draft_arg {
29
45
  Some(draft) => match draft.to_string().to_lowercase().as_str() {
@@ -42,20 +58,19 @@ impl Validator {
42
58
  None => jsonschema::Draft::default(),
43
59
  };
44
60
 
45
- let value: serde_json::Value = match serde_json::from_str(&json) {
46
- Ok(value) => value,
47
- Err(error) => {
48
- return Err(Error::new(
49
- Self::ruby().get_inner(&JSON_PARSE_ERROR),
50
- error.to_string(),
51
- ))
52
- }
61
+ let options = jsonschema::options().with_draft(draft);
62
+
63
+ let base_uri = match with_base_uri_arg {
64
+ Some(uri) if !uri.is_nil() => Some(uri.to_string()),
65
+ _ => None,
53
66
  };
54
67
 
55
- let mut schema = JSONSchema::options();
56
- schema.with_draft(draft);
68
+ let options = match base_uri {
69
+ Some(ref uri) => options.with_base_uri(uri),
70
+ None => options,
71
+ };
57
72
 
58
- let schema = match schema.compile(&value) {
73
+ let schema = match options.build(&value) {
59
74
  Ok(schema) => schema,
60
75
  Err(error) => {
61
76
  return Err(Error::new(
@@ -65,7 +80,11 @@ impl Validator {
65
80
  }
66
81
  };
67
82
 
68
- Ok(Validator { schema, draft })
83
+ Ok(Validator {
84
+ schema,
85
+ draft,
86
+ base_uri,
87
+ })
69
88
  }
70
89
 
71
90
  fn is_valid(&self, json: String) -> Result<bool, Error> {
@@ -83,16 +102,19 @@ impl Validator {
83
102
  }
84
103
 
85
104
  fn options(&self) -> Result<RHash, Error> {
86
- let options = RHash::new();
105
+ let ruby = Self::ruby();
106
+ let result = ruby.hash_new();
87
107
 
88
- options
89
- .aset(
90
- StaticSymbol::new("draft"),
91
- StaticSymbol::new(format!("{:?}", self.draft).to_lowercase()),
92
- )
93
- .unwrap();
108
+ result.aset(
109
+ ruby.sym_new("draft"),
110
+ ruby.sym_new(format!("{:?}", self.draft).to_lowercase()),
111
+ )?;
94
112
 
95
- Ok(options)
113
+ if let Some(uri) = &self.base_uri {
114
+ result.aset(ruby.sym_new("with_base_uri"), ruby.str_new(uri))?;
115
+ }
116
+
117
+ Ok(result)
96
118
  }
97
119
 
98
120
  fn validate(&self, json: String) -> Result<Vec<String>, Error> {
@@ -108,15 +130,13 @@ impl Validator {
108
130
 
109
131
  let mut errors: Vec<String> = vec![];
110
132
 
111
- if let Err(validation_errors) = self.schema.validate(&value) {
112
- for error in validation_errors {
113
- let path = match format!("{}", error.instance_path).as_str() {
114
- "" => "/".to_string(),
115
- p => p.to_string(),
116
- };
133
+ for error in self.schema.iter_errors(&value) {
134
+ let path = match format!("{}", error.instance_path).as_str() {
135
+ "" => "/".to_string(),
136
+ p => p.to_string(),
137
+ };
117
138
 
118
- errors.push(format!("path \"{}\": {}", path, error));
119
- }
139
+ errors.push(format!("path \"{}\": {}", path, error));
120
140
  }
121
141
 
122
142
  Ok(errors)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RustJSONSchema
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.2"
5
5
  end
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.3.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Thurlow
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-09-16 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rake-compiler
@@ -47,6 +46,7 @@ extensions:
47
46
  extra_rdoc_files: []
48
47
  files:
49
48
  - ".rspec"
49
+ - ".ruby-version"
50
50
  - ".standard.yml"
51
51
  - Cargo.lock
52
52
  - Cargo.toml
@@ -66,7 +66,6 @@ metadata:
66
66
  homepage_uri: https://github.com/taylorthurlow/rust_json_schema-rb
67
67
  source_code_uri: https://github.com/taylorthurlow/rust_json_schema-rb
68
68
  changelog_uri: https://github.com/taylorthurlow/rust_json_schema-rb/releases
69
- post_install_message:
70
69
  rdoc_options: []
71
70
  require_paths:
72
71
  - lib
@@ -81,8 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
80
  - !ruby/object:Gem::Version
82
81
  version: 3.3.11
83
82
  requirements: []
84
- rubygems_version: 3.5.18
85
- signing_key:
83
+ rubygems_version: 4.0.17
86
84
  specification_version: 4
87
85
  summary: Ruby wrapper for jsonschema-rs
88
86
  test_files: []