Class: Castkit::Contract::Base
- Inherits:
-
Object
- Object
- Castkit::Contract::Base
- 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.
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
-
.attribute(field, type, **options) ⇒ void
Defines an attribute for the contract.
-
.attributes ⇒ Hash{Symbol => Castkit::Attribute}
Returns the defined attributes.
-
.definition ⇒ Hash
Returns internal contract metadata.
-
.register!(as: nil) ⇒ Class
Registers the current class under ‘Castkit::Contracts`.
-
.validate(input) ⇒ Castkit::Contract::Result
Validates input against the contract and returns a Result.
-
.validate!(input) ⇒ void
Validates input and raises on failure.
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
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.
61 62 63 64 |
# File 'lib/castkit/contract/base.rb', line 61 def attribute(field, type, **) = .slice(*ATTRIBUTE_OPTIONS) attributes[field] = Castkit::Attribute.new(field, type, **) end |
.attributes ⇒ Hash{Symbol => Castkit::Attribute}
Returns the defined attributes.
99 100 101 |
# File 'lib/castkit/contract/base.rb', line 99 def attributes definition[:attributes] end |
.definition ⇒ Hash
Returns internal contract metadata.
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`.
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.
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.
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 |