Class: Schemacop::V3::NumericNode Abstract
- Defined in:
- lib/schemacop/v3/numeric_node.rb
Overview
This class is abstract.
Direct Known Subclasses
Constant Summary collapse
- ATTRIBUTES =
%i[ minimum maximum multiple_of exclusive_minimum exclusive_maximum ].freeze
Instance Attribute Summary
Attributes inherited from Node
#as, #default, #description, #name, #options, #parent, #require_key, #title
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from Node
#allowed_types, #as_json, #cast, #children, create, #create, dsl_methods, #dsl_node, #dsl_scm, #init, #initialize, #require_key?, #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
13 14 15 |
# File 'lib/schemacop/v3/numeric_node.rb', line 13 def self. super + ATTRIBUTES + %i[cast_str] end |
Instance Method Details
#_validate(data, result:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/schemacop/v3/numeric_node.rb', line 35 def _validate(data, result:) super_data = super return if super_data.nil? # Validate minimum # if [:minimum] && super_data < [:minimum] result.error "Value must have a minimum of #{[:minimum]}." end if [:exclusive_minimum] && super_data <= [:exclusive_minimum] result.error "Value must have an exclusive minimum of #{[:exclusive_minimum]}." end # Validate maximum # if [:maximum] && super_data > [:maximum] result.error "Value must have a maximum of #{[:maximum]}." end if [:exclusive_maximum] && super_data >= [:exclusive_maximum] result.error "Value must have an exclusive maximum of #{[:exclusive_maximum]}." end # Validate multiple of # if [:multiple_of] && !compare_float((super_data % [:multiple_of]), 0.0) result.error "Value must be a multiple of #{[:multiple_of]}." end end |
#process_json(attrs, json) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/schemacop/v3/numeric_node.rb', line 17 def process_json(attrs, json) if context.swagger_json? if [:exclusive_minimum] json[:minimum] = [:exclusive_minimum] json[:exclusiveMinimum] = true end if [:exclusive_maximum] json[:maximum] = [:exclusive_maximum] json[:exclusiveMaximum] = true end attrs -= %i[exclusive_minimum exclusive_maximum] end super attrs, json end |
#validate_self ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/schemacop/v3/numeric_node.rb', line 63 def validate_self # Check that the options have the correct type ATTRIBUTES.each do |attribute| next if [attribute].nil? next unless allowed_types.keys.none? { |c| [attribute].send(type_assertion_method, c) } collection = allowed_types.values.map { |t| "\"#{t}\"" }.uniq.sort.join(' or ') fail "Option \"#{attribute}\" must be a #{collection}" end if [:minimum] && [:maximum] && [:minimum] > [:maximum] fail 'Option "minimum" can\'t be greater than "maximum".' end if [:exclusive_minimum] && [:exclusive_maximum]\ && [:exclusive_minimum] > [:exclusive_maximum] fail 'Option "exclusive_minimum" can\'t be greater than "exclusive_maximum".' end if [:multiple_of]&.zero? fail 'Option "multiple_of" can\'t be 0.' end end |