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

Add support for symbolized keys #91

Closed
mbykovskyy opened this issue Apr 9, 2021 · 3 comments
Closed

Add support for symbolized keys #91

mbykovskyy opened this issue Apr 9, 2021 · 3 comments

Comments

@mbykovskyy
Copy link

Currently, parsed JSON with symbolized key fails validation. Please add support for validating JSON with symbolized keys.

@jamietanna
Copy link

Have you got an example for what this trips up on?

@AhmedKamal20
Copy link

AhmedKamal20 commented Jan 8, 2022

Have you got an example for what this trips up on? @jamietanna

For example, Using activerecord_json_validator and passing a schema with symbolized keys like this :
validates :data, presence: false, json: { message: ->(errors) { errors }, schema: { type: :object } }
Raise this error :
JSONSchemer::InvalidSymbolKey (schemas must use string keys)

But like this
validates :data, presence: false, json: { message: ->(errors) { errors }, schema: { "type" => :object } }
Works fine

Refs:

davishmcclurg added a commit that referenced this issue May 22, 2023
This uses a refinement to support indifferent access for user-provided
hashes. The interface is purposefully limited to prevent future use of
hash methods that may not work as expected. Refinements are tricky,
though, since you lose them whenever they're out of scope (eg, passing a
hash to `include?` doesn't call the refinement's `==` method).

I initially tried using activesupport's `HashWithIndifferentAccess` but
it doesn't work for data hashes because `insert_property_defaults` needs
to operate on the actual hash. Requiring all of activesupport just for
`HashWithIndifferentAccess` also felt like a pain.

The refinement works slightly different from `HashWithIndifferentAccess`
because it leaves the keys as whatever type they already are. That might
be a little confusing in error objects and `insert_property_defaults`
but at least it's consistent with whatever is passed in.

Symbol keys are mostly being tested by running the
json-schema-test-suite tests again with symbol keys (using
`symbolize_names: true` when parsing JSON). I also pulled those tests
into their own file and cleaned things up.

Addresses:

- #91
- #123
davishmcclurg added a commit that referenced this issue May 22, 2023
This uses a refinement to support indifferent access for user-provided
hashes. The interface is purposefully limited to prevent future use of
hash methods that may not work as expected. Refinements are tricky,
though, since you lose them whenever they're out of scope (eg, passing a
hash to `include?` doesn't call the refinement's `==` method).

I initially tried using activesupport's `HashWithIndifferentAccess` but
it doesn't work for data hashes because `insert_property_defaults` needs
to operate on the actual hash. Requiring all of activesupport just for
`HashWithIndifferentAccess` also felt like a pain.

The refinement works slightly different from `HashWithIndifferentAccess`
because it leaves the keys as whatever type they already are. That might
be a little confusing in error objects and `insert_property_defaults`
but at least it's consistent with whatever is passed in.

Symbol keys are mostly being tested by running the
json-schema-test-suite tests again with symbol keys (using
`symbolize_names: true` when parsing JSON). I also pulled those tests
into their own file and cleaned things up.

Addresses:

- #91
- #123
davishmcclurg added a commit that referenced this issue Jul 27, 2023
This deep stringifies input hashes in order to support symbol (and other
non-string) keys. Symbol keys have been a common issue for people
forever (as evidenced by `InvalidSymbolKey`) and I've been hesitant to
address it because duplicating hashes and stringifying keys hurts
performance, but I think it's probably worth it.

The tricky thing here is that `insert_property_defaults`,
`before_property_validation`, and `after_property_validation` need
access to the actual hashes instead of the stringified version. To work
around that, the original instance is passed around in `Context` and can
be accessed by location using `original_instance`. Values passed in
hooks need to be re-stringified in case the user added non-string keys.

If this becomes a performance bottleneck, it may make sense to add a way
to turn it off for people that know they're using string keys.

Related:

- #91
- #123
davishmcclurg added a commit that referenced this issue Jul 29, 2023
This deep stringifies input hashes in order to support symbol (and other
non-string) keys. Symbol keys have been a common issue for people
forever (as evidenced by `InvalidSymbolKey`) and I've been hesitant to
address it because duplicating hashes and stringifying keys hurts
performance, but I think it's probably worth it.

The tricky thing here is that `insert_property_defaults`,
`before_property_validation`, and `after_property_validation` need
access to the actual hashes instead of the stringified version. To work
around that, the original instance is passed around in `Context` and can
be accessed by location using `original_instance`. Values passed in
hooks need to be re-stringified in case the user added non-string keys.

If this becomes a performance bottleneck, it may make sense to add a way
to turn it off for people that know they're using string keys.

Related:

- #91
- #123
davishmcclurg added a commit that referenced this issue Jul 31, 2023
Features:

- Draft 2020-12 support
- Draft 2019-09 support
- Output formats
- Annotations
- OpenAPI 3.1 schema support
- OpenAPI 3.0 schema support
- `insert_property_defaults` in conditional subschemas
- Error messages
- Non-string schema and data keys

See individual commits for more details.

Closes:

- #27
- #44
- #55
- #91
- #94
- #116
- #123
davishmcclurg added a commit that referenced this issue Aug 1, 2023
Features:

- Draft 2020-12 support
- Draft 2019-09 support
- Output formats
- Annotations
- OpenAPI 3.1 schema support
- OpenAPI 3.0 schema support
- `insert_property_defaults` in conditional subschemas
- Error messages
- Non-string schema and data keys

See individual commits for more details.

Closes:

- #27
- #44
- #55
- #91
- #94
- #116
- #123
@davishmcclurg davishmcclurg mentioned this issue Aug 1, 2023
davishmcclurg added a commit that referenced this issue Aug 19, 2023
This deep stringifies input hashes in order to support symbol (and other
non-string) keys. Symbol keys have been a common issue for people
forever (as evidenced by `InvalidSymbolKey`) and I've been hesitant to
address it because duplicating hashes and stringifying keys hurts
performance, but I think it's probably worth it.

The tricky thing here is that `insert_property_defaults`,
`before_property_validation`, and `after_property_validation` need
access to the actual hashes instead of the stringified version. To work
around that, the original instance is passed around in `Context` and can
be accessed by location using `original_instance`. Values passed in
hooks need to be re-stringified in case the user added non-string keys.

If this becomes a performance bottleneck, it may make sense to add a way
to turn it off for people that know they're using string keys.

Related:

- #91
- #123
davishmcclurg added a commit that referenced this issue Aug 19, 2023
Features:

- Draft 2020-12 support
- Draft 2019-09 support
- Output formats
- Annotations
- OpenAPI 3.1 schema support
- OpenAPI 3.0 schema support
- `insert_property_defaults` in conditional subschemas
- Error messages
- Non-string schema and data keys
- Schema bundling

See individual commits for more details.

Closes:

- #27
- #44
- #55
- #91
- #94
- #116
- #123
- #136
davishmcclurg added a commit that referenced this issue Aug 20, 2023
This deep stringifies input hashes in order to support symbol (and other
non-string) keys. Symbol keys have been a common issue for people
forever (as evidenced by `InvalidSymbolKey`) and I've been hesitant to
address it because duplicating hashes and stringifying keys hurts
performance, but I think it's probably worth it.

The tricky thing here is that `insert_property_defaults`,
`before_property_validation`, and `after_property_validation` need
access to the actual hashes instead of the stringified version. To work
around that, the original instance is passed around in `Context` and can
be accessed by location using `original_instance`. Values passed in
hooks need to be re-stringified in case the user added non-string keys.

If this becomes a performance bottleneck, it may make sense to add a way
to turn it off for people that know they're using string keys.

Related:

- #91
- #123
davishmcclurg added a commit that referenced this issue Aug 20, 2023
Features:

- Draft 2020-12 support
- Draft 2019-09 support
- Output formats
- Annotations
- OpenAPI 3.1 schema support
- OpenAPI 3.0 schema support
- `insert_property_defaults` in conditional subschemas
- Error messages
- Non-string schema and data keys
- Schema bundling

See individual commits for more details.

Closes:

- #27
- #44
- #55
- #91
- #94
- #116
- #123
- #136
@davishmcclurg
Copy link
Owner

Released in 2.0.0. Schema keys are deep-stringified to support symbols.

Note: symbol values are not supported (eg, type: :object above).

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

4 participants