Class: Schemacop::V3::NumericNode Abstract

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

Overview

This class is abstract.

Direct Known Subclasses

IntegerNode, NumberNode

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_optionsObject



13
14
15
# File 'lib/schemacop/v3/numeric_node.rb', line 13

def self.allowed_options
  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 options[:minimum] && super_data < options[:minimum]
    result.error "Value must have a minimum of #{options[:minimum]}."
  end

  if options[:exclusive_minimum] && super_data <= options[:exclusive_minimum]
    result.error "Value must have an exclusive minimum of #{options[:exclusive_minimum]}."
  end

  # Validate maximum #
  if options[:maximum] && super_data > options[:maximum]
    result.error "Value must have a maximum of #{options[:maximum]}."
  end

  if options[:exclusive_maximum] && super_data >= options[:exclusive_maximum]
    result.error "Value must have an exclusive maximum of #{options[:exclusive_maximum]}."
  end

  # Validate multiple of #
  if options[:multiple_of] && !compare_float((super_data % options[:multiple_of]), 0.0)
    result.error "Value must be a multiple of #{options[: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 options[:exclusive_minimum]
      json[:minimum] = options[:exclusive_minimum]
      json[:exclusiveMinimum] = true
    end

    if options[:exclusive_maximum]
      json[:maximum] = options[:exclusive_maximum]
      json[:exclusiveMaximum] = true
    end

    attrs -= %i[exclusive_minimum exclusive_maximum]
  end

  super attrs, json
end

#validate_selfObject



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 options[attribute].nil?
    next unless allowed_types.keys.none? { |c| options[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 options[:minimum] && options[:maximum] && options[:minimum] > options[:maximum]
    fail 'Option "minimum" can\'t be greater than "maximum".'
  end

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

  if options[:multiple_of]&.zero?
    fail 'Option "multiple_of" can\'t be 0.'
  end
end