ActiveRecord::JSONValidator
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-03/schema",
"properties": {
"city": { "type": "string", "required": false },
"country": { "type": "string", "required": true }
}
}
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 JSON schema option section) |
:message |
The ActiveRecord message added to the record errors (default: :invalid_json) |
JSON schema option
You can specify four kinds of value for the :schema option.
A path to a file containing a JSON schema
class User < ActiveRecord::Base
# Constants
PROFILE_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema').to_s
# Validations
validates :profile, presence: true, json: { schema: PROFILE_JSON_SCHEMA }
end
A Ruby Hash representing a JSON schema
class User < ActiveRecord::Base
# Constants
PROFILE_JSON_SCHEMA = {
type: 'object',
:'$schema' => 'http://json-schema.org/draft-03/schema',
properties: {
city: { type: 'string', required: false },
country: { type: 'string', required: true }
}
}
# Validations
validates :profile, presence: true, json: { schema: PROFILE_JSON_SCHEMA }
end
A plain JSON schema as a Ruby String
class User < ActiveRecord::Base
# Constants
PROFILE_JSON_SCHEMA = '{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"properties": {
"city": { "type": "string", "required": false },
"country": { "type": "string", "required": true }
}
}'
# Validations
validates :profile, presence: true, json: { schema: PROFILE_JSON_SCHEMA }
end
A lambda that will get evaluated in the context of the validated record
The lambda must return a valid value for the :schema option (file path, JSON String or Ruby Hash).
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 } }
def dynamic_profile_schema
admin? ? PROFILE_ADMIN_JSON_SCHEMA : PROFILE_REGULAR_JSON_SCHEMA
end
end
License
ActiveRecord::JSONValidator 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.