class Schemacop::V3::StringNode

Constants

ATTRIBUTES
FORMAT_PATTERNS

rubocop:disable Layout/LineLength

Public Class Methods

allowed_options() click to toggle source

rubocop:enable Layout/LineLength

Calls superclass method Schemacop::V3::Node::allowed_options
# File lib/schemacop/v3/string_node.rb, line 24
def self.allowed_options
  super + ATTRIBUTES + %i[format_options pattern allow_blank]
end

Public Instance Methods

_validate(data, result:) click to toggle source
Calls superclass method Schemacop::V3::Node#_validate
# File lib/schemacop/v3/string_node.rb, line 40
def _validate(data, result:)
  super_data = super

  # Validate blank #
  if options[:allow_blank].is_a?(FalseClass) && super_data.blank?
    result.error 'String is blank but must not be blank!'
  end

  return if super_data.nil?

  # Validate length #
  length = super_data.size

  if options[:min_length] && length < options[:min_length]
    result.error "String is #{length} characters long but must be at least #{options[:min_length]}."
  end

  if options[:max_length] && length > options[:max_length]
    result.error "String is #{length} characters long but must be at most #{options[:max_length]}."
  end

  # Validate pattern #
  if (pattern = options[:pattern])
    unless options[:pattern].is_a?(Regexp)
      pattern = Regexp.compile(pattern)
    end

    unless super_data.match?(pattern)
      result.error "String does not match pattern #{V3.sanitize_exp(pattern).inspect}."
    end
  end

  # Validate format #
  if options[:format] && FORMAT_PATTERNS.include?(options[:format])
    pattern = FORMAT_PATTERNS[options[:format]]
    if pattern && !super_data.match?(pattern)
      result.error "String does not match format #{options[:format].to_s.inspect}."
    elsif options[:format_options] && Node.resolve_class(options[:format])
      node = create(options[:format], **options[:format_options])
      node._validate(cast(super_data), result: result)
    end
  end
end
allowed_types() click to toggle source
# File lib/schemacop/v3/string_node.rb, line 28
def allowed_types
  { String => :string }
end
as_json() click to toggle source
# File lib/schemacop/v3/string_node.rb, line 32
def as_json
  json = { type: :string }
  if options[:pattern]
    json[:pattern] = V3.sanitize_exp(Regexp.compile(options[:pattern]))
  end
  process_json(ATTRIBUTES, json)
end
cast(value) click to toggle source
# File lib/schemacop/v3/string_node.rb, line 84
def cast(value)
  if !value.nil?
    to_cast = value
  elsif default.present?
    to_cast = default
  else
    return nil
  end

  case options[:format]
  when :boolean
    return to_cast == 'true'
  when :date
    return Date.parse(to_cast)
  when :'date-time'
    return DateTime.parse(to_cast)
  when :time
    Time.parse(to_cast)
  when :integer
    return Integer(to_cast)
  when :number
    return Float(to_cast)
  when :symbol
    return to_cast.to_sym
  else
    return to_cast
  end
end

Protected Instance Methods

init() click to toggle source
# File lib/schemacop/v3/string_node.rb, line 115
def init
  if options.include?(:format)
    options[:format] = options[:format].to_s.dasherize.to_sym
  end
end
validate_self() click to toggle source
# File lib/schemacop/v3/string_node.rb, line 121
def validate_self
  if options.include?(:format) && !FORMAT_PATTERNS.include?(options[:format])
    fail "Format #{options[:format].to_s.inspect} is not supported."
  end

  unless options[:min_length].nil? || options[:min_length].is_a?(Integer)
    fail 'Option "min_length" must be an "integer"'
  end

  unless options[:max_length].nil? || options[:max_length].is_a?(Integer)
    fail 'Option "max_length" must be an "integer"'
  end

  if options[:min_length] && options[:max_length] && options[:min_length] > options[:max_length]
    fail 'Option "min_length" can\'t be greater than "max_length".'
  end

  if options[:pattern]
    unless options[:pattern].is_a?(String) || options[:pattern].is_a?(Regexp)
      fail 'Option "pattern" must be a string or Regexp.'
    end

    begin
      Regexp.compile(options[:pattern])
    rescue RegexpError => e
      fail "Option \"pattern\" can't be parsed: #{e.message}."
    end
  end
end