Module: Cuprum::ParameterValidation::ClassMethods

Defined in:
lib/cuprum/parameter_validation.rb

Overview

Class methods for parameter validation.

Instance Method Summary collapse

Instance Method Details

#each_validationObject



19
20
21
22
23
24
25
26
27
# File 'lib/cuprum/parameter_validation.rb', line 19

def each_validation(&)
  return enum_for(:each_validation) unless block_given?

  ancestors.reverse_each do |ancestor|
    next unless ancestor.respond_to?(:validation_rules, true)

    ancestor.validation_rules.each(&)
  end
end

#validate(name, **options) ⇒ Object #validate(name, using: , **options) ⇒ Object #validate(name, **options) {|value| ... } ⇒ Object #validate(name, type, **options) ⇒ Object

Overloads:

  • #validate(name, **options) ⇒ Object

    Defines a validation for the specified parameter.

    This validation will call the #validate_$name method on the command with the value of the named parameter. If the method returns a failure message, that message is added to the failed validations.

    Parameters:

    • name (String, Symbol)

      the parameter to validate.

    • options (Hash)

      additional options to pass to the validation method.

    Options Hash (**options):

    • as (String, Symbol)

      the name of the parameter as displayed in the failure message, if any. Defaults to the value of the name parameter.

    Returns:

    • void

  • #validate(name, using: , **options) ⇒ Object

    Defines a validation for the specified parameter.

    This validation will call the named method on the command with the value of the named parameter. If the method returns a failure message, that message is added to the failed validations.

    Parameters:

    • name (String, Symbol)

      the parameter to validate.

    • using (String, Symbol) (defaults to: )

      the name of the method used to validate the parameter.

    • options (Hash)

      additional options to pass to the validation method.

    Options Hash (**options):

    • as (String, Symbol)

      the name of the parameter as displayed in the failure message, if any. Defaults to the value of the name parameter.

    Returns:

    • void

  • #validate(name, **options) {|value| ... } ⇒ Object

    Defines a validation for the specified parameter.

    This validation will call the given block with the value of the named parameter. If the block returns nil or false, a failure message is added to the failed validations

    Parameters:

    • name (String, Symbol)

      the parameter to validate.

    • options (Hash)

      additional options for the validation.

    Options Hash (**options):

    • as (String, Symbol)

      the name of the parameter as displayed in the failure message, if any. Defaults to the value of the name parameter.

    • message (String)

      the failure message to display. Defaults to “$name is invalid”.

    Yields:

    • the block to validate the parameter.

    Yield Parameters:

    • value (Object)

      the value of the named parameter.

    Yield Returns:

    • (true, false)

      true if the given value is valid for the parameter; otherwise false.

    Returns:

    • void

  • #validate(name, type, **options) ⇒ Object

    Defines a validation for the specified parameter.

    This validation will call the #validate_$type method on the command with the value of the named parameter. If the method returns a failure message, that message is added to the failed validations.

    If the command does not define the method, it will call the SleepingKingStudios::Tools::Assertions instance method with the same name. If the validation fails, the failure message is added to the failed validations.

    Parameters:

    • name (String, Symbol)

      the parameter to validate.

    • type (String, Symbol)

      the validation method to run.

    • options (Hash)

      additional options to pass to the validation method.

    Options Hash (**options):

    • as (String, Symbol)

      the name of the parameter as displayed in the failure message, if any. Defaults to the value of the name parameter.

    • message (String)

      the message to display on a failed validation.

    Raises:



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cuprum/parameter_validation.rb', line 113

def validate(name, type = nil, using: nil, **options, &)
  tools.assertions.validate_name(name, as: 'name')

  if type && !type.is_a?(Module)
    tools.assertions.validate_name(type, as: 'type')
  end

  tools.assertions.validate_name(using, as: 'using') if using

  validation_rules <<
    build_validation_rule(name:, options:, type:, using:, &)
end

#validate_parameters(command) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cuprum/parameter_validation.rb', line 127

def validate_parameters(command, ...)
  parameters = parameters_mapping.call(...)
  rules      = each_validation

  Validator.new.call(command:, parameters:, rules:)
rescue NameError => exception
  raise unless exception.name == :process

  error = Cuprum::Errors::CommandNotImplemented.new(command:)

  Cuprum::Result.new(error:)
end