2009-02-03 04:21:32 +01:00
|
|
|
# Extracted from dm-validations 0.9.10
|
|
|
|
#
|
|
|
|
# Copyright (c) 2007 Guy van den Berg
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
# a copy of this software and associated documentation files (the
|
|
|
|
# "Software"), to deal in the Software without restriction, including
|
|
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
# the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
class Object
|
|
|
|
def validatable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-06 02:06:12 +01:00
|
|
|
require 'pathname'
|
|
|
|
require File.join(File.dirname(__FILE__), '..', 'support', 'class')
|
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
dir = File.join(Pathname(__FILE__).dirname.expand_path, '..', 'validation')
|
|
|
|
|
|
|
|
require File.join(dir, 'validation_errors')
|
|
|
|
require File.join(dir, 'contextual_validators')
|
2009-02-06 02:06:12 +01:00
|
|
|
require File.join(dir, 'auto_validate')
|
2009-02-03 04:21:32 +01:00
|
|
|
|
|
|
|
require File.join(dir, 'validators', 'generic_validator')
|
|
|
|
require File.join(dir, 'validators', 'required_field_validator')
|
|
|
|
require File.join(dir, 'validators', 'absent_field_validator')
|
|
|
|
require File.join(dir, 'validators', 'format_validator')
|
|
|
|
require File.join(dir, 'validators', 'length_validator')
|
|
|
|
require File.join(dir, 'validators', 'numeric_validator')
|
|
|
|
require File.join(dir, 'validators', 'method_validator')
|
2009-02-21 00:34:49 +01:00
|
|
|
require File.join(dir, 'validators', 'confirmation_validator')
|
2009-02-03 04:21:32 +01:00
|
|
|
|
|
|
|
module CouchRest
|
|
|
|
module Validation
|
|
|
|
|
|
|
|
def self.included(base)
|
2009-03-20 02:53:17 +01:00
|
|
|
base.extlib_inheritable_accessor(:auto_validation)
|
2009-02-10 11:15:39 +01:00
|
|
|
base.class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
# Turn off auto validation by default
|
2009-03-20 02:53:17 +01:00
|
|
|
self.auto_validation ||= false
|
2009-02-10 11:15:39 +01:00
|
|
|
|
|
|
|
# Force the auto validation for the class properties
|
|
|
|
# This feature is still not fully ported over,
|
|
|
|
# test are lacking, so please use with caution
|
|
|
|
def self.auto_validate!
|
|
|
|
self.auto_validation = true
|
|
|
|
end
|
2009-03-20 02:53:17 +01:00
|
|
|
|
|
|
|
# share the validations with subclasses
|
|
|
|
def self.inherited(subklass)
|
2009-03-25 06:18:28 +01:00
|
|
|
self.validators.contexts.each do |k, v|
|
|
|
|
subklass.validators.contexts[k] = v.dup
|
|
|
|
end
|
2009-03-25 20:00:20 +01:00
|
|
|
super
|
2009-03-20 02:53:17 +01:00
|
|
|
end
|
2009-02-10 11:15:39 +01:00
|
|
|
EOS
|
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
base.extend(ClassMethods)
|
2009-02-04 02:33:31 +01:00
|
|
|
base.class_eval <<-EOS, __FILE__, __LINE__
|
2009-02-04 03:23:30 +01:00
|
|
|
if method_defined?(:_run_save_callbacks)
|
2009-02-04 02:33:31 +01:00
|
|
|
save_callback :before, :check_validations
|
|
|
|
end
|
|
|
|
EOS
|
2009-02-06 02:06:12 +01:00
|
|
|
base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
|
|
|
def self.define_property(name, options={})
|
|
|
|
super
|
2009-03-20 02:53:17 +01:00
|
|
|
auto_generate_validations(properties.last) if properties && properties.size > 0
|
2009-02-06 02:06:12 +01:00
|
|
|
autovalidation_check = true
|
|
|
|
end
|
|
|
|
RUBY_EVAL
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 02:33:31 +01:00
|
|
|
# Ensures the object is valid for the context provided, and otherwise
|
|
|
|
# throws :halt and returns false.
|
|
|
|
#
|
|
|
|
def check_validations(context = :default)
|
|
|
|
throw(:halt, false) unless context.nil? || valid?(context)
|
|
|
|
end
|
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
# Return the ValidationErrors
|
|
|
|
#
|
|
|
|
def errors
|
|
|
|
@errors ||= ValidationErrors.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Mark this resource as validatable. When we validate associations of a
|
|
|
|
# resource we can check if they respond to validatable? before trying to
|
|
|
|
# recursivly validate them
|
|
|
|
#
|
|
|
|
def validatable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Alias for valid?(:default)
|
|
|
|
#
|
|
|
|
def valid_for_default?
|
|
|
|
valid?(:default)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check if a resource is valid in a given context
|
|
|
|
#
|
|
|
|
def valid?(context = :default)
|
2009-05-28 21:18:23 +02:00
|
|
|
recursive_valid?(self, context, true)
|
2009-05-14 07:50:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# checking on casted objects
|
|
|
|
def validate_casted_arrays
|
|
|
|
result = true
|
|
|
|
array_casted_properties = self.class.properties.select { |property| property.casted && property.type.instance_of?(Array) }
|
|
|
|
array_casted_properties.each do |property|
|
|
|
|
casted_values = self.send(property.name)
|
2009-05-21 03:35:41 +02:00
|
|
|
next unless casted_values.is_a?(Array) && casted_values.first.respond_to?(:valid?)
|
2009-05-14 07:50:29 +02:00
|
|
|
casted_values.each do |value|
|
|
|
|
result = (result && value.valid?) if value.respond_to?(:valid?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Do recursive validity checking
|
|
|
|
#
|
|
|
|
def recursive_valid?(target, context, state)
|
|
|
|
valid = state
|
2009-05-28 21:18:23 +02:00
|
|
|
target.each do |key, prop|
|
|
|
|
if prop.is_a?(Array)
|
|
|
|
prop.each do |item|
|
2009-02-03 04:21:32 +01:00
|
|
|
if item.validatable?
|
2009-05-28 21:18:23 +02:00
|
|
|
valid = recursive_valid?(item, context, valid) && valid
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
end
|
2009-05-28 21:18:23 +02:00
|
|
|
elsif prop.validatable?
|
|
|
|
valid = recursive_valid?(prop, context, valid) && valid
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
end
|
2009-05-28 21:18:23 +02:00
|
|
|
target.class.validators.execute(context, target) && valid
|
2009-02-03 04:21:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def validation_property_value(name)
|
|
|
|
self.respond_to?(name, true) ? self.send(name) : nil
|
|
|
|
end
|
|
|
|
|
2009-02-09 20:20:23 +01:00
|
|
|
# Get the corresponding Object property, if it exists.
|
2009-02-03 04:21:32 +01:00
|
|
|
def validation_property(field_name)
|
|
|
|
properties.find{|p| p.name == field_name}
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
include CouchRest::Validation::ValidatesPresent
|
|
|
|
include CouchRest::Validation::ValidatesAbsent
|
2009-02-21 00:34:49 +01:00
|
|
|
include CouchRest::Validation::ValidatesIsConfirmed
|
2009-02-03 04:21:32 +01:00
|
|
|
# include CouchRest::Validation::ValidatesIsPrimitive
|
|
|
|
# include CouchRest::Validation::ValidatesIsAccepted
|
|
|
|
include CouchRest::Validation::ValidatesFormat
|
|
|
|
include CouchRest::Validation::ValidatesLength
|
|
|
|
# include CouchRest::Validation::ValidatesWithin
|
|
|
|
include CouchRest::Validation::ValidatesIsNumber
|
|
|
|
include CouchRest::Validation::ValidatesWithMethod
|
|
|
|
# include CouchRest::Validation::ValidatesWithBlock
|
|
|
|
# include CouchRest::Validation::ValidatesIsUnique
|
2009-02-06 02:06:12 +01:00
|
|
|
include CouchRest::Validation::AutoValidate
|
2009-02-04 02:33:31 +01:00
|
|
|
|
2009-02-03 04:21:32 +01:00
|
|
|
# Return the set of contextual validators or create a new one
|
|
|
|
#
|
|
|
|
def validators
|
|
|
|
@validations ||= ContextualValidators.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Clean up the argument list and return a opts hash, including the
|
|
|
|
# merging of any default opts. Set the context to default if none is
|
|
|
|
# provided. Also allow :context to be aliased to :on, :when & group
|
|
|
|
#
|
|
|
|
def opts_from_validator_args(args, defaults = nil)
|
|
|
|
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
|
|
|
context = :default
|
|
|
|
context = opts[:context] if opts.has_key?(:context)
|
|
|
|
context = opts.delete(:on) if opts.has_key?(:on)
|
|
|
|
context = opts.delete(:when) if opts.has_key?(:when)
|
|
|
|
context = opts.delete(:group) if opts.has_key?(:group)
|
|
|
|
opts[:context] = context
|
2009-02-10 11:15:39 +01:00
|
|
|
opts.merge!(defaults) unless defaults.nil?
|
2009-02-03 04:21:32 +01:00
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
# Given a new context create an instance method of
|
|
|
|
# valid_for_<context>? which simply calls valid?(context)
|
|
|
|
# if it does not already exist
|
|
|
|
#
|
|
|
|
def create_context_instance_methods(context)
|
|
|
|
name = "valid_for_#{context.to_s}?" # valid_for_signup?
|
|
|
|
if !self.instance_methods.include?(name)
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name} # def valid_for_signup?
|
|
|
|
valid?('#{context.to_s}'.to_sym) # valid?('signup'.to_sym)
|
|
|
|
end # end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new validator of the given klazz and push it onto the
|
|
|
|
# requested context for each of the attributes in the fields list
|
|
|
|
#
|
|
|
|
def add_validator_to_context(opts, fields, klazz)
|
|
|
|
fields.each do |field|
|
2009-02-10 11:15:39 +01:00
|
|
|
validator = klazz.new(field.to_sym, opts)
|
2009-02-03 04:21:32 +01:00
|
|
|
if opts[:context].is_a?(Symbol)
|
|
|
|
unless validators.context(opts[:context]).include?(validator)
|
|
|
|
validators.context(opts[:context]) << validator
|
|
|
|
create_context_instance_methods(opts[:context])
|
|
|
|
end
|
|
|
|
elsif opts[:context].is_a?(Array)
|
|
|
|
opts[:context].each do |c|
|
|
|
|
unless validators.context(c).include?(validator)
|
|
|
|
validators.context(c) << validator
|
|
|
|
create_context_instance_methods(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # module ClassMethods
|
|
|
|
end # module Validation
|
|
|
|
|
|
|
|
end # module CouchRest
|