Skip to content

Commit

Permalink
Use json_schemer 2.0
Browse files Browse the repository at this point in the history
This upgrades to json_schemer 2.0, which is the latest major version. It
includes support for all current JSON Schema versions and new keywords
(eg, `unevaluatedProperties`). There are also new features that users of
this library may find helpful:

- Symbol key support for schemas and data
- Custom error messages with i18n or `x-error` keyword (it would be
  great to get feedback on this from your users)
- Global configuration options

Using 2.0 will likely require a major version bump because it comes with
breaking changes. See the [changelog][0] for more details.

Closes:
- mirego#72
- mirego#73

[0]: https://github.com/davishmcclurg/json_schemer/blob/main/CHANGELOG.md
  • Loading branch information
davishmcclurg committed Mar 2, 2024
1 parent b36556a commit 1ba600d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion activerecord_json_validator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop-rspec', '~> 1.44'
spec.add_development_dependency 'rubocop-standard', '~> 6.0'

spec.add_dependency 'json_schemer', '~> 0.2.18'
spec.add_dependency 'json_schemer', '~> 2.0'
spec.add_dependency 'activerecord', '>= 4.2.0', '< 8'
end
4 changes: 2 additions & 2 deletions lib/active_record/json_validator/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def validate_each(record, attribute, value)
return if errors.empty? && record.send(:"#{attribute}_invalid_json").blank?

# Add error message to the attribute
details = errors.map { |e| JSONSchemer::Errors.pretty(e) }
details = errors.map { |e| e.fetch('error') }
message(errors).each do |error|
error = JSONSchemer::Errors.pretty(error) if error.is_a?(Hash)
error = error.fetch('error') if error.is_a?(Hash)
record.errors.add(attribute, error, errors: details, value: value)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/json_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def smart_data

specify do
expect(user).not_to be_valid
expect(user.errors.full_messages).to eql(['Data root is missing required keys: country', 'Other data missing_keys country'])
expect(user.errors.full_messages).to eql(['Data object at root is missing required properties: country', 'Other data missing_keys country'])
expect(user.errors.group_by_attribute[:data].first).to have_attributes(
options: include(errors: ['root is missing required keys: country'])
options: include(errors: ['object at root is missing required properties: country'])
)
expect(user.errors.group_by_attribute[:other_data].first).to have_attributes(
options: include(errors: ['root is missing required keys: country'])
options: include(errors: ['object at root is missing required properties: country'])
)
expect(user.data).to eql({ 'city' => 'Quebec City' })
expect(user.data_invalid_json).to be_nil
Expand Down

0 comments on commit 1ba600d

Please sign in to comment.