Class: Smartbill::Sdk::Contracts::Base

Inherits:
Dry::Validation::Contract
  • Object
show all
Defined in:
lib/smartbill/sdk/contracts/base.rb

Overview

Base class for every SmartBill validation contract.

Declares shared type predicates (e.g. DATE_REGEX) and provides validate, the helper services call to run a struct’s attributes through the contract and raise ValidationError on failure.

Constant Summary collapse

DATE_REGEX =

‘YYYY-MM-DD`, matching the SmartBill API date format.

/\A\d{4}-\d{2}-\d{2}\z/

Class Method Summary collapse

Class Method Details

.validate!(struct) ⇒ Object

Validate struct (a Models::Struct) against this contract, raising ValidationError with the aggregated error messages when the contract fails. Returns the struct unchanged on success.

Raises:



19
20
21
22
23
24
25
26
27
28
# File 'lib/smartbill/sdk/contracts/base.rb', line 19

def validate!(struct)
  result = new.call(struct.to_attributes)
  return struct if result.success?

  messages = result.errors.map do |err|
    path = err.path.is_a?(Array) ? err.path.join(".") : err.path
    "#{path} #{err.text}"
  end
  raise ValidationError, "Validation failed: #{messages.join("; ")}"
end