Class: MetadataPresenter::BaseValidator Abstract
- Inherits:
-
Object
- Object
- MetadataPresenter::BaseValidator
- Defined in:
- app/validators/metadata_presenter/base_validator.rb
Overview
Abstract base class for validation utilities. Provides an interface for implementing validation from the metadata.
The Base validator expects the subclass to implement only #invalid_answer? as long the conventions are followed:
-
The default metadata for error messages follows the “error.name_of_the_class_without_validator”
-
The class should have the same name as the schema e.g ‘required’ will lookup for RequiredValidator.
On the example below the base validator will look for the custom message on “errors” -> “grogu” -> “any” and if there is none, then will look for the default message on default metadata as “error.grogu”.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#answers ⇒ Hash
readonly
The user answers.
-
#component ⇒ MetadataPresenter::Component
readonly
Component object from the metadata.
-
#page ⇒ MetadataPresenter::Page
readonly
Page object from the metadata.
Instance Method Summary collapse
-
#custom_error_message ⇒ String
The custom message will be lookup from the schema key on the metadata.
-
#default_error_message ⇒ String
The default error message will be look using the schema key.
-
#error_message_hash ⇒ Object
Error message hash that will be interpolate with the custom message or the default metadata.
-
#initialize(page:, answers:, component:) ⇒ BaseValidator
constructor
A new instance of BaseValidator.
-
#invalid_answer? ⇒ TrueClass, FalseClass
Needs to be implemented on the subclass.
-
#schema_key ⇒ String
The convention to be looked on the metadata is by the name of the class.
- #valid? ⇒ Boolean
Constructor Details
#initialize(page:, answers:, component:) ⇒ BaseValidator
Returns a new instance of BaseValidator.
38 39 40 41 42 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 38 def initialize(page:, answers:, component:) @page = page @answers = answers @component = component end |
Instance Attribute Details
#answers ⇒ Hash (readonly)
Returns the user answers.
33 34 35 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 33 def answers @answers end |
#component ⇒ MetadataPresenter::Component (readonly)
Returns component object from the metadata.
36 37 38 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 36 def component @component end |
#page ⇒ MetadataPresenter::Page (readonly)
Returns page object from the metadata.
30 31 32 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 30 def page @page end |
Instance Method Details
#custom_error_message ⇒ String
The custom message will be lookup from the schema key on the metadata. Assuming for example that the schema key is ‘grogu’ then the message will lookup for ‘errors.grogu.any’.
59 60 61 62 63 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 59 def = component.dig('errors', schema_key, 'any') % if .present? end |
#default_error_message ⇒ String
The default error message will be look using the schema key. Assuming the schema key is ‘grogu’ then the default message will look for ‘error.grogu.value’.
is not present
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 72 def = "error.#{schema_key}" = Rails .application .config .[] if .present? ['value'] % else raise NoDefaultMessage, "No default message found for key '#{default_error_message_key}'." end end |
#error_message_hash ⇒ Object
Error message hash that will be interpolate with the custom message or the default metadata
The message could include ‘%control’ to add the label name. Or for the GroguValidator will be ‘%grogu’ and the value setup in the metadata.
112 113 114 115 116 117 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 112 def { control: component.label, schema_key.to_sym => component.validation[schema_key] } end |
#invalid_answer? ⇒ TrueClass, FalseClass
Needs to be implemented on the subclass
91 92 93 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 91 def invalid_answer? raise NotImplementedError end |
#schema_key ⇒ String
The convention to be looked on the metadata is by the name of the class. E.g the GroguValidator will look for ‘grogu’ on the metadata. Overwrite this method if the validator doesn’t follow the convetions.
default metadata
102 103 104 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 102 def schema_key @schema_key ||= self.class.name.demodulize.gsub('Validator', '').underscore end |
#valid? ⇒ Boolean
44 45 46 47 48 49 50 51 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 44 def valid? if invalid_answer? = || page.errors.add(component.id, ) end page.errors.blank? end |