https://github.com/mirego/active_model_serializers_validator
:black_joker: An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
https://github.com/mirego/active_model_serializers_validator
activemodel activerecord json-schema
Last synced: 7 months ago
JSON representation
:black_joker: An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
- Host: GitHub
- URL: https://github.com/mirego/active_model_serializers_validator
- Owner: mirego
- License: bsd-3-clause
- Created: 2013-05-24T18:53:37.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2017-02-22T21:27:29.000Z (about 9 years ago)
- Last Synced: 2025-07-12T03:12:09.360Z (8 months ago)
- Topics: activemodel, activerecord, json-schema
- Language: Ruby
- Homepage: http://open.mirego.com
- Size: 324 KB
- Stars: 18
- Watchers: 62
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ActiveModel::Serializer::Validator
[](https://rubygems.org/gems/active_model_serializers_validator)
[](https://codeclimate.com/github/mirego/active_model_serializers_validator)
[](https://travis-ci.org/mirego/active_model_serializers_validator)
This gem adds JSON schema validations for the JSON output generated by an `ActiveModel::Serializer`.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'active_model_serializers_validator'
```
## Usage
The gem adds two instance methods (`#valid?` and `#errors`) and one class method (`.json_schema`) to the `ActiveModel::Serializer` class.
### A JSON schema
```json
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "#",
"required": false,
"additionalProperties": false,
"properties": {
"favorite_number": {
"type": "integer",
"id": "favorite_number",
"required": true
},
}
}
```
### A serializer
```ruby
class MySerializer < ActiveModel::Serializer
json_schema '/path/to/my/schema.jsonschema'
attribute :favorite_number
end
object = OpenStruct.new(favorite_number: 'what?')
serializer = MySerializer.new(object)
serializer.valid?
# => false
serializer.errors
# => [The property '#/foo' of type String did not match the following type: integer in schema /path/to/my/schema.jsonschema]
```
## License
`ActiveModel::Serializer::Validator` is © 2013-2015 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/active_model_serializers_validator/blob/master/LICENSE.md) file.
## About Mirego
[Mirego](http://mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of [talented people](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).
We also [love open-source software](http://open.mirego.com) and we try to give back to the community as much as we can.