Skip to content
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

Open
stanig2106 opened this issue Jul 2, 2023 · 2 comments
Open

InvalidSymbolKey error when using symbol keys in schema #72

stanig2106 opened this issue Jul 2, 2023 · 2 comments

Comments

@stanig2106
Copy link

stanig2106 commented Jul 2, 2023

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:

validates :data, json: {
  schema: {
    type: :object,
    properties: {
      time: { type: :number, minimum: 0 }
    },
    required: %i[time]
  }
}

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:

raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.empty? && !schema.first.first.is_a?(String)

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

@remi
Copy link
Member

remi commented Jul 5, 2023

Hi @stanig2106,

You’re right, json_schemer produces this error when symbols are used as keys for the schema.

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 json_schemer when passing hashes as the schema.

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!

davishmcclurg added a commit to davishmcclurg/activerecord_json_validator that referenced this issue Mar 2, 2024
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
@davishmcclurg
Copy link
Contributor

This would be partially covered by upgrading to json_schemer 2.0: #78
Symbol keys are supported, but not symbol values (eg, type: 'object' works but not type: :object)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants