Class: Sukima::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/sukima/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(value, error_field) ⇒ Dsl

Returns a new instance of Dsl.



5
6
7
8
# File 'lib/sukima/dsl.rb', line 5

def initialize(value, error_field)
  @value = value
  @error_field = error_field
end

Instance Method Details

#field(name, schema = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/sukima/dsl.rb', line 10

def field(name, schema = nil, **, &)
  schema = Sukima.new(**, &) if schema.nil?
  if @value.key?(name)
    result = schema.validate(@value[name])
    @error_field[name] = result unless result.valid?
  elsif schema.constraints[:required]
    @error_field[name] = ErrorField.new(errors: ['is required'])
  end
end

#items(schema = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/sukima/dsl.rb', line 20

def items(schema = nil, **, &)
  schema = Sukima.new(**, &) if schema.nil?
  @value.each_with_index do |item, index|
    result = schema.validate(item)
    @error_field[index] = result unless result.valid?
  end
end