Class: ActionSpec::Schema::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, required:, schema:, transform: nil, validate: nil, px_key: nil, scopes: [], error_message: nil) ⇒ Field

Returns a new instance of Field.



8
9
10
11
12
13
14
15
16
17
# File 'lib/action_spec/schema/field.rb', line 8

def initialize(name:, required:, schema:, transform: nil, validate: nil, px_key: nil, scopes: [], error_message: nil)
  @name = name.to_sym
  @required = required
  @schema = schema
  @transform = transform
  @validate = validate
  @px_key = px_key&.to_sym
  @scopes = Array(scopes).map(&:to_sym).freeze
  @error_message = error_message
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



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

def error_message
  @error_message
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#px_keyObject (readonly)

Returns the value of attribute px_key.



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

def px_key
  @px_key
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

#scopesObject (readonly)

Returns the value of attribute scopes.



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

def scopes
  @scopes
end

#transformObject (readonly)

Returns the value of attribute transform.



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

def transform
  @transform
end

#validateObject (readonly)

Returns the value of attribute validate.



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

def validate
  @validate
end

Instance Method Details

#add_error(result, path:, type:, value:, context: nil, **options) ⇒ Object



54
55
56
# File 'lib/action_spec/schema/field.rb', line 54

def add_error(result, path:, type:, value:, context: nil, **options)
  result.add_error(path.join("."), type, message: resolve_error_message(type, value, context:), **options)
end

#copyObject



58
59
60
# File 'lib/action_spec/schema/field.rb', line 58

def copy
  self.class.new(name:, required: required?, schema: schema.copy, transform:, validate:, px_key:, scopes:, error_message:)
end

#custom_validation?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/action_spec/schema/field.rb', line 50

def custom_validation?
  @custom_validation ||= validate.present? || schema.custom_validation?
end

#default_valueObject



23
24
25
# File 'lib/action_spec/schema/field.rb', line 23

def default_value
  schema.default
end

#output_nameObject



27
28
29
# File 'lib/action_spec/schema/field.rb', line 27

def output_name
  px_key || name
end

#required?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/action_spec/schema/field.rb', line 19

def required?
  @required
end

#transform_value(value, context: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/action_spec/schema/field.rb', line 31

def transform_value(value, context: nil)
  return value if transform.nil? || value.equal?(ActionSpec::Schema::Missing)

  case transform
  when Symbol, String then apply_symbol_transform(value, context:)
  when Proc then apply_proc_transform(value, context:)
  else value
  end
end

#validate_value(value, context: nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/action_spec/schema/field.rb', line 41

def validate_value(value, context: nil)
  return true if validate.nil? || value.equal?(ActionSpec::Schema::Missing)

  case validate
  when Proc then apply_validation_proc(value, context:)
  else true
  end
end