Class: Castkit::Validators::StringValidator
- Defined in:
- lib/castkit/validators/string_validator.rb
Overview
Validates that a value is a String and optionally conforms to a format.
Supports format validation using a Regexp or a custom Proc.
Instance Method Summary collapse
-
#call(value, options:, context:) ⇒ void
Validates the string value.
Methods inherited from Base
Instance Method Details
#call(value, options:, context:) ⇒ void
This method returns an undefined value.
Validates the string value.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/castkit/validators/string_validator.rb', line 18 def call(value, options:, context:) return type_error!(:string, value, context: context) unless value.is_a?(::String) return unless [:format] case [:format] when Regexp raise Castkit::AttributeError, "#{context} must match format" unless value =~ [:format] when Proc raise Castkit::AttributeError, "#{context} failed format validation" unless [:format].call(value) else raise Castkit::AttributeError, "#{context} has unsupported format validator: #{[:format].class}" end end |