Class: RequestHandler::Validation::DryEngine
- Defined in:
- lib/request_handler/validation/dry_engine.rb
Class Method Summary collapse
- .call_schema(value, schema, options) ⇒ Object
- .error_message(validation_error) ⇒ Object
- .error_pointer(_validation_error) ⇒ Object
- .schema_class?(schema) ⇒ Boolean
- .schema_instance?(schema) ⇒ Boolean
- .to_result(result) ⇒ Object
- .valid_schema?(schema) ⇒ Boolean
- .validate(value, schema, options: {}) ⇒ Object
- .validate!(value, schema, options: {}) ⇒ Object
Class Method Details
.call_schema(value, schema, options) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/request_handler/validation/dry_engine.rb', line 20 def self.call_schema(value, schema, ) if .empty? schema_instance?(schema) ? schema.call(value) : schema.new.call(value) else schema_instance?(schema) ? schema.with().call(value) : schema.new().call(value) end end |
.error_message(validation_error) ⇒ Object
49 50 51 |
# File 'lib/request_handler/validation/dry_engine.rb', line 49 def self.(validation_error) validation_error end |
.error_pointer(_validation_error) ⇒ Object
53 54 55 |
# File 'lib/request_handler/validation/dry_engine.rb', line 53 def self.error_pointer(_validation_error) nil end |
.schema_class?(schema) ⇒ Boolean
32 33 34 |
# File 'lib/request_handler/validation/dry_engine.rb', line 32 def self.schema_class?(schema) schema.respond_to?(:schema) end |
.schema_instance?(schema) ⇒ Boolean
28 29 30 |
# File 'lib/request_handler/validation/dry_engine.rb', line 28 def self.schema_instance?(schema) schema.respond_to?(:call) end |
.to_result(result) ⇒ Object
43 44 45 46 47 |
# File 'lib/request_handler/validation/dry_engine.rb', line 43 def self.to_result(result) output = result.respond_to?(:to_h) ? result.to_h : result errors = result.respond_to?(:errors) ? result.errors.to_h : {} Result.new(output: output, errors: errors) end |
.valid_schema?(schema) ⇒ Boolean
9 10 11 |
# File 'lib/request_handler/validation/dry_engine.rb', line 9 def self.valid_schema?(schema) schema_instance?(schema) || schema_class?(schema) end |
.validate(value, schema, options: {}) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/request_handler/validation/dry_engine.rb', line 13 def self.validate(value, schema, options: {}) value = value.deep_symbolize_keys if value.is_a?(Hash) to_result(call_schema(value, schema, )) rescue Dry::Types::ConstraintError => e Result.new(output: nil, errors: { '' => e }) end |
.validate!(value, schema, options: {}) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/request_handler/validation/dry_engine.rb', line 36 def self.validate!(value, schema, options: {}) validate(value, schema, ).tap do |result| valid = result.respond_to?(:valid?) ? result.valid? : result.success? raise Validation::Error unless valid end end |