我该怎么做有条件验证的ActiveRecord的有n个条件是什么? [英] How do I do conditional validation in ActiveRecord with n conditions?

查看:700
本文介绍了我该怎么做有条件验证的ActiveRecord的有n个条件是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供一个Web服务由外部公司进行调用。所需的数据覆盖了几个模型,包括人,地址等。我想验证有条件基于所述请求内的一些字段的接收的数据。我最终将有校验数据的许多不同的组,但目前我只有一个,我要添加第二个。

I am providing a web service to be called by external companies. The required data covers several models including person, address etc. I want to validate the received data conditionally based upon some fields within the request. I will eventually have many differing sets of validation data, although currently I only have one and I am about to add the second.

我目前的模式看起来像这样

My current model looks something like this

class Person < ActiveRecord::Base
    validates_length_of :first_name, :within => 1..32, :allow_blank => true
    ...
    ...
end

在概念上我的模型,现在需要做这样的事情。

Conceptually my model now needs to do something like this.

class Person < ActiveRecord::Base
    validate :first_name?  

    def first_name?
        if country == 'UK'
            if company_name == 'ABC'
                validates_length_of :first_name, :within => 1..32
            else if company_name == 'DEF'
                validates_length_of :first_name, :within => 2..20
            end
        else if country == 'DE'
             if company_name == 'ABC'
                validates_length_of :first_name, :within => 1..32
            else if company_name == 'DEF'
                validates_length_of :first_name, :within => 2..20
            end
    end
end

这显然会工作得很好了2家企业/国家,但由于公司和/或国家的数量增加将无法正常工作。我现在考虑保持或者在数据库或在YAML文件的验证数据,然后手动执行验证为基于所述最小值,最大值,格式从模型外部存储值的各字段

This would obviously work fine for 2 companies/countries but will not work well as the number of companies and/or countries increases. I am now considering keeping the validation data either in the database or in a YAML file and then performing the validations manually for each field based upon the minimum, maximum, format values stored externally from the model.

我在想,我可以存储验证数据类似于以下

I am thinking that I could store the validation data in a structure similar to the following


country: UK
    companyname: ABC  
        field: first_name  
            minimum_length: 2  
            maximum_length: 20  
            required: true  
        field: middle_name  
            minimum_length: 1  
            maximum_length: 10  
        field: email_address  
            minimum_length: 10  
            format: /someregexforemail addresses/

    companyname: DEF
        field
           ...
country: DE
    companyname: XYZ
       field: 
         ....  

等。

然后,我可以加载此验证数据,并在我自己的手卷验证使用。

I could then load this validation data and use this within my own hand-rolled validator.

有没有人做过类似的事情?你用什么方法呢?我特别想知道你是如何处理以下。

Has anyone done similar things in the past and what methods did you use? I am particularly interested to know how you approached the following.

  • 你在哪里存储配置数据,即DB或YAML?
  • 在你加载和分析的配置数据为每个请求或另一次是在服务器上加载?
  • 您是如何结构做了验证?
  • 的实际方法

推荐答案

我与电话号码类似的东西。你的做法很相似,我做我自己。要回答你的问题:

I did something similar with phone numbers. Your approach is very similar to what I did myself. To answer your questions:

  1. 在我保存在YAML文件验证配置。 Hovewer,如果你的数据会增长,在很大程度上,你应该考虑使用的数据库。
  2. 我加载和分析对每个请求的数据。我不认为这是最好的方法,但并计划重写这一部分。
  3. 在我写我自己的验证。你可以来知道如何在这里做

有没有能够帮助您?

这篇关于我该怎么做有条件验证的ActiveRecord的有n个条件是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆