Module: TTY::Option::ParamValidation Private

Defined in:
lib/tty/option/param_validation.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Responsible for parameter validation

Class Method Summary collapse

Class Method Details

.[]TTY::Option::Result

Validate parameter value against validation rule

Examples:

param = TTY::Option::Parameter::Option.create(:foo, validate: "\d+")
TTY::Option::ParamValidation[param, "12"] # => "12"

Parameters:

  • param (TTY::Option::Parameter)

    the parameter with a validation rule

  • value (Object)

    the value to validate

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tty/option/param_validation.rb', line 41

def call(param, value)
  return Result.success(value) if !param.validate? || value.nil?

  errors = []
  result = validate_object(param, value) do |error|
    errors << error
  end

  if errors.empty?
    Result.success(result)
  else
    Result.failure(errors)
  end
end

.call(param, value) ⇒ TTY::Option::Result

Validate parameter value against validation rule

Examples:

param = TTY::Option::Parameter::Option.create(:foo, validate: "\d+")
TTY::Option::ParamValidation[param, "12"] # => "12"

Parameters:

  • param (TTY::Option::Parameter)

    the parameter with a validation rule

  • value (Object)

    the value to validate

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tty/option/param_validation.rb', line 25

def call(param, value)
  return Result.success(value) if !param.validate? || value.nil?

  errors = []
  result = validate_object(param, value) do |error|
    errors << error
  end

  if errors.empty?
    Result.success(result)
  else
    Result.failure(errors)
  end
end