Class: Typero::ModelType

Inherits:
Type
  • Object
show all
Defined in:
lib/typero/type/types/model_type.rb

Constant Summary

Constants inherited from Type

Type::ERRORS, Type::OPTS, Type::OPTS_KEYS

Instance Attribute Summary

Attributes inherited from Type

#opts

Instance Method Summary collapse

Methods inherited from Type

allowed_opt?, #db_field, db_schema, #default, error, #get, #initialize, load, opts, #value

Constructor Details

This class inherits a constructor from Typero::Type

Instance Method Details

#db_schemaObject



18
19
20
21
22
# File 'lib/typero/type/types/model_type.rb', line 18

def db_schema
  [:jsonb, {
    null: false
  }]
end

#setObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/typero/type/types/model_type.rb', line 2

def set
  value(&:to_h)

  errors = {}
  schema = opts[:model].is_a?(Typero::Schema) ? opts[:model] : Typero.schema(opts[:model])

  # by default models in schems are strict true (remove undefined keys)
  schema.validate value, strict: true do |field, error|
    errors[field] = error
  end

  @value.delete_if {|_, v| v.respond_to?(:empty?) && v.empty? }

  raise TypeError.new errors.to_json if errors.keys.first
end