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

Only parse $schema during schema validation #171

Merged
merged 3 commits into from Jan 23, 2024

Conversation

davishmcclurg
Copy link
Owner

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


Also made schema validation methods more consistent with the regular validation methods:

  • String and Pathname support
  • Validation option support (output_format, resolve_enumerators, and access_mode)

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
This aligns the schema generation (`JSONSchemer.schema`) and schema
validation (`valid_schema?` and `validate_schema`) method interfaces, so
that they all take the same schema object types.
The default meta schemas don't inherit the provided options, because
they're initialized with their own options and cached. To override
validation-time options, they need to be passed directly to the
validation methods.

This is a little brittle because the validation-time options are listed
separately to pull them out of the options hash. I couldn't think of a
better way to split the initialization and validation options.
@davishmcclurg davishmcclurg merged commit c0688ef into 2.2.0 Jan 23, 2024
58 of 62 checks passed
@davishmcclurg davishmcclurg deleted the schema-validation-parse-error branch January 23, 2024 22:28
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

Successfully merging this pull request may close these issues.

None yet

1 participant