rust_json_schema 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64fe337a421bd1dc7c86285685347052694d0277ec43517e575a0d18abe53a19
4
- data.tar.gz: 64840e47fb7a9f03b8a40bb626b227f465a956e1b34c4f3247a0ec9c19894c48
3
+ metadata.gz: 29e46fa7734afd93281656bfbd1380e07ac6a9d810e619ed0091967f74dc2394
4
+ data.tar.gz: 806994df60e5857e1c5d29d672a9098e22c4d29dd17d59dde52d2ca54c499858
5
5
  SHA512:
6
- metadata.gz: 2f91e752ffd9b53875f496e1872ef155428901fe080cfcae390be74074ddc4b642936886d80d23624c35924b53557a8eaee3a4a9bd49a73cd8202ca7719ecf1b
7
- data.tar.gz: fce2704fcbfcc943b1c4013a67a2a4b30313695fe9be6f7787c8fbd1db3b6c16aca54bfff0612edf7f6b44828d2a70f0aba90c0c083c7fe3d8df9823a186478d
6
+ metadata.gz: fca158592848060388e817a9b350b222f422df3ef7eb01f9b22785429889ff8b8d1f7770bcc68589030c7e02aecd0266b3f0cae2de391da853f83a007c8105d8
7
+ data.tar.gz: 00c3f526a81a7c837e195d1b81ded85e3cf6ade49bf2aedaa591b67636aa1ee1f72cf2ce393ad81dc8b226fef20a9a08ba48bd04da596713e18b1b89832686e7
@@ -12,12 +12,12 @@ struct Validator {
12
12
  }
13
13
 
14
14
  impl Validator {
15
- fn new(ruby: &Ruby, json: String) -> Result<Validator, magnus::Error> {
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).unwrap();
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).unwrap();
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
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RustJSONSchema
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -2,14 +2,6 @@
2
2
 
3
3
  require_relative "rust_json_schema/version"
4
4
 
5
- # Tries to require the extension for the given Ruby version first
6
- begin
7
- RUBY_VERSION =~ /(\d+\.\d+)/
8
- require "rust_json_schema/#{Regexp.last_match(1)}/rust_json_schema"
9
- rescue LoadError
10
- require_relative "rust_json_schema/rust_json_schema"
11
- end
12
-
13
5
  module RustJSONSchema
14
6
  class Error < StandardError; end
15
7
 
@@ -17,3 +9,11 @@ module RustJSONSchema
17
9
 
18
10
  class SchemaParseError < Error; end
19
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rust_json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Thurlow