New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert error hash to object with helpful error rendering messages? #48
Comments
Here's a more complete error rendering method. I'm sure it's incomplete, but it's a start: def decode_json_schemer_errors(error)
messages = []
formatted_data_pointer = format_data_pointer error["data_pointer"]
case error["type"]
when "required"
if error["details"].key? "missing_keys"
messages << "#{formatted_data_pointer}is missing required keys #{error["details"]["missing_keys"].join(", ")}"
else
messages << "#{formatted_data_pointer}is missing required keys"
end
when "schema"
if error["schema_pointer"] == "/additionalProperties"
messages << "contains unrecognized property '#{formatted_data_pointer}'"
else
messages << "does not validate: schema_pointer=#{error["schema_pointer"]}"
end
when "string"
messages << "property '#{formatted_data_pointer}' should be a string"
when "boolean"
messages << "property '#{formatted_data_pointer}' should be a boolean"
when "integer"
messages << "property '#{formatted_data_pointer}' should be a number"
when "object"
messages << "property '#{formatted_data_pointer}' should be an object"
when "pattern"
messages << "property '#{formatted_data_pointer}' does not match pattern: #{error["schema"]["pattern"]}"
else
messages << "does not validate: error_type=#{error["type"]}"
end
messages.flatten.join ", "
end
def format_data_pointer(data_pointer)
data_pointer = data_pointer
.sub(%r|^/|, '') # remove leading /
.sub('/', '.') # convert / into .
unless data_pointer.blank?
data_pointer = data_pointer + ' '
end
data_pointer
end |
Thanks for the issue! I'll keep this in mind when I get around to implementing the latest draft's "output formatting": #2 |
I'll likely leave the errors as hashes with a more useful default error message. |
leshill
added a commit
to leshill/json_schemer
that referenced
this issue
Apr 28, 2020
This is based on a snippet of code offered by @robacarp in davishmcclurg#48. The code has been cleaned up and tested and provides a standalone interface for converting errors into a human readable message: ```ruby errors = schema.validate({ 'a' => { 'x' => 1 } }).to_a errors.map {|err| JSONSchemer::Errors.pretty err } ``` Longer term, an option can be introduced that allows the generation of the message inline with the validation. For example, given the option `pretty: true`, then on line 229 of `lib/json_schemer/schema/base.rb` (as of #a2a6df8), the message could be added to the error hash: ```ruby error['message'] = JSONSchemer::Errors.pretty(error) if pretty ```
leshill
added a commit
to leshill/json_schemer
that referenced
this issue
Apr 28, 2020
This is based on a snippet of code offered by @robacarp in davishmcclurg#48. The code has been cleaned up and tested and provides a standalone interface for converting errors into a human readable message: ```ruby errors = schema.validate({ 'a' => { 'x' => 1 } }).to_a errors.map {|err| JSONSchemer::Errors.pretty err } ``` Longer term, an option can be introduced that allows the generation of the message inline with the validation. For example, given the option `pretty: true`, then on line 229 of `lib/json_schemer/schema/base.rb` (as of #a2a6df8), the message could be added to the error hash: ```ruby error['message'] = JSONSchemer::Errors.pretty(error) if pretty ```
leshill
added a commit
to leshill/json_schemer
that referenced
this issue
Apr 28, 2020
This is based on a snippet of code offered by @robacarp in davishmcclurg#48. The code has been cleaned up and tested and provides a standalone interface for converting errors into a human readable message: ```ruby errors = schema.validate({ 'a' => { 'x' => 1 } }).to_a errors.map {|err| JSONSchemer::Errors.pretty err } ``` Longer term, an option can be introduced that allows the generation of the message inline with the validation. For example, given the option `pretty: true`, then on line 229 of `lib/json_schemer/schema/base.rb` (as of #a2a6df8), the message could be added to the error hash: ```ruby error['message'] = JSONSchemer::Errors.pretty(error) if pretty ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, and thanks for working on this project. I'm working to get some helpful error messages produced by schema validation and having to jump through some hoops:
Would it be a useful enhancement to convert the error hash into an object which has rendering methods for building the plain text version of the error?
The text was updated successfully, but these errors were encountered: