Sha256: a6b7364af1f285cd82a23cf01623418c5965ae1d1ce42fe9aec4891b23c9e66a

Contents?: true

Size: 1.05 KB

Versions: 40

Compression:

Stored size: 1.05 KB

Contents

module Schemacop
  module V2
    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
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
schemacop-3.0.13 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.12 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.11 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.10 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.9 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.8 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.7 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.6 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.5 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.4 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.3 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.2 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.1 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0.rc5 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0.rc4 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0.rc3 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0.rc2 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0.rc1 lib/schemacop/v2/validator/string_validator.rb
schemacop-3.0.0.rc0 lib/schemacop/v2/validator/string_validator.rb