Class: Schemacop::V2::StringValidator

Inherits:
Node
  • Object
show all
Defined in:
lib/schemacop/v2/validator/string_validator.rb

Instance Attribute Summary

Attributes inherited from Node

#options

Instance Method Summary collapse

Methods inherited from Node

build, class_matches?, clear_klasses, clear_symbols, #exec_block, klass, option, #option, #option?, register, #resolve_type_klass, symbol, symbol_matches?, #type_filter_matches?, #type_label, type_matches?, #type_matches?

Constructor Details

#initialize(options = {}) ⇒ StringValidator

Returns a new instance of StringValidator.



9
10
11
12
13
# File 'lib/schemacop/v2/validator/string_validator.rb', line 9

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

  validate_options!
end

Instance Method Details

#validate(data, collector) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/schemacop/v2/validator/string_validator.rb', line 15

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