jsonapi-rb / jsonapi-renderer

Efficiently render JSON API documents.
http://jsonapi-rb.org
MIT License
27 stars 11 forks source link

Validate keys during initialization #17

Closed kinopyo closed 5 years ago

kinopyo commented 7 years ago

(Follow up https://github.com/jsonapi-rb/jsonapi-renderer/issues/16)

Raises InvalidKey exception when initialization:

JSONAPI::IncludeDirective.new("friends, comments, posts, author.posts")
# JSONAPI::IncludeDirective::InvalidKey:  comments

(@beauby Still quite a rough implementation, just to see if this is something we want. If so I'll continue do some refactoring, handling hounds and probably extracting the validation part to its own class πŸ™‚)

codecov-io commented 7 years ago

Codecov Report

Merging #17 into master will increase coverage by 0.02%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #17      +/-   ##
==========================================
+ Coverage   99.33%   99.36%   +0.02%     
==========================================
  Files           5        5              
  Lines         150      157       +7     
==========================================
+ Hits          149      156       +7     
  Misses          1        1
Impacted Files Coverage Ξ”
lib/jsonapi/include_directive.rb 100% <100%> (ΓΈ) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data Powered by Codecov. Last update df8965f...046cb68. Read the comment docs.

kinopyo commented 7 years ago

@beauby may I get some feedback πŸ™ πŸ™‡ (before I forget what I was doing πŸ˜… )

beauby commented 7 years ago

Hi @kinopyo – will do it tomorrow hopefully (do not hesitate to ping me if I don't !)

kinopyo commented 7 years ago

@beauby (human low tech ping πŸ‘‹ πŸ˜… )

beauby commented 7 years ago

Hi @kinopyo – thanks for bearing with my slow reviews! ;)

While the current approach has the benefit of listing all invalid keys, I'd rather have the initializer raise InvalidKey as soon as it hits the first invalid key. There are mainly two reasons why I prefer this fail fast strategy:

  1. once one key is invalid, the parser no longer works under the same assumptions, which means the following invalid keys may or may not make sense to the user (this is more of a general point about failing upon first invalid token in parsing, as I believe in the current implementation this would not be a problem);
  2. It's much less code (it becomes mainly a matter of validating the key here, and raise InvalidKey if invalid), and a smaller overhead.

What do you think?

kinopyo commented 7 years ago

@beauby oops sorry for keeping you waiting πŸ™‡ , and many thanks for your inputs! I think the fail-fast strategy is ok, and the gain of less code is more rewarding πŸ™‚ I've updated the PR and resolved Hound comments, what do you think?

kinopyo commented 7 years ago

@beauby Thank you for you quick review! Pushed 2 fixes 🎈 (and let me know if you prefer to have a single rebased commit before merging πŸ™‡ )

beauby commented 7 years ago

Looks good, will merge tonight! Thanks for following up with this! :+1:

dawidof commented 5 years ago

https://github.com/jsonapi-rb/jsonapi-renderer/pull/36