在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:mirego/activerecord_json_validator开源软件地址:https://github.com/mirego/activerecord_json_validator开源编程语言:Ruby 100.0%开源软件介绍:
InstallationAdd this line to your application's Gemfile: gem 'activerecord_json_validator', '~> 2.0.0' UsageJSON SchemaSchemas should be a JSON file {
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"city": { "type": "string" },
"country": { "type": "string" }
},
"required": ["country"]
} Rubycreate_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')
# 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
Schema
Additionally, you can use a class User < ActiveRecord::Base
# Constants
PROFILE_REGULAR_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema')
PROFILE_ADMIN_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile_admin.json_schema')
# 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 The schema is passed to the class User < ActiveRecord::Base
# Constants
JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema')
# JSON_SCHEMA = { 'type' => 'object', 'properties' => { 'foo' => { 'type' => 'integer', 'minimum' => 3 } } }
# JSON_SCHEMA = '{"type":"object","properties":{"foo":{"type":"integer","minimum":3}}}'
# Validations
validates :profile, presence: true, json: { schema: JSON_SCHEMA }
end MessageLike any other ActiveModel validation, you can specify either a However, you can also specify a 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
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 MiregoMirego 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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论