Class: Castkit::Contract::Base

Inherits:
Object
  • Object
show all
Extended by:
Castkit::Core::AttributeTypes, Castkit::Core::Config, Castkit::Core::Registerable
Defined in:
lib/castkit/contract/base.rb

Overview

Base class for all Castkit contracts.

Castkit contracts define validation logic over a set of attributes using a DSL. You can either subclass this directly or use build to generate ephemeral or reusable contract classes.

Examples:

Subclassing directly

class MyContract < Castkit::Contract::Base
  string :id
  integer :count, required: false
end

MyContract.validate!(id: "abc")

Using Contract.build (preferred for dynamic generation)

UserContract = Castkit::Contract.build(:user) do
  string :id
  string :email, required: false
end

UserContract.validate!(id: "123")

See Also:

Constant Summary collapse

ATTRIBUTE_OPTIONS =
i[
  required aliases min max format of validator unwrapped prefix force_type
].freeze

Constants included from Castkit::Core::Registerable

Castkit::Core::Registerable::CASTKIT_NAMESPACES

Class Method Summary collapse

Methods included from Castkit::Core::Config

allow_unknown, ignore_unknown, relaxed, strict, validation_rules, warn_on_unknown

Methods included from Castkit::Core::AttributeTypes

array, boolean, dataobject, date, datetime, define_type_dsl, float, hash, included, integer, string, unwrapped

Methods included from Castkit::Core::Registerable

register!

Class Method Details

.attribute(field, type, **options) ⇒ void

This method returns an undefined value.

Defines an attribute for the contract.

Only a subset of options is allowed inside a contract.

Parameters:

  • field (Symbol)

    the field name

  • type (Symbol, Class, Array)

    the type or union of types

  • options (Hash)

    allowed options like :required or :validator



61
62
63
64
# File 'lib/castkit/contract/base.rb', line 61

def attribute(field, type, **options)
  options = options.slice(*ATTRIBUTE_OPTIONS)
  attributes[field] = Castkit::Attribute.new(field, type, **options)
end

.attributesHash{Symbol => Castkit::Attribute}

Returns the defined attributes.

Returns:



99
100
101
# File 'lib/castkit/contract/base.rb', line 99

def attributes
  definition[:attributes]
end

.definitionHash

Returns internal contract metadata.

Returns:

  • (Hash)


89
90
91
92
93
94
# File 'lib/castkit/contract/base.rb', line 89

def definition
  @definition ||= {
    name: :ephemeral,
    attributes: {}
  }
end

.register!(as: nil) ⇒ Class

Registers the current class under ‘Castkit::Contracts`.

Parameters:

  • as (String, Symbol, nil) (defaults to: nil)

    The constant name to use (PascalCase). Defaults to the name used when building the contract. If no name was provided, an error is raised.

Returns:

  • (Class)

    the registered contract class

Raises:



49
50
51
# File 'lib/castkit/contract/base.rb', line 49

def register!(as: nil)
  super(namespace: :contracts, as: as || definition[:name])
end

.validate(input) ⇒ Castkit::Contract::Result

Validates input against the contract and returns a Result.

Parameters:

  • input (Hash)

Returns:



70
71
72
73
74
# File 'lib/castkit/contract/base.rb', line 70

def validate(input)
  validate!(input)
rescue Castkit::ContractError => e
  Castkit::Contract::Result.new(definition[:name].to_s, input, errors: e.errors)
end

.validate!(input) ⇒ void

This method returns an undefined value.

Validates input and raises on failure.

Parameters:

  • input (Hash)

Raises:



81
82
83
84
# File 'lib/castkit/contract/base.rb', line 81

def validate!(input)
  Castkit::Contract::Validator.call!(attributes.values, input, **validation_rules)
  Castkit::Contract::Result.new(definition[:name].to_s, input)
end