Class: YES::Lint

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/lint.rb

Overview

YAML Easy Schema - Constrain Validator (“Lint”)

Issues

  • Matching fully-quaified tags.

  • Handling namespaces.

  • Handling YAML version directive.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Lint

Returns a new instance of Lint.



23
24
25
26
# File 'lib/yes/lint.rb', line 23

def initialize(schema)
  @schema    = YAML.load(schema)
  @checklist = []
end

Instance Attribute Details

#checklistObject (readonly)

The constraint checklist.



20
21
22
# File 'lib/yes/lint.rb', line 20

def checklist
  @checklist
end

#schemaObject (readonly)

The schema document.



14
15
16
# File 'lib/yes/lint.rb', line 14

def schema
  @schema
end

Instance Method Details

#select_nodes(ypath, tree) ⇒ Object

TODO: Maybe this should be a class method, too ?



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/yes/lint.rb', line 72

def select_nodes(ypath, tree)
  case ypath
  when Hash
    if path = ypath['path'] || ypath['ypath']
      nodes = tree.select(path)
    else
      nodes = tree.select('*') # all nodes
    end
    YES.constraints.each do |c|
      next unless Constraints::NodeConstraint === c
      checklist = c.checklist(spec, tree, nodes)
      selection = checklist.select{ |v| v.valid? }
      nodes.concat(selection.map{ |s| s.node })
    end
    nodes
  else
    tree.select(ypath)
  end
end

#validate(yaml) ⇒ Array

Returns List of constraints that were invalid.

Parameters:

  • yaml (String)

    A YAML document.

Returns:

  • (Array)

    List of constraints that were invalid.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yes/lint.rb', line 35

def validate(yaml)
  @checklist = []

  tree = YAML.parse(yaml)

  @schema.each do |ypath, spec|
    validate_ypath(ypath, spec, tree)
  end

  @checklist.reject{ |v| v.valid? }
end

#validate_spec(ypath, spec, tree) ⇒ Object

Process all validations.

FIXME: Add logic handling.



63
64
65
66
67
68
69
# File 'lib/yes/lint.rb', line 63

def validate_spec(ypath, spec, tree)
  nodes = select_nodes(ypath, tree)

  YES.constraints.each do |c|
    @checklist.concat(c.checklist(spec, tree, nodes))
  end
end

#validate_ypath(ypath, spec, tree) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/yes/lint.rb', line 48

def validate_ypath(ypath, spec, tree)
  validate_spec(ypath, spec, tree)

  ## handle sub-paths
  if spec['map']
    spec['map'].each do |sub_path, sub_spec|
      sub_ypath = ypath + '/' + sub_path
      validate_ypath(sub_ypath, sub_spec, tree)
    end
  end
end