Sha256: 6f16b25ce38b73eae1dbdbaf2f940b85d95e66735e1764994a0e56f1eda080c1

Contents?: true

Size: 860 Bytes

Versions: 6

Compression:

Stored size: 860 Bytes

Contents

module Schemacop
  class ArrayValidator < NodeSupportingType
    register symbols: :array, klasses: Array

    option :min # Minimal number of elements
    option :max # Maximal number of elements
    option :nil # Whether to allow nil values

    def initialize(*args)
      super
      type(:nil) if option(:nil)
    end

    def validate(data, collector)
      validate_custom_check(data, collector)

      if option?(:min) && data.size < option(:min)
        collector.error "Array must have more (>=) than #{option(:min)} elements."
      end
      if option?(:max) && data.size > option(:max)
        collector.error "Array must have less (<=) than #{option(:max)} elements."
      end
      data.each_with_index do |entry, index|
        collector.path("[#{index}]") do
          validate_types(entry, collector)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
schemacop-2.3.2 lib/schemacop/validator/array_validator.rb
schemacop-2.3.1 lib/schemacop/validator/array_validator.rb
schemacop-2.3.0 lib/schemacop/validator/array_validator.rb
schemacop-2.2.0 lib/schemacop/validator/array_validator.rb
schemacop-2.1.0 lib/schemacop/validator/array_validator.rb
schemacop-2.0.0 lib/schemacop/validator/array_validator.rb