Class: ActionSpec::Schema::ObjectOf

Inherits:
Base
  • Object
show all
Defined in:
lib/action_spec/schema/object_of.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#blank, #default, #description, #enum, #example, #examples, #length, #pattern, #range

Instance Method Summary collapse

Methods inherited from Base

#blank_allowed?, #blank_value, #validate_constraints

Constructor Details

#initialize(fields, options = {}) ⇒ ObjectOf

Returns a new instance of ObjectOf.



8
9
10
11
# File 'lib/action_spec/schema/object_of.rb', line 8

def initialize(fields, options = {})
  super(options)
  @fields = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/action_spec/schema/object_of.rb', line 6

def fields
  @fields
end

Instance Method Details

#cast(value, context:, coerce:, result:, path:, field: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/action_spec/schema/object_of.rb', line 13

def cast(value, context:, coerce:, result:, path:, field: nil)
  source = normalize_source(value, result:, path:, field:, context:, invalid_value: value)
  return Schema::Missing if source.equal?(Schema::Missing)

  output = ActiveSupport::HashWithIndifferentAccess.new
  fields.each_value do |field|
    resolved = Resolver.new(
      field:,
      source:,
      context:,
      coerce:,
      result:,
      path:
    ).resolve
    output[field.output_name] = resolved unless resolved.equal?(Schema::Missing)
  end
  output.presence || (source.present? ? output : Schema::Missing)
end

#copyObject



36
37
38
# File 'lib/action_spec/schema/object_of.rb', line 36

def copy
  self.class.new(fields.transform_values(&:copy), default:, enum:, range:, pattern:, length:, blank:, desc: description, example:, examples:)
end

#custom_validation?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/action_spec/schema/object_of.rb', line 40

def custom_validation?
  custom_validation_fields.any?
end

#custom_validation_fieldsObject



44
45
46
# File 'lib/action_spec/schema/object_of.rb', line 44

def custom_validation_fields
  @custom_validation_fields ||= fields.each_value.select(&:custom_validation?).freeze
end

#materialize_missing(context:, coerce:, result:, path:) ⇒ Object



32
33
34
# File 'lib/action_spec/schema/object_of.rb', line 32

def materialize_missing(context:, coerce:, result:, path:)
  cast({}, context:, coerce:, result:, path:, field: nil)
end