-
Notifications
You must be signed in to change notification settings - Fork 49
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
InvalidSymbolKey error when using symbol keys in schema #72
Comments
Hi @stanig2106, You’re right, I don’t think we should circumvent this limitation in ActiveRecord::JSONValidator though, we don’t want to add and maintain an additional logic layer between us and What I would suggest is that you explicitly convert symbols to strings: validates :data, json: {
schema: {
type: :object,
properties: {
time: { type: :number, minimum: 0 }
},
required: %i[time]
}.deep_stringify_keys
} Hope that helps! |
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
This would be partially covered by upgrading to json_schemer 2.0: #78 |
Hello,
I'm currently using the
activerecord_json_validator
gem in a project and I've come across an issue when I use symbol keys in the JSON schema. Here's the code where the issue arises:When I run this, I get the following error:
JSONSchemer::InvalidSymbolKey: schemas must use string keys
This error seems to be raised from the
JSONSchemer::Schema::Base#initialize
method when the schema is a Hash, is not empty, and the first key in the schema is not a string. Here's the code block where this happens:Proposed Solution:
As a simple fix for this issue, instead of raising an error when the schema contains symbol keys, the schema could be modified to convert all keys to strings. We can use
Hash#deep_stringify_keys
(from active_support) method on the schema.This change would make the gem more flexible and user-friendly by eliminating the need for users to ensure that their schemas only use string keys. I believe this would be a beneficial change and would appreciate if it could be considered for implementation.
Thank you for your time and consideration.
Best,
Stani
The text was updated successfully, but these errors were encountered: