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

JSON::Schema::Reader #175

Merged
merged 6 commits into from
Dec 2, 2014
Merged

JSON::Schema::Reader #175

merged 6 commits into from
Dec 2, 2014

Conversation

pd
Copy link
Contributor

@pd pd commented Nov 3, 2014

Adds a JSON::Schema::Reader object/interface that can be used to control how URLs/paths are fetched. The default behavior is the same as on master: unregistered schema references will be fetched used open-uri or File.read. The default implementation accepts initializer arguments that can be used to reject all URIs, all files, or as callbacks that will receive an Addressable::URI or Pathname object and should return true or false to indicate whether it should be requested.

I've added documentation to the README explaining how to configure the reader, either by replacing the default instance used, or supplying a reader to use for a single call to validate.

This addresses issues from #47, #65, #152, #173 / #174. Probably others. I didn't add any support for my NullLoader NullReader idea mentioned in #152; since it's a simple interface to implement, users can just define their own which returns an empty schema (JSON::Schema.new({}, ...)).

@iainbeeston
Copy link
Contributor

So the loader will be the single point through which we load any url, anywhere? (If so I'm all for it)

@pd pd force-pushed the schema-loader branch 2 times, most recently from 02d08bb to 32314c3 Compare November 3, 2014 21:24
@pd
Copy link
Contributor Author

pd commented Nov 3, 2014

I think the remaining failures here are artifacts of the mixed use of URI and Addressable::URI, and would probably be solved by #174. I'll try merging that in (minus the no-file-URI rule stuff) and seeing what happens when I get a chance.

@pd pd force-pushed the schema-loader branch 3 times, most recently from 26e2582 to 7bc7362 Compare November 4, 2014 01:50
@pd
Copy link
Contributor Author

pd commented Nov 4, 2014

Rebased onto @RST-J's changes in #174 and the build is green. =)

Still a variety of things to do: document this in the README, see if we can 100% disconnect the test suite from the interwebs and maybe even drop webmock, etc.

@pd
Copy link
Contributor Author

pd commented Nov 25, 2014

Just rebased this onto the new master after a lot of other changes around URI handling have landed; @RST-J's new tests for handling $ref with spaces in the names ended up exposing a bug in this branch!

I'll try to come back and tidy this up soon, plus add some documentation.

@pd
Copy link
Contributor Author

pd commented Nov 28, 2014

@RST-J @iainbeeston this is ready for review if you get time.

end
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
Validator.add_schema(schema)
rescue
Copy link
Contributor

Choose a reason for hiding this comment

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

This is kind of a side-note, but it'd be really good if we could catch specific errors here...

@iainbeeston
Copy link
Contributor

Sorry I didn't get a chance before, but this looks good. I like the idea of having a modular loader like this.

My only comment is that I wonder if there should be a simpler API for users to set accept_uri and accept_file (I've no idea how, maybe by adding a convenience method on JSON::Validator?). It's a feature I can imagine using a lot, but every other json-schema option (at present) can be set when calling JSON::Validator.validate

Conflicts:
	lib/json-schema/validator.rb
Prolly got some textile syntax a bit wrong, and the link to the yard
docs won't work prior to this being merged.
I'll submit a separate PR to add pry (and pry-byebug) as dependencies in
the Gemfile.

Reverted some changes I made to attributes/ref.rb that would conflict
with voxpupuli#186.
@pd
Copy link
Contributor Author

pd commented Nov 30, 2014

I'd be nervous about adding any further options to validate and its friends. I don't imagine there being a lot of variants to using this feature in a single code base, and my main goal was to be able to replace the entire thing with a single call; in my use case, I actually just want to deny any-remote-request-ever, because that's a sign of a bug. My initial (now-rebased-away) version of the Loader class didn't support any options at all, it just maintained the current behavior of master, but made it trivial for me to change that by providing my own loader instance.

@iainbeeston
Copy link
Contributor

Does anyone else have any feedback on this PR?

@RST-J
Copy link
Contributor

RST-J commented Dec 1, 2014

Very cool feature, this boosts performance more than we could with anything else at the moment I think. Plus it provides more security :-)
Seems fine to me. 👍

@RST-J RST-J mentioned this pull request Dec 1, 2014
4 tasks
@iainbeeston
Copy link
Contributor

So, do we want to rename Loader to something else before merging?

@RST-J RST-J added this to the v2.5.0 milestone Dec 2, 2014
@pd
Copy link
Contributor Author

pd commented Dec 2, 2014

Yeah, let me rename this first. I'm gonna go with Reader since it's basically spot on with how I described it's role: "read (as in I/O)". Not sure why it didn't occur to me before. =)

I'll do that in the next couple of hours and ping you guys here when I'm done.

@pd pd changed the title JSON::Schema::Loader JSON::Schema::Reader Dec 2, 2014
@pd
Copy link
Contributor Author

pd commented Dec 2, 2014

Renamed JSON::Schema::Loader -> JSON::Schema::Reader. I think I got everything, but this could definitely use another set of eyes; ruby can be a bit unfriendly for "rename class" type refactorings. =)

@iainbeeston
Copy link
Contributor

The tests would catch it, right? 😉

@RST-J
Copy link
Contributor

RST-J commented Dec 2, 2014

👍

1 similar comment
@iainbeeston
Copy link
Contributor

👍

iainbeeston added a commit that referenced this pull request Dec 2, 2014
@iainbeeston iainbeeston merged commit 7c9d44a into voxpupuli:master Dec 2, 2014
pd added a commit to pd/json-schema that referenced this pull request Dec 2, 2014
Introduced by conflicts in how voxpupuli#175 and voxpupuli#196 juggled webmock usage.

Closes voxpupuli#207.
@pd pd deleted the schema-loader branch December 2, 2014 22:30
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.

3 participants