Simple Ruby Input Validation Library

前端 未结 3 1710
孤街浪徒
孤街浪徒 2020-12-28 10:28

I\'ve been looking all over the place for a simple input validation library for Ruby. Everything seems to point towards ActiveRecord (or similar). I\'m not using Rails, I\'m

3条回答
  •  遥遥无期
    2020-12-28 11:16

    You could use ActiveModel::Validations, from Rails 3 RC:

    require 'active_model'
    # this appears to be a bug in ActiveModel - it uses this, but does not require it
    require 'active_support/core_ext/hash'
    
    class Model
      include ActiveModel::Validations
    
      attr_accessor :name
      validates_presence_of :name
    end
    
    m = model.new
    puts m.valid? # false
    m.name = "John Doe"
    puts m.valid? # true
    

提交回复
热议问题