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

Uncaught parse errors during schema validation #167

Closed
eapache-opslevel opened this issue Dec 22, 2023 · 3 comments
Closed

Uncaught parse errors during schema validation #167

eapache-opslevel opened this issue Dec 22, 2023 · 3 comments

Comments

@eapache-opslevel
Copy link

json_schemer version 2.1.1

Trying to validate the following malformed schema:

JSONSchemer.validate_schema({"$schema"=>"https://json-schema.org/draft/2020-12/schema", "type"=>"object", "properties"=>""})

Results in an exception:

/Users/eapache/.rvm/gems/ruby-3.2.2@opslevel/gems/json_schemer-2.1.1/lib/json_schemer/draft202012/vocab/applicator.rb:224:in `parse': undefined method `each_with_object' for "":String (NoMethodError)

            value.each_with_object({}) do |(property, subschema), out|
                 ^^^^^^^^^^^^^^^^^
@davishmcclurg
Copy link
Owner

Hi @eapache-opslevel! Thanks for opening this. You're right—it's broken when schema parsing fails. I think the solution is to not parse the schema fully (should only need $schema) when validation schemas. I need to think about it some more, though. I'm not sure about custom meta schemas.

davishmcclurg added a commit that referenced this issue Jan 23, 2024
Parsing the whole schema can lead to errors for malformed keyword
values, eg:

```
>> JSONSchemer.schema({ 'properties' => '' })
/Users/dharsha/repos/json_schemer/lib/json_schemer/draft202012/vocab/applicator.rb:224:in `parse': undefined method `each_with_object' for an instance of String (NoMethodError)

            value.each_with_object({}) do |(property, subschema), out|
                 ^^^^^^^^^^^^^^^^^
	from /Users/dharsha/repos/json_schemer/lib/json_schemer/keyword.rb:14:in `initialize'
```

Instead, this creates a minimal parseable schema with just the `$schema`
value, if it's present and a string. That way the meta schema can be
determined as usual and then used to validate the provided schema.

Addresses: #167
@davishmcclurg davishmcclurg changed the title undefined method each_with_object for "":String (NoMethodError) Uncaught parse errors during schema validation Feb 25, 2024
@davishmcclurg
Copy link
Owner

Another example from #174:

>> schemer = JSONSchemer.schema({ "$id" => 1 })
>> schemer.valid_schema?
/Users/dharsha/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/uri/rfc3986_parser.rb:177:in `convert_to_uri': bad argument (expected URI object or URI string) (ArgumentError)
	from /Users/dharsha/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/uri/generic.rb:1110:in `merge'
	from /Users/dharsha/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/uri/rfc3986_parser.rb:141:in `inject'
	from /Users/dharsha/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/uri/rfc3986_parser.rb:141:in `join'
	from /Users/dharsha/.asdf/installs/ruby/3.3.0/lib/ruby/3.3.0/uri/common.rb:212:in `join'
	from /Users/dharsha/repos/json_schemer/lib/json_schemer/draft202012/vocab/core.rb:38:in `parse'

davishmcclurg added a commit that referenced this issue Feb 25, 2024
Parsing the whole schema can lead to errors for malformed keyword
values, eg:

```
>> JSONSchemer.schema({ 'properties' => '' })
/Users/dharsha/repos/json_schemer/lib/json_schemer/draft202012/vocab/applicator.rb:224:in `parse': undefined method `each_with_object' for an instance of String (NoMethodError)

            value.each_with_object({}) do |(property, subschema), out|
                 ^^^^^^^^^^^^^^^^^
	from /Users/dharsha/repos/json_schemer/lib/json_schemer/keyword.rb:14:in `initialize'
```

Instead, this creates a minimal parseable schema with just the `$schema`
value, if it's present and a string. That way the meta schema can be
determined as usual and then used to validate the provided schema.

Addresses: #167
davishmcclurg added a commit that referenced this issue Mar 2, 2024
Features:

- Global configuration
- Symbol key property defaults
- Better schema validation support

See individual commits for more details.

Closes:

- #157
- #162
- #167
- #173
davishmcclurg added a commit that referenced this issue Mar 2, 2024
Features:

- Global configuration
- Symbol key property defaults
- Better schema validation support

See CHANGELOG.md and individual commits for more details.

Closes:

- #157
- #162
- #167
- #173
@davishmcclurg davishmcclurg mentioned this issue Mar 2, 2024
@davishmcclurg
Copy link
Owner

I merged a fix—it'll be released shortly in 2.2.0. Original example now returns an error:

>> JSONSchemer.validate_schema({"$schema"=>"https://json-schema.org/draft/2020-12/schema", "type"=>"object", "properties"=>""}).map { _1.fetch('error') }
=> ["value at `/properties` is not an object"]

JSONSchemer.schema still doesn't support invalid schemas (and likely won't). I don't want every schema object to have to take the hit of validating the provided schema before parsing. When working with schemas that are potentially invalid, it's best to check them with JSONSchemer.valid_schema? first.

Now that I think of it, JSONSchemer::Schema#valid_schema? and JSONSchemer::Schema#validate_schema were probably mistakes to begin with—may want to deprecate in the future.

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

2 participants