Class: YES::Constraints::Value

Inherits:
NodeConstraint show all
Defined in:
lib/yes/constraints/value.rb

Overview

Validate if a node’s value conforms to a constraint, where a value is either an sequence element or a mapping value.

//authors:
  type: seq
  value:
    type: str

A valid code value could then have no more than three characters.

Instance Attribute Summary

Attributes inherited from NodeConstraint

#node

Attributes inherited from AbstractConstraint

#nodes, #spec, #tree

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeConstraint

#initialize, #tag, #value

Methods inherited from AbstractConstraint

#applicable?, inherited, #initialize, #match_delta, #recurse_valid?, #valid?

Constructor Details

This class inherits a constructor from YES::Constraints::NodeConstraint

Class Method Details

.applicable?(spec) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/yes/constraints/value.rb', line 47

def self.applicable?(spec)
  spec['value']
end

.checklist(spec, tree, nodes) ⇒ Array<Constraint>

For value constraint, the work is all handled by the checklist method.

Returns:

  • (Array<Constraint>)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yes/constraints/value.rb', line 20

def self.checklist(spec, tree, nodes)
  return [] unless applicable?(spec)

  vspec = spec['value']
  list  = []

  nodes.each do |node|
    case node.kind
    when :seq
      YES.constraints.each do |c|
        list.concat(c.checklist(vspec, tree, node.children))
      end
    when :map
      YES.constraints.each do |c|
        list.concat(c.checklist(vspec, tree, node.value.values))
      end
    else
      # TODO: might value for scalars have a useful meaning?
      raise "value constraint does not apply to scalars"
    end
  end

  list
end

Instance Method Details

#validate(spec) ⇒ Boolean

no-op

Returns:

  • (Boolean)

    validity



55
56
# File 'lib/yes/constraints/value.rb', line 55

def validate(spec)
end