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

has_only: enforce a fixed sets of JSON object properties #8

Merged
merged 1 commit into from Apr 20, 2015

Conversation

derek-watson
Copy link
Contributor

I sometimes need to guarantee nothing gets accidentally added to a JSON response. I can't predict what might get accidentally added with has_not, so I've added a has_only method, which will cause any properties that have not been tested with has to throw an exception.

This applies to the object you add the call to, as well as any nested objects.

assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
  has_only
  item 0 do
    has :id, 1
    has :key, 'test'
    has :name, 'test'
  end
  item 1 do
    has 'id', 2
    has 'key', 'test'
  end
end

# Failure: element 1 has unexpected keys: name

@alto
Copy link
Owner

alto commented Apr 20, 2015

That's a very cool feature!

alto added a commit that referenced this pull request Apr 20, 2015
has_only: enforce a fixed sets of JSON object properties
@alto alto merged commit b34011d into alto:master Apr 20, 2015
@alto
Copy link
Owner

alto commented Apr 20, 2015

I released version 0.3.0 including that feature. 👍

@GBH
Copy link

GBH commented Apr 20, 2015

👍

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

3 participants