Rails:将模型的验证器序列化为 json [英] Rails: serializing a model's validators into json
问题描述
我一直在使用 Rails 和许多前端 js 框架和库,包括 Ember、Angular 和 React.虽然这三个库在他们自己的方面都很强大,但一个痛点(至少对我来说)一直是表单验证.我一直讨厌让我的模型验证(在 Rails 中)与我的表单验证(在 Ember/Angular/React 中)同步.
I've been working with Rails and a number of frontend js frameworks and libs, including Ember, Angular, and React. While all three libraries are powerful in their own regard, one pain point (for me at least) has always been form validation. I've always hated having to keep my model validations (in Rails) in sync with my form validations (in Ember/Angular/React).
最近,我一直在尝试将模型的验证器序列化为 json.然而,虽然在记录上调用 as_json
会给我一个 json
哈希,但它不会给我特定属性的验证器类型.
Lately, I've been attempting to serialize a model's validators into json. However, while calling as_json
on a record will give me back a json
hash, it doesn't give me the type of validators for a particular attribute.
例如,假设我有一个名为 Assignment
的模型.当我创建一个新的 Assignment
记录并对其调用 _validators
时,这就是我得到的.
For example, let's say I have a model called Assignment
. When I create a new Assignment
record and call _validators
on it, this is what I get.
pry(main)> Assignment.new._validators
=>
{
:title=>[#<ActiveRecord::Validations::PresenceValidator:0x0000010e123900 @attributes= [:title], @options={}>],
:full_prompt=>[#<ActiveRecord::Validations::PresenceValidator:0x0000010e122e60 @attributes=[:full_prompt], @options={}>],
:submission_window=>[#<ActiveRecord::Validations::PresenceValidator:0x0000010e1223c0 @attributes=[:submission_window], @options={}>]
}
现在这是我添加 as_json
调用时得到的结果:
Now here's what I get when I add on the as_json
call:
pry(main)> Assignment.new._validators.as_json
=>
{
"title"=>[{"attributes"=>["title"], "options"=>{}}],
"full_prompt"=>[{"attributes"=>["full_prompt"], "options"=>{}}],
"submission_window"=>[{"attributes"=>["submission_window"], "options"=>{}}]
}
如您所见,调用 as_json
会删除附加到模型属性的 validators
类型.
As you can see, calling as_json
removes what types of validators
were attached to a model's attribute.
有没有人遇到过类似的情况和/或有解决方法?感谢您的帮助!
Has anybody run into a similar situation and/or has a workaround? Thanks for any help!
库尔特
推荐答案
Rails 向 Object
添加了一个名为 as_json
的方法.如您所见此处 它检查有问题的对象是否响应 to_hash
(默认情况下 ActiveModel::Validator
不响应).
Rails adds a method to Object
called as_json
. As you can see here it checks to see if the object in question responds to to_hash
(which ActiveModel::Validator
does not by default).
后备它调用 instance_values.as_json(options)
这就是你获取默认 JSON 的方式.
The fallback it to invoke instance_values.as_json(options)
which is how you're getting the default JSON.
我会在 ActiveModel::Validator
上实现 to_hash
方法,你可以把任何你想要的信息放进去.
I would implement the to_hash
method on ActiveModel::Validator
and you can put whatever information in there you'd like.
初始化程序/active_model_validator.rb
class ActiveModel::Validator
def to_hash
{} # custom implementation here...
end
end
如果您想维护默认的 JSON 并简单地向其附加附加信息,您可以执行类似的操作
If you want to maintain the default JSON and simply append additional information to it you could do something like
初始化程序/active_model_validator.rb
class ActiveModel::Validator
def to_hash
instance_values.tap do |hash|
# custom implementation here...
end
end
end
这篇关于Rails:将模型的验证器序列化为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!