Class: Dry::Validation::Matchers::ValidateMatcher
- Inherits:
-
Object
- Object
- Dry::Validation::Matchers::ValidateMatcher
- Defined in:
- lib/dry/validation/matchers/validate_matcher.rb
Constant Summary collapse
- DEFAULT_TYPE =
:string
- TYPE_ERRORS =
{ string: { test_value: "str", message: "must be a string", }, integer: { test_value: 43, message: "must be an integer", }, float: { test_value: 41.5, message: "must be a float", }, decimal: { test_value: BigDecimal("41.5"), message: "must be a decimal", }, bool: { test_value: false, message: "must be a boolean", }, date: { test_value: Date.new(2011, 1, 2), message: "must be a date", }, time: { test_value: Time.new(2011, 1, 2, 2, 33), message: "must be a time", }, date_time: { test_value: DateTime.new(2011, 5, 1, 2, 3, 4), message: "must be a date time", }, array: { test_value: [1, 3, 5], message: "must be a array", }, hash: { test_value: {hello: "there"}, message: "must be a hash", }, }
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #filled(type = DEFAULT_TYPE) ⇒ Object
-
#initialize(attr, acceptance) ⇒ ValidateMatcher
constructor
A new instance of ValidateMatcher.
- #macro_use?(macro_params) ⇒ Boolean
- #matches?(schema_or_schema_class) ⇒ Boolean
- #value(value_rules) ⇒ Object
Constructor Details
#initialize(attr, acceptance) ⇒ ValidateMatcher
Returns a new instance of ValidateMatcher.
48 49 50 51 52 53 54 55 56 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 48 def initialize(attr, acceptance) @attr = attr @acceptance = acceptance @type = DEFAULT_TYPE @value_rules = [] @macro_usage_params = [] @check_filled = false @check_macro = false end |
Instance Method Details
#description ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 58 def description @desc = [] @desc << "validate for #{@acceptance} `#{@attr}`" = [] << "filled with #{@type}" if @check_filled << "macro usage `#{@macro_usage_params.to_s}`" if @check_macro unless .empty? @desc << " (" @desc << .join("; ") @desc << ")" end @desc << " exists" @desc.join end |
#failure_message ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 76 def @desc = [] @desc << "be missing validation for #{@acceptance} `#{@attr}`" = [] << "filled with #{@type}" if @check_filled << "macro usage `#{@macro_usage_params.to_s}`" if @check_macro unless .empty? @desc << " (" @desc << .join("; ") @desc << ")" end @desc.join end |
#filled(type = DEFAULT_TYPE) ⇒ Object
114 115 116 117 118 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 114 def filled(type=DEFAULT_TYPE) @check_filled = true @type = type self end |
#macro_use?(macro_params) ⇒ Boolean
125 126 127 128 129 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 125 def macro_use?(macro_params) @check_macro = true @macro_usage_params = macro_params self end |
#matches?(schema_or_schema_class) ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 93 def matches?(schema_or_schema_class) if schema_or_schema_class.is_a?(Dry::Validation::Contract) schema = schema_or_schema_class elsif schema_or_schema_class.is_a?(Class) && schema_or_schema_class.ancestors.include?(Dry::Validation::Contract) schema = schema_or_schema_class.new else fail( ArgumentError, "must be a schema instance or class; got #{schema_or_schema_class.inspect} instead" ) end check_required_or_optional!(schema) && check_filled!(schema) && check_filled_with_type!(schema) && check_value!(schema) && check_macro_usage!(schema) end |
#value(value_rules) ⇒ Object
120 121 122 123 |
# File 'lib/dry/validation/matchers/validate_matcher.rb', line 120 def value(value_rules) @value_rules = value_rules self end |