rust_json_schema 0.1.1 → 0.1.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.
- checksums.yaml +4 -4
- 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 +8 -8
- metadata +1 -1
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
|
@@ -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
@@ -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
|