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
AddressValidator, AutocompleteValidator, BaseUploadValidator, CatchAllValidator, DateValidator, EmailValidator, MaxLengthValidator, MaxWordValidator, MinLengthValidator, MinWordValidator, NumberValidator, PatternValidator, PostcodeValidator, RequiredValidator
Instance Attribute Summary collapse
-
#component ⇒ MetadataPresenter::Component
readonly
Component object from the metadata.
-
#page_answers ⇒ MetadataPresenter::PageAnswers
readonly
Page answers object.
Instance Method Summary collapse
-
#allow_blank? ⇒ Boolean
Method signature to be overwrite in the subclass if you do not want to allow blank values.
-
#custom_error_message ⇒ String
The custom message will be lookup from the schema key on the metadata.
-
#default_error_message(params = {}) ⇒ String
The default error message will be look using the schema key.
-
#error_key ⇒ String
The key to use when retrieving localised error messages.
-
#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.
-
#user_answer ⇒ String
User answer for the specific component.
- #valid? ⇒ Boolean
-
#validation_value ⇒ Object
The validation configuration value Override if additional formatting of the value is required.
Constructor Details
#initialize(page_answers:, component:) ⇒ BaseValidator
35 36 37 38 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 35 def initialize(page_answers:, component:) @page_answers = page_answers @component = component end |
Instance Attribute Details
#component ⇒ MetadataPresenter::Component (readonly)
33 34 35 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 33 def component @component end |
#page_answers ⇒ MetadataPresenter::PageAnswers (readonly)
30 31 32 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 30 def page_answers @page_answers end |
Instance Method Details
#allow_blank? ⇒ Boolean
Method signature to be overwrite in the subclass if you do not want to allow blank values. We should not allow blank when performing the required validation.
150 151 152 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 150 def allow_blank? user_answer.blank? && !self.class.name.demodulize.include?('RequiredValidator') end |
#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’.
If there is a custom message defined in the locales, it will take precedence. Example: ‘presenter.components.upload.errors.required`
60 61 62 63 64 65 66 67 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 60 def = I18n.t( "presenter.components.#{component.type}.errors.#{schema_key}", default: component.dig('errors', schema_key, 'any') ) % if .present? end |
#default_error_message(params = {}) ⇒ 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:en’ or ‘error.grogu.value:cy’.
is not present
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 82 def (params = {}) = "error.#{schema_key}" = Rails.application .config .[] if .present? [error_key] % .merge(params) else raise NoDefaultMessage, "No default '#{error_key}' message found for key '#{default_error_message_key}'." end end |
#error_key ⇒ String
The key to use when retrieving localised error messages. This key will be ‘value:locale` i.e. `value:en` for English or `value:cy` for Welsh.
121 122 123 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 121 def error_key @error_key ||= ['value', I18n.locale].join(':').freeze 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.
131 132 133 134 135 136 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 131 def { control: component.humanised_title, schema_key.to_sym => validation_value } end |
#invalid_answer? ⇒ TrueClass, FalseClass
Needs to be implemented on the subclass
100 101 102 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 100 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
111 112 113 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 111 def schema_key @schema_key ||= self.class.name.demodulize.gsub('Validator', '').underscore end |
#user_answer ⇒ String
71 72 73 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 71 def user_answer page_answers.send(component.name) end |
#valid? ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 40 def valid? return true if allow_blank? if invalid_answer? = || page_answers.errors.add(component.id, ) end page_answers.errors.blank? end |
#validation_value ⇒ Object
The validation configuration value Override if additional formatting of the value is required
140 141 142 |
# File 'app/validators/metadata_presenter/base_validator.rb', line 140 def validation_value component.validation[schema_key] end |