activerecord_json_validator
activerecord_json_validator copied to clipboard
Error message is not parsed properly
I am using it in a engine core
, on Rails 5.2.0
validates :children, json: { schema: CHILDREN_SCHEMA }
I am not getting proper error message
"children": [
"translation missing: en.activerecord.errors.models.core/user.attributes.children.invalid_json"
]
After adding :message
option
validates :children, json: { message: ->(errors) { errors }, schema: CHILDREN_SCHEMA }
The error message is:
"children": [
"The property '#/0' did not contain a required property of 'gender' in schema file:///Users/navneet/rails_app/components/core/app/models/schemas/children.json"
]
Is there any way to change the message format similar to:
"children": [
"0": {
"gender property missing"
}
]
Any suggestions will be helpful.
I think customising the error is not possible as the message is generated by json-schema
json-schema/required
Yes you’re right, the default error message is invalid_json
, which is then translated by ActiveRecord.
The way you’re using the message
option is correct — you could pass the errors
argument through a custom method to parse errors into a structured hash:
class Foo < ApplicationRecord
validates :children, json: { message: ->(errors) { Foo.parse_json_errors(errors) }, schema: CHILDREN_SCHEMA }
def self.parse_json_errors(errors)
# Here, you can parse the `errors` array into something more meaningful
end
end
I hope this helps you!