Skip to content

Commit 15bda4c

Browse files
committed
Fix JSONParseError not being raised when #validate or #valid? called with invalid JSON
1 parent 70f90bd commit 15bda4c

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

ext/rust_json_schema/src/lib.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ struct Validator {
1212
}
1313

1414
impl Validator {
15-
fn new(ruby: &Ruby, json: String) -> Result<Validator, magnus::Error> {
15+
fn new(json: String) -> Result<Validator, magnus::Error> {
1616
let value: serde_json::Value = match serde_json::from_str(&json) {
1717
Ok(value) => value,
1818
Err(error) => {
1919
return Err(magnus::Error::new(
20-
ruby.get_inner(&JSON_PARSE_ERROR),
20+
Self::ruby().get_inner(&JSON_PARSE_ERROR),
2121
error.to_string(),
2222
))
2323
}
@@ -27,7 +27,7 @@ impl Validator {
2727
Ok(schema) => schema,
2828
Err(error) => {
2929
return Err(magnus::Error::new(
30-
ruby.get_inner(&SCHEMA_PARSE_ERROR),
30+
Self::ruby().get_inner(&SCHEMA_PARSE_ERROR),
3131
error.to_string(),
3232
))
3333
}
@@ -36,14 +36,31 @@ impl Validator {
3636
Ok(Validator { schema })
3737
}
3838

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+
};
4149

42-
self.schema.is_valid(&value)
50+
Ok(self.schema.is_valid(&value))
4351
}
4452

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+
4764
let mut errors: Vec<String> = vec![];
4865

4966
if let Err(validation_errors) = self.schema.validate(&value) {
@@ -57,7 +74,11 @@ impl Validator {
5774
}
5875
}
5976

60-
errors
77+
Ok(errors)
78+
}
79+
80+
fn ruby() -> Ruby {
81+
Ruby::get().unwrap()
6182
}
6283
}
6384

spec/rust_json_schema_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@
7878
)
7979
end
8080
end
81+
82+
context "when the input is invalid JSON" do
83+
it "raises RustJSONSchema::JSONParseError" do
84+
validator = RustJSONSchema::Validator.new(schema)
85+
86+
expect {
87+
validator.validate("not valid json")
88+
}.to raise_exception(RustJSONSchema::JSONParseError)
89+
end
90+
end
8191
end
8292

8393
describe "#valid?" do
@@ -130,6 +140,16 @@
130140
expect(result).to be false
131141
end
132142
end
143+
144+
context "when the input is invalid JSON" do
145+
it "raises RustJSONSchema::JSONParseError" do
146+
validator = RustJSONSchema::Validator.new(schema)
147+
148+
expect {
149+
validator.valid?("not valid json")
150+
}.to raise_exception(RustJSONSchema::JSONParseError)
151+
end
152+
end
133153
end
134154
end
135155
end

0 commit comments

Comments
 (0)