Class: OpenapiFirst::Schema::Hash
- Inherits:
-
Object
- Object
- OpenapiFirst::Schema::Hash
- Defined in:
- lib/openapi_first/schema/hash.rb
Overview
A hash of Schemas
Instance Method Summary collapse
-
#initialize(schemas, required: nil, **options) ⇒ Hash
constructor
A new instance of Hash.
- #validate(root_value) ⇒ Object
Constructor Details
#initialize(schemas, required: nil, **options) ⇒ Hash
Returns a new instance of Hash.
11 12 13 14 15 16 17 18 |
# File 'lib/openapi_first/schema/hash.rb', line 11 def initialize(schemas, required: nil, **) @schemas = schemas @options = @after_property_validation = .delete(:after_property_validation) schema = { 'type' => 'object' } schema['required'] = required if required @root_schema = JSONSchemer.schema(schema, **) end |
Instance Method Details
#validate(root_value) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/openapi_first/schema/hash.rb', line 20 def validate(root_value) validation = @root_schema.validate(root_value) validations = @schemas.reduce(validation) do |enum, (key, schema)| root_value[key] = schema.value['default'] if schema.value.key?('default') && !root_value.key?(key) next enum unless root_value.key?(key) value = root_value[key] key_validation = schema.validate(value) @after_property_validation&.each do |hook| hook.call(root_value, key, schema.value, nil) end enum.chain(key_validation.map do |err| err.merge('data_pointer' => "/#{key}") end) end ValidationResult.new(validations) end |