StrongJSON
This library allows you to test the structure of JSON objects.
This is similar to Strong Parameters, which is introduced by Rails 4, but expected to work with more complex structures. It may help you to understand what this is as: Strong Parameter is for simple structures, like HTML forms, and StrongJSON is for complex structures, like JSON objects posted to API.
Installation
Add this line to your application's Gemfile:
gem 'strong_json'
And then execute:
$ bundle
Or install it yourself as:
$ gem install strong_json
Usage
s = StrongJSON.new do
let :item, object(id: prohibited, name: string, count: numeric)
let :custoemr, object(name: string, address: string, phone: string, email: optional(string))
let :order, object(customer: customer, items: array(item))
end
json = s.order.coerce(JSON.parse(input))
If the input JSON data is conformant with order's structure, the json will be that value.
If the input JSON contains attributes which is not white-listed in the definition, the fields will be droped.
If an attribute has a value which does not match with given type, the coerce method call will raise an exception StrongJSON::Type::Error.
If the input JSON contains prohibited attributes, id of item in the example, it also will result in an exception.
Catalogue of Types
object(f1: type1, f2: type2, ...)
- The value must be an object
- Fields,
f1,f2, and ..., must be present and its values must be oftype1,type2, ..., respectively - Other fields will be ignored
array(type)
- The value must be an array
- All elements in the array must be value of given
type
optional(type)
- The value can be
nil(or not contained in an object) - If an value exists, it must be of given
type
Base types
numberThe value must be an instance ofNumericstringThe value must be an instance ofStringbooleanThe value must betrueorfalsenumericThe value must be an instance ofNumericor a string which represents a numberanyAny value exceptnilis acceptedprohibitedAny value will be rejected
Shortcuts
There are some alias for optional(base), where base is base types, as the following:
number?string?boolean?numeric?
Contributing
- Fork it ( https://github.com/soutaro/strong_json/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request