Class: ActionSpec::Schema::Field
- Inherits:
-
Object
- Object
- ActionSpec::Schema::Field
- Defined in:
- lib/action_spec/schema/field.rb
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#px_key ⇒ Object
readonly
Returns the value of attribute px_key.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
-
#validate ⇒ Object
readonly
Returns the value of attribute validate.
Instance Method Summary collapse
- #add_error(result, path:, type:, value:, context: nil, **options) ⇒ Object
- #copy ⇒ Object
- #custom_validation? ⇒ Boolean
- #default_value ⇒ Object
-
#initialize(name:, required:, schema:, transform: nil, validate: nil, px_key: nil, scopes: [], error_message: nil) ⇒ Field
constructor
A new instance of Field.
- #output_name ⇒ Object
- #required? ⇒ Boolean
- #transform_value(value, context: nil) ⇒ Object
- #validate_value(value, context: nil) ⇒ Object
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 = end |
Instance Attribute Details
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
6 7 8 |
# File 'lib/action_spec/schema/field.rb', line 6 def @error_message end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/action_spec/schema/field.rb', line 6 def name @name end |
#px_key ⇒ Object (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 |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
6 7 8 |
# File 'lib/action_spec/schema/field.rb', line 6 def schema @schema end |
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
6 7 8 |
# File 'lib/action_spec/schema/field.rb', line 6 def scopes @scopes end |
#transform ⇒ Object (readonly)
Returns the value of attribute transform.
6 7 8 |
# File 'lib/action_spec/schema/field.rb', line 6 def transform @transform end |
#validate ⇒ Object (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, **) result.add_error(path.join("."), type, message: (type, value, context:), **) end |
#copy ⇒ Object
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
50 51 52 |
# File 'lib/action_spec/schema/field.rb', line 50 def custom_validation? @custom_validation ||= validate.present? || schema.custom_validation? end |
#default_value ⇒ Object
23 24 25 |
# File 'lib/action_spec/schema/field.rb', line 23 def default_value schema.default end |
#output_name ⇒ Object
27 28 29 |
# File 'lib/action_spec/schema/field.rb', line 27 def output_name px_key || name end |
#required? ⇒ 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 |