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

Load local copy of draft schemas #362

Merged
merged 1 commit into from
Oct 5, 2016

Conversation

iainbeeston
Copy link
Contributor

If you refer to a draft schema in a $ref (as the common test suite does)
json-schema will load it from json-schema.org even though the gem
contains a local copy.

This change patches JSON::Validator#load_ref_schema to check whether a
$ref refers to a draft schema before trying to load from the web, and if
so parsing and serving up the local copy.

I've also replaced the local copy of draft3 with the exact version found
online (only whitespace was different).

@iainbeeston
Copy link
Contributor Author

This sounds like a minor change but it's necessary in order to clear the schema cache between tests in the common test suite.

begin
validator = self.class.validator_for_uri(schema_uri)
schema_uri = JSON::Util::URI.file_uri(validator.metaschema)
rescue JSON::Schema::SchemaError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this using exceptions for control flow (aka bad habit)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... That is exactly right. I tried to avoid refactoring the other method. Perhaps it should take an option, so that it can return nil if it finds nothing? (Or, even better, I could replace validator_for with two methods - validator_for and validator_for! - and have the first method return nil and the second throw an exception?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've already tried to clean up validator_for in #346 - perhaps I should take it further?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing that would also help resolve #254

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a bang and a non-bang method as you suggest is a good idea. And it should be easy to do as validator_for_uri itself explicitly checks for nil and if found raises the exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've corrected that now, by allowing validator_for_uri to take a second parameter that allows it to return nil (rather than raising an exception).

I chose that implementation (rather than bang methods) so that we don't change the default behaviour of any methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

If you refer to a draft schema in a $ref (as the common test suite does)
json-schema will load it from json-schema.org even though the gem
contains a local copy.

This change patches JSON::Validator#load_ref_schema to check whether a
$ref refers to a draft schema before trying to load from the web, and if
so parsing and serving up the local copy.

I've also replaced the local copy of draft3 with the exact version found
online (only whitespace was different).
@iainbeeston iainbeeston merged commit 9917157 into voxpupuli:master Oct 5, 2016
@iainbeeston iainbeeston deleted the cache-draft-schemas branch October 5, 2016 13:32
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.

2 participants