Class: Mortymer::Contract

Inherits:
Dry::Validation::Contract
  • Object
show all
Includes:
Moldeable, Types
Defined in:
lib/mortymer/contract.rb

Overview

A base model for defining schemas

Defined Under Namespace

Classes: ContractError

Constant Summary

Constants included from Types

Types::UploadedFile, Types::UploadedFiles

Class Method Summary collapse

Class Method Details

.__internal_struct_repr__Object



26
27
28
# File 'lib/mortymer/contract.rb', line 26

def self.__internal_struct_repr__
  @__internal_struct_repr__ ||= StructCompiler.new.compile(schema.json_schema)
end

.compile!Object



46
47
48
49
50
51
52
53
# File 'lib/mortymer/contract.rb', line 46

def self.compile!
  # Force eager compilation of the internal struct representation.
  # This provides an optimization by precompiling the struct when
  # the class is defined rather than waiting for the first use.
  # The compilation result is memoized, so subsequent accesses
  # will reuse the compiled struct.
  __internal_struct_repr__
end

.json_schemaObject



30
31
32
# File 'lib/mortymer/contract.rb', line 30

def self.json_schema
  Generator.new.from_validation(self)
end

.structify(params) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mortymer/contract.rb', line 34

def self.structify(params)
  # If params are already built using the internal struct, then there is
  # no need to re-validate it
  return params if params.instance_of?(__internal_struct_repr__)

  params = params.to_h if params.is_a?(Dry::Struct)
  result = new.call(params)
  raise ContractError, result.errors.to_h unless result.errors.empty?

  __internal_struct_repr__.new(**result.to_h)
end