No commit activity in last 3 years
No release in over 3 years
An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 1.7.7
>= 0
~> 2.13

Runtime

 Project Readme

ActiveModel::Serializer::Validator

Gem Version Code Climate Build Status

This gem adds JSON schema validations for the JSON output generated by an ActiveModel::Serializer.

Installation

Add this line to your application's Gemfile:

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

{
  "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

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 and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego 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 who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.