Class: Schemacop::V3::StringNode
- 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]+)?$/ }.freeze
Instance Attribute Summary
Attributes inherited from Node
#as, #default, #description, #name, #options, #parent, #title
Class Method Summary collapse
-
.allowed_options ⇒ Object
rubocop:enable Layout/LineLength.
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_options ⇒ Object
rubocop:enable Layout/LineLength
24 25 26 |
# File 'lib/schemacop/v3/string_node.rb', line 24 def self. super + ATTRIBUTES + %i[format_options pattern allow_blank] end |
Instance Method Details
#_validate(data, result:) ⇒ Object
40 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 |
# File 'lib/schemacop/v3/string_node.rb', line 40 def _validate(data, result:) super_data = super # Validate blank # if [: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 [:min_length] && length < [:min_length] result.error "String is #{length} characters long but must be at least #{[:min_length]}." end if [:max_length] && length > [:max_length] result.error "String is #{length} characters long but must be at most #{[:max_length]}." end # Validate pattern # if (pattern = [:pattern]) unless [: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 [:format] && FORMAT_PATTERNS.include?([:format]) pattern = FORMAT_PATTERNS[[:format]] if pattern && !super_data.match?(pattern) result.error "String does not match format #{[:format].to_s.inspect}." elsif [:format_options] && Node.resolve_class([:format]) node = create([:format], **[:format_options]) node._validate(cast(super_data), result: result) end end end |
#allowed_types ⇒ Object
28 29 30 |
# File 'lib/schemacop/v3/string_node.rb', line 28 def allowed_types { String => :string } end |
#as_json ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/schemacop/v3/string_node.rb', line 32 def as_json json = { type: :string } if [:pattern] json[:pattern] = V3.sanitize_exp(Regexp.compile([:pattern])) end process_json(ATTRIBUTES, json) end |
#cast(value) ⇒ Object
84 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 |
# 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 [:format] when :boolean return to_cast == 'true' || to_cast == '1' 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 |