Class: Schemacop::V3::StringNode

Inherits:
Node
  • Object
show all
Defined in:
lib/schemacop/v3/string_node.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  min_length
  max_length
  format
].freeze
FORMAT_PATTERNS =

rubocop:disable Layout/LineLength

{
  date:           /^([0-9]{4})-?(1[0-2]|0[1-9])-?(3[01]|0[1-9]|[12][0-9])$/,
  'date-time':    /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/,
  time:           /^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/,
  email:          URI::MailTo::EMAIL_REGEXP,
  boolean:        /^(true|false|0|1)$/,
  binary:         nil,
  symbol:         nil,
  integer:        /^-?[0-9]+$/,
  number:         /^-?[0-9]+(\.[0-9]+)?$/,
  'integer-list': /^(-?[0-9]+)(,-?[0-9]+)*$/
}.freeze

Instance Attribute Summary

Attributes inherited from Node

#as, #default, #description, #name, #options, #parent, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#children, #create, create, dsl_methods, #dsl_node, #dsl_scm, #initialize, #required?, resolve_class, #schemas, supports_children, supports_children_options, #used_external_schemas, #validate

Constructor Details

This class inherits a constructor from Schemacop::V3::Node

Class Method Details

.allowed_optionsObject

rubocop:enable Layout/LineLength



25
26
27
# File 'lib/schemacop/v3/string_node.rb', line 25

def self.allowed_options
  super + ATTRIBUTES + %i[format_options pattern allow_blank]
end

Instance Method Details

#_validate(data, result:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/schemacop/v3/string_node.rb', line 41

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_typesObject



29
30
31
# File 'lib/schemacop/v3/string_node.rb', line 29

def allowed_types
  { String => :string }
end

#as_jsonObject



33
34
35
36
37
38
39
# File 'lib/schemacop/v3/string_node.rb', line 33

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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/schemacop/v3/string_node.rb', line 85

def cast(value)
  if !value.nil?
    to_cast = value
  elsif default.present?
    to_cast = default
  else
    return nil
  end

  case options[:format]
  when :boolean
    %w[true 1].include?(to_cast)
  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 :'integer-list'
    return to_cast.split(',').map(&:to_i)
  when :symbol
    return to_cast.to_sym
  else
    return to_cast
  end
end