Sha256: f95910f946b91d6bbecd5302fa1adb7c52da88e0dd23db7ba36ac10409353045

Contents?: true

Size: 999 Bytes

Versions: 14

Compression:

Stored size: 999 Bytes

Contents

module Schemacop
  class StringValidator < Node
    register symbols: :string, klasses: String

    option :min
    option :max

    def initialize(options = {})
      super(options)

      validate_options!
    end

    def validate(data, collector)
      super

      if option?(:min) && data.size < option(:min)
        collector.error "String must be longer (>=) than #{option(:min)} characters."
      end
      if option?(:max) && data.size > option(:max)
        collector.error "String must be shorter (<=) than #{option(:max)} characters."
      end
    end

    protected

    def validate_options!
      option_schema = Schema.new :integer, min: 0

      if option?(:min) && option_schema.invalid?(option(:min))
        fail Exceptions::InvalidSchemaError, 'String option :min must be an integer >= 0.'
      elsif option?(:max) && option_schema.invalid?(option(:max))
        fail Exceptions::InvalidSchemaError, 'String option :max must be an integer >= 0.'
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
schemacop-2.4.7 lib/schemacop/validator/string_validator.rb
schemacop-2.4.6 lib/schemacop/validator/string_validator.rb
schemacop-2.4.5 lib/schemacop/validator/string_validator.rb
schemacop-2.4.4 lib/schemacop/validator/string_validator.rb
schemacop-2.4.3 lib/schemacop/validator/string_validator.rb
schemacop-2.4.2 lib/schemacop/validator/string_validator.rb
schemacop-2.4.1 lib/schemacop/validator/string_validator.rb
schemacop-2.4.0 lib/schemacop/validator/string_validator.rb
schemacop-2.3.2 lib/schemacop/validator/string_validator.rb
schemacop-2.3.1 lib/schemacop/validator/string_validator.rb
schemacop-2.3.0 lib/schemacop/validator/string_validator.rb
schemacop-2.2.0 lib/schemacop/validator/string_validator.rb
schemacop-2.1.0 lib/schemacop/validator/string_validator.rb
schemacop-2.0.0 lib/schemacop/validator/string_validator.rb