Class: Apiwork::Contract::Object::Validator
- Inherits:
-
Object
- Object
- Apiwork::Contract::Object::Validator
- Defined in:
- lib/apiwork/contract/object/validator.rb,
lib/apiwork/contract/object/validator/result.rb
Defined Under Namespace
Classes: Result
Constant Summary collapse
- NOT_SET =
Module.new.freeze
- NUMERIC_TYPES =
Set[:integer, :number, :decimal].freeze
- ISSUE_DETAILS =
{ array_too_large: 'Too many items', array_too_small: 'Too few items', depth_exceeded: 'Too deeply nested', field_missing: 'Required', field_unknown: 'Unknown field', number_too_large: 'Too large', number_too_small: 'Too small', string_too_long: 'Too long', string_too_short: 'Too short', type_invalid: 'Invalid type', value_invalid: 'Invalid value', value_null: 'Cannot be null', }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(shape) ⇒ Validator
constructor
A new instance of Validator.
- #validate(data, current_depth: 0, max_depth: 10, path: []) ⇒ Object
Constructor Details
#initialize(shape) ⇒ Validator
Returns a new instance of Validator.
31 32 33 |
# File 'lib/apiwork/contract/object/validator.rb', line 31 def initialize(shape) @shape = shape end |
Class Method Details
.validate(shape, data, current_depth: 0, max_depth: 10, path: []) ⇒ Object
11 12 13 |
# File 'lib/apiwork/contract/object/validator.rb', line 11 def validate(shape, data, current_depth: 0, max_depth: 10, path: []) new(shape).validate(data, current_depth:, max_depth:, path:) end |
Instance Method Details
#validate(data, current_depth: 0, max_depth: 10, path: []) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/apiwork/contract/object/validator.rb', line 35 def validate(data, current_depth: 0, max_depth: 10, path: []) issues = [] params = {} data = data.deep_symbolize_keys if data.is_a?(Hash) return max_depth_error(current_depth, max_depth, path) if current_depth > max_depth @shape.params.each do |name, | param_issues, param_value = validate_param( name, data[name], , data, path, current_depth:, max_depth:, ) issues.concat(param_issues) params[name] = param_value unless param_value.equal?(NOT_SET) end issues.concat(check_unknown_params(data, path)) Result.new(issues:, params:) end |