lib/respect/array_schema.rb
def validate(object)
      # Handle nil case.
      if object.nil?
        if allow_nil?
          self.sanitized_object = nil
          return true
        else
          raise ValidationError, "object is nil but this #{self.class} does not allow nil"
        end
      end
      # Validate type.
      unless object.is_a?(Array)
        raise ValidationError, "object is not an array but a #{object.class}"
      end
      # At this point we are sure @item and (@items or @extra_items) cannot be
      # defined both. (see the setters).
      sanitized_object = []
      # Validate expected item.
      if @item
        if options[:min_size] && object.size < options[:min_size]
          raise ValidationError,
                "expected at least #{options[:min_size]} item(s) but got #{object.size}"
        end
        if options[:max_size] && object.size > options[:max_size]
          raise ValidationError,
                "expected at most #{options[:min_size]} item(s) but got #{object.size}"
        end
        object.each_with_index do |item, i|
          validate_item(i, @item, object, sanitized_object)
        end
      end
      # Validate object items count.
      if @items || @extra_items
        if @extra_items
          min_size = @items ? @items.size : 0
          unless min_size <= object.size
            raise ValidationError,
                  "array size should be at least #{min_size} but is #{object.size}"
          end
        else
          if @items.size != object.size
            raise ValidationError,
                  "array size should be #{@items.size} but is #{object.size}"
          end
        end
      end
      # Validate expected multiple items.
      if @items
        @items.each_with_index do |schema, i|
          validate_item(i, schema, object, sanitized_object)
        end
      end
      # Validate extra items.
      if @extra_items
        @extra_items.each_with_index do |schema, i|
          if @items.size + i < object.size
            validate_item(@items.size + i, schema, object, sanitized_object)
          end
        end
      end
      # Validate all items are unique.
      if options[:uniq]
        s = Set.new
        object.each_with_index do |e, i|
          if s.add?(e).nil?
            raise ValidationError,
                  "duplicated item number #{i}"
          end
        end
      end
      self.sanitized_object = sanitized_object
      true
    rescue ValidationError => e
      # Reset sanitized object.
      self.sanitized_object = nil
      raise e
    end
def validate(object)
      # Handle nil case.
      if object.nil?
        if allow_nil?
          self.sanitized_object = nil
          return true
        else
          raise ValidationError, "object is nil but this #{self.class} does not allow nil"
        end
      end
      # Validate type.
      unless object.is_a?(Array)
        raise ValidationError, "object is not an array but a #{object.class}"
      end
      # At this point we are sure @item and (@items or @extra_items) cannot be
      # defined both. (see the setters).
      sanitized_object = []
      # Validate expected item.
      if @item
        if options[:min_size] && object.size < options[:min_size]
          raise ValidationError,
                "expected at least #{options[:min_size]} item(s) but got #{object.size}"
        end
        if options[:max_size] && object.size > options[:max_size]
          raise ValidationError,
                "expected at most #{options[:min_size]} item(s) but got #{object.size}"
        end
        object.each_with_index do |item, i|
          validate_item(i, @item, object, sanitized_object)
        end
      end
      # Validate object items count.
      if @items || @extra_items
        if @extra_items
          min_size = @items ? @items.size : 0
          unless min_size <= object.size
            raise ValidationError,
                  "array size should be at least #{min_size} but is #{object.size}"
          end
        else
          if @items.size != object.size
            raise ValidationError,
                  "array size should be #{@items.size} but is #{object.size}"
          end
        end
      end
      # Validate expected multiple items.
      if @items
        @items.each_with_index do |schema, i|
          validate_item(i, schema, object, sanitized_object)
        end
      end
      # Validate extra items.
      if @extra_items
        @extra_items.each_with_index do |schema, i|
          if @items.size + i < object.size
            validate_item(@items.size + i, schema, object, sanitized_object)
          end
        end
      end
      # Validate all items are unique.
      if options[:uniq]
        s = Set.new
        object.each_with_index do |e, i|
          if s.add?(e).nil?
            raise ValidationError,
                  "duplicated item number #{i}"
          end
        end
      end
      self.sanitized_object = sanitized_object
      true
    rescue ValidationError => e
      # Reset sanitized object.
      self.sanitized_object = nil
      raise e
    end
def validate ( object ) # Handle nil case. if object . nil? if allow_nil? self . sanitized_object = nil return true else raise ValidationError , "object is nil but this #{self.class} does not allow nil" end end # Validate type. unless object . is_a? ( Array ) raise ValidationError , "object is not an array but a #{object.class}" end # At this point we are sure @item and (@items or @extra_items) cannot be # defined both. (see the setters). sanitized_object = [ ] # Validate expected item. if @item if options [ :min_size ] && object . size < options [ :min_size ] raise ValidationError , "expected at least #{options[:min_size]} item(s) but got #{object.size}" end if options [ :max_size ] && object . size > options [ :max_size ] raise ValidationError , "expected at most #{options[:min_size]} item(s) but got #{object.size}" end object . each_with_index do | item , i | validate_item ( i , @item , object , sanitized_object ) end end # Validate object items count. if @items || @extra_items if @extra_items min_size = @items ? @items . size : 0 unless min_size <= object . size raise ValidationError , "array size should be at least #{min_size} but is #{object.size}" end else if @items . size != object . size raise ValidationError , "array size should be #{@items.size} but is #{object.size}" end end end # Validate expected multiple items. if @items @items . each_with_index do | schema , i | validate_item ( i , schema , object , sanitized_object ) end end # Validate extra items. if @extra_items @extra_items . each_with_index do | schema , i | if @items . size + i < object . size validate_item ( @items . size + i , schema , object , sanitized_object ) end end end # Validate all items are unique. if options [ :uniq ] s = Set . new object . each_with_index do | e , i | if s . add? ( e ) . nil? raise ValidationError , "duplicated item number #{i}" end end end self . sanitized_object = sanitized_object true rescue ValidationError => e # Reset sanitized object. self . sanitized_object = nil raise e end
Overwritten method. See {Schema#validate}
Overwritten method . See { Schema#validate }