Skip to content

Commit

Permalink
Merge pull request #9 from driv3r/feature/update-error-messages
Browse files Browse the repository at this point in the history
Update jsonschema-rs & error messages formatting
  • Loading branch information
driv3r authored Jun 2, 2021
2 parents 9240edc + e40b2e9 commit 12fb1a8
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.0]
### Changed
- Update `jsonschema` dependency
- Update gems
- Update error messages to include path, that's instead of `"rusty" is not of type "numer"` it will be `path "/bar": "rusty" is not of type "number"`

## [0.6.1]
### Changed
- Update `jsonschema` dependency
Expand Down
101 changes: 98 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ crate-type = ["cdylib"]

[dependencies]
libc = "0.2.81"
jsonschema = "0.6.1"
jsonschema = "0.9.0"
serde_json = "1.0"
48 changes: 24 additions & 24 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
PATH
remote: .
specs:
rusty_json_schema (0.6.1)
rusty_json_schema (0.9.0)
ffi (~> 1.14)
json (>= 1.0)
thermite (~> 0)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.1)
benchmark-ips (2.8.4)
ast (2.4.2)
benchmark-ips (2.9.1)
diff-lcs (1.4.4)
ecma-re-validator (0.3.0)
regexp_parser (~> 2.0)
ffi (1.15.0)
ffi (1.15.1)
hana (1.3.7)
json (2.5.1)
json_schemer (0.2.17)
json_schemer (0.2.18)
ecma-re-validator (~> 0.3)
hana (~> 1.3)
regexp_parser (~> 2.0)
uri_template (~> 0.7)
minitar (0.9)
parallel (1.20.1)
parser (2.7.2.0)
parser (3.0.1.1)
ast (~> 2.4.1)
rainbow (3.0.0)
rake (13.0.1)
regexp_parser (2.0.0)
rexml (3.2.4)
rake (13.0.3)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.0)
rubocop (1.6.1)
rspec-support (3.10.2)
rubocop (1.16.0)
parallel (~> 1.10)
parser (>= 2.7.1.5)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.2.0, < 2.0)
rubocop-ast (>= 1.7.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.3.0)
parser (>= 2.7.1.5)
rubocop-performance (1.9.1)
rubocop (>= 0.90.0, < 2.0)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.7.0)
parser (>= 3.0.1.1)
rubocop-performance (1.11.3)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rspec (2.1.0)
rubocop-rspec (2.3.0)
rubocop (~> 1.0)
rubocop-ast (>= 1.1.0)
ruby-progressbar (1.10.1)
ruby-progressbar (1.11.0)
thermite (0.13.0)
minitar (~> 0.5)
rake (>= 10)
tomlrb (~> 1.2)
tomlrb (1.3.0)
unicode-display_width (1.7.0)
unicode-display_width (2.0.0)
uri_template (0.7.0)

PLATFORMS
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To get validation errors

```ruby
validator.validate(event_json)
# => ["invalid...", ...]
# => ["path \"...\": invalid...", ...]
```

## Development
Expand Down
17 changes: 17 additions & 0 deletions lib/rusty_json_schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@ def self.release(pointer)

# Simple validation without actual error messages
#
# ## Examples
#
# validator = RustyJSONSchema.build(json_schema)
# validator.valid?(event)
# # => false|true
#
def valid?(event)
Binding.is_valid(self, RustyJSONSchema.dump(event))
end

# Full validation and error messages
#
# ## Examples
#
# validator = RustyJSONSchema.build(json_schema)
# validator.validate(event)
# # => [
# # 'path "/foo": "rusty" is not a "number"',
# # ...
# # ]
#
def validate(event)
Binding.validate(self, RustyJSONSchema.dump(event)).to_a
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rusty_json_schema/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RustyJSONSchema

VERSION = "0.6.1"
VERSION = "0.9.0"

end
16 changes: 8 additions & 8 deletions spec/rusty_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@

let(:schema) do
{
"properties": {
"foo": {
properties: {
foo: {
type: "string"
},
"bar": {
bar: {
type: "number"
},
"baz": {}
baz: {}
},
"required": ["baz"]
required: ["baz"]
}
end

Expand All @@ -63,9 +63,9 @@

it "returns validation errors" do
expect(validator.validate(event)).to eq [
"'\"rusty\"' is not of type 'number'",
"'1' is not of type 'string'",
"'baz' is a required property"
%(path "/bar": "rusty" is not of type "number"),
%(path "/foo": 1 is not of type "string"),
%(path "/": "baz" is a required property)
]
end
end
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ impl Validator {

if let Err(validation_errors) = self.schema.validate(event) {
for error in validation_errors {
errors.push(error.to_string());
let path = match format!("{}", error.instance_path).as_str() {
"" => "/".to_string(),
p => p.to_string(),
};

errors.push(format!("path \"{}\": {}", path, error));
}
}

Expand Down Expand Up @@ -209,9 +214,9 @@ mod tests {
let result = unsafe { helper_validate_result_as_vec(raw_result) };

let expectation: Vec<String> = vec![
String::from("\'\"rusty\"\' is not of type \'number\'"),
String::from("\'1\' is not of type \'string\'"),
String::from("\'baz\' is a required property"),
String::from("path \"/bar\": \"rusty\" is not of type \"number\""),
String::from("path \"/foo\": 1 is not of type \"string\""),
String::from("path \"/\": \"baz\" is a required property"),
];

assert_eq!(result, expectation);
Expand Down

0 comments on commit 12fb1a8

Please # to comment.