Method: Iry::Macros#check_constraint

Defined in:
lib/iry/macros.rb

#check_constraint(key, name: nil, message: :invalid) ⇒ void

This method returns an undefined value.

Tracks check constraint for the given key and convert constraint errors into validation errors

Parameters:

  • key (Symbol)

    key to apply validation errors to

  • message (Symbol, String) (defaults to: :invalid)

    the validation error message

  • name (nil, String) (defaults to: nil)

    constraint name. If omitted, it will be inferred using table name + key

Raises:

  • (ArgumentError)

    raised if constraint name already in use by another constraint of any type



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/iry/macros.rb', line 14

def check_constraint(
  key,
  name: nil,
  message: :invalid
)
  name ||= Constraint::Check.infer_name(key, table_name)

  if constraints.key?(name)
    raise ArgumentError, "Constraint already exists"
  end

  self.constraints = constraints.dup
  constraints[name] = Constraint::Check.new(
    key,
    message: message,
    name: name
  )
end