Class: Jsapi::Meta::Schema::Validation::MaxLength

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/meta/schema/validation/max_length.rb

Instance Attribute Summary

Attributes inherited from Base

#value

Instance Method Summary collapse

Methods inherited from Base

keyword, #to_json_schema_validation, #to_openapi_validation

Constructor Details

#initialize(value) ⇒ MaxLength

Returns a new instance of MaxLength.



8
9
10
11
12
13
14
# File 'lib/jsapi/meta/schema/validation/max_length.rb', line 8

def initialize(value)
  unless value.respond_to?(:<=)
    raise ArgumentError, "invalid max length: #{value.inspect}"
  end

  super
end

Instance Method Details

#validate(value, errors) ⇒ Object



16
17
18
19
20
21
# File 'lib/jsapi/meta/schema/validation/max_length.rb', line 16

def validate(value, errors)
  return true if value.to_s.length <= self.value

  errors.add(:base, :too_long, count: self.value)
  false
end