-
Notifications
You must be signed in to change notification settings - Fork 64
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
Support custom error messages #149
Conversation
This introduces two ways of defining custom error messages: the `x-error` keyword and I18n translations. `x-error` is a json_schemer-specific schema keyword that allows overriding error messsages for a schema. It can either be a string, which overrides errors for all keywords (and for the schema itself), or a hash of keyword-specific messages. Interpolation of some values is supported using `gsub` because `sprintf` logs annoying warnings if all arguments aren't present in the string. I18n translations are enabled when the `i18n` gem is loaded and a `json_schemer` translation key exists. Translation keys are based on schema $id, keyword location, meta-schema $id, and keyword. The lookup is ordered from most specific to least specific so that overrides can be applied as necessary. Notes: - Both `x-error` and I18n use special catch-all (`*`) and schema (`^`) "keywords" to support fallbacks and schema-level errors, respectively. - `x-error` uses the `x-` prefix to match the future spec for unknown keywords: https://github.com/orgs/json-schema-org/discussions/329 - The I18n check to enable translations is cached forever because I18n is slow and I didn't want it to hurt performance when it's not used. - I18n uses the ASCII unit separator (\x1F) because the `.` default doesn't work well with absolute URIs ($id and $schema). - I moved `CLASSIC_ERROR_TYPES` out of `JSONSchemer::Result` because that's actually where it's being defined. The `Struct.new` block scope doesn't hold constants like a class. Closes: #143
@ahx any interest in trying this out? I'd like to get some feedback before merging. |
I will try to look into this this weekend latest. Don't hesitate to punch me if I don't get back to you. |
One suggestion I have is, in the detailed results, add some additional key to indicate that the error was set via |
These keys make it possible to determine how error messages were generated. #149 (comment)
@babelfish good idea—I pushed a commit that adds |
Something not related to this PR, but to the 2.1.0 branch: I noticed that |
This shouldn't've been removed in: ccffac7 I was under the impression that it would be provided by draft4 since that's openapi30's meta schema, but it's not in that spec. OpenAPI 3.0 formats listed here: https://spec.openapis.org/oas/v3.0.3#data-types @ahx noticed this here: #149 (comment)
@ahx have you had a chance to look at this at all? |
I tried out the branch in openapi_first and found out about the issue with the date format, but I have not tried the i18n integration. But I really like the idea. |
This introduces two ways of defining custom error messages: the
x-error
keyword and I18n translations.x-error
is a json_schemer-specific schema keyword that allows overriding error messsages for a schema. It can either be a string, which overrides errors for all keywords (and for the schema itself), or a hash of keyword-specific messages. Interpolation of some values is supported usinggsub
becausesprintf
logs annoying warnings if all arguments aren't present in the string.I18n translations are enabled when the
i18n
gem is loaded and ajson_schemer
translation key exists. Translation keys are based on schema $id, keyword location, meta-schema $id, and keyword. The lookup is ordered from most specific to least specific so that overrides can be applied as necessary.Notes:
x-error
and I18n use special catch-all (*
) and schema (^
) "keywords" to support fallbacks and schema-level errors, respectively.x-error
uses thex-
prefix to match the future spec for unknown keywords: https://github.com/orgs/json-schema-org/discussions/329.
default doesn't work well with absolute URIs ($id and $schema).CLASSIC_ERROR_TYPES
out ofJSONSchemer::Result
because that's actually where it's being defined. TheStruct.new
block scope doesn't hold constants like a class.Closes: #143