All Projects → mirego → Activerecord_json_validator

mirego / Activerecord_json_validator

Licence: bsd-3-clause
🔩 ActiveRecord::JSONValidator makes it easy to validate JSON attributes against a JSON schema.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Activerecord json validator

active model serializers validator
🃏 An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
Stars: ✭ 18 (-91.82%)
Mutual labels:  activerecord, json-schema
Anima
Minimal database operation library.
Stars: ✭ 210 (-4.55%)
Mutual labels:  activerecord
Groupify
Add group and membership functionality to your Rails models
Stars: ✭ 187 (-15%)
Mutual labels:  activerecord
Api Development Tools
📚 A collection of useful resources for building RESTful HTTP+JSON APIs.
Stars: ✭ 2,519 (+1045%)
Mutual labels:  json-schema
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (-13.18%)
Mutual labels:  activerecord
Activerecord Turntable
ActiveRecord Sharding Plugin
Stars: ✭ 206 (-6.36%)
Mutual labels:  activerecord
Ajv Errors
Custom error messages in JSON-Schema for Ajv
Stars: ✭ 185 (-15.91%)
Mutual labels:  json-schema
Type O Rama
👾 JS type systems interportability
Stars: ✭ 217 (-1.36%)
Mutual labels:  json-schema
Randumb
Adds ability to pull back random records from Active Record
Stars: ✭ 208 (-5.45%)
Mutual labels:  activerecord
React Json Editor
A dynamic form component for react using JSON-Schema.
Stars: ✭ 201 (-8.64%)
Mutual labels:  json-schema
Json schemer
JSON Schema validator. Supports drafts 4, 6, and 7.
Stars: ✭ 199 (-9.55%)
Mutual labels:  json-schema
Json Schema Spec
The JSON Schema I-D sources
Stars: ✭ 2,441 (+1009.55%)
Mutual labels:  json-schema
Verify Json
verify-json
Stars: ✭ 208 (-5.45%)
Mutual labels:  json-schema
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (-14.55%)
Mutual labels:  activerecord
Json Schema Org.github.io
JSON Schema website
Stars: ✭ 213 (-3.18%)
Mutual labels:  json-schema
Ajv Keywords
Custom JSON-Schema keywords for Ajv validator
Stars: ✭ 186 (-15.45%)
Mutual labels:  json-schema
Jsonform
Build forms from JSON Schema. Easily template-able. Compatible with Bootstrap 3 out of the box.
Stars: ✭ 2,416 (+998.18%)
Mutual labels:  json-schema
Validr
A simple, fast, extensible python library for data validation.
Stars: ✭ 205 (-6.82%)
Mutual labels:  json-schema
Angular Schema Form
Generate forms from a JSON schema, with AngularJS!
Stars: ✭ 2,456 (+1016.36%)
Mutual labels:  json-schema
Secondbase
Seamless second database integration for Rails.
Stars: ✭ 216 (-1.82%)
Mutual labels:  activerecord


ActiveRecord::JSONValidator makes it easy to validate
JSON attributes against a JSON schema.


Installation

Add this line to your application's Gemfile:

gem 'activerecord_json_validator'

Usage

JSON Schema

{
  "type": "object",
  "$schema": "http://json-schema.org/draft-04/schema",
  "properties": {
    "city": { "type": "string" },
    "country": { "type": "string" }
  },
  "required": ["country"]
}

Ruby

create_table "users" do |t|
  t.string "name"
  t.json "profile" # First-class JSON with PostgreSQL, yo.
end

class User < ActiveRecord::Base
  # Constants
  PROFILE_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema').to_s

  # Validations
  validates :name, presence: true
  validates :profile, presence: true, json: { schema: PROFILE_JSON_SCHEMA }
end

user = User.new(name: 'Samuel Garneau', profile: { city: 'Quebec City' })
user.valid? # => false

user = User.new(name: 'Samuel Garneau', profile: { city: 'Quebec City', country: 'Canada' })
user.valid? # => true

user = User.new(name: 'Samuel Garneau', profile: '{invalid JSON":}')
user.valid? # => false
user.profile_invalid_json # => '{invalid JSON":}'

Options

Option Description
:schema The JSON schema to validate the data against (see Schema section)
:message The ActiveRecord message added to the record errors (see Message section)
:options A Hash of json-schema-supported options to pass to the validator
Schema

ActiveRecord::JSONValidator uses the json-schema gem to validate the JSON data against a JSON schema. You can use any value that JSON::Validator.validate would take as the schema argument.

Additionally, you can use a Symbol or a Proc. Both will be executed in the context of the validated record (Symbol will be sent as a method and the Proc will be instance_execed)

class User < ActiveRecord::Base
  # Constants
  PROFILE_REGULAR_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema').to_s
  PROFILE_ADMIN_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile_admin.json_schema').to_s

  # Validations
  validates :profile, presence: true, json: { schema: lambda { dynamic_profile_schema } } # `schema: :dynamic_profile_schema` would also work

  def dynamic_profile_schema
    admin? ? PROFILE_ADMIN_JSON_SCHEMA : PROFILE_REGULAR_JSON_SCHEMA
  end
end
Message

Like any other ActiveModel validation, you can specify either a Symbol or String value for the :message option. The default value is :invalid_json.

However, you can also specify a Proc that returns an array of errors. The Proc will be called with a single argument — an array of errors returned by the JSON schema validator. So, if you’d like to add each of these errors as a first-level error for the record, you can do this:

class User < ActiveRecord::Base
  # Validations
  validates :profile, presence: true, json: { message: ->(errors) { errors }, schema: 'foo.json_schema' }
end

user = User.new.tap(&:valid?)
user.errors.full_messages
# => [
#      'The property '#/email' of type Fixnum did not match the following type: string in schema 2d44293f-cd9d-5dca-8a6a-fb9db1de722b#',
#      'The property '#/full_name' of type Fixnum did not match the following type: string in schema 2d44293f-cd9d-5dca-8a6a-fb9db1de722b#',
#    ]

License

ActiveRecord::JSONValidator is © 2013-2016 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

The tree logo is based on this lovely icon by Sara Quintana, from The Noun Project. Used under a Creative Commons BY 3.0 license.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].