Class: ZodRails::Introspection::ValidationInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/zod_rails/introspection/validation_info.rb

Constant Summary collapse

CONDITIONAL_KEYS =
%i[if unless on].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, attribute:, options:, conditional:) ⇒ ValidationInfo

Returns a new instance of ValidationInfo.



10
11
12
13
14
15
16
# File 'lib/zod_rails/introspection/validation_info.rb', line 10

def initialize(kind:, attribute:, options:, conditional:)
  @kind = kind
  @attribute = attribute
  @options = options
  @conditional = conditional
  freeze
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



8
9
10
# File 'lib/zod_rails/introspection/validation_info.rb', line 8

def attribute
  @attribute
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/zod_rails/introspection/validation_info.rb', line 8

def kind
  @kind
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/zod_rails/introspection/validation_info.rb', line 8

def options
  @options
end

Class Method Details

.from_validator(validator, attribute) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/zod_rails/introspection/validation_info.rb', line 18

def self.from_validator(validator, attribute)
  opts = validator.options.dup
  conditional = CONDITIONAL_KEYS.any? { |key| opts.key?(key) }

  new(
    kind: validator.kind,
    attribute: attribute,
    options: opts.except(*CONDITIONAL_KEYS),
    conditional: conditional
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



34
35
36
37
38
39
40
# File 'lib/zod_rails/introspection/validation_info.rb', line 34

def ==(other)
  other.is_a?(self.class) &&
    kind == other.kind &&
    attribute == other.attribute &&
    options == other.options &&
    conditional? == other.conditional?
end

#conditional?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/zod_rails/introspection/validation_info.rb', line 30

def conditional?
  @conditional
end

#hashObject



43
44
45
# File 'lib/zod_rails/introspection/validation_info.rb', line 43

def hash
  [self.class, kind, attribute, options, @conditional].hash
end