File: validate08.rb

package info (click to toggle)
kwalify 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,852 kB
  • ctags: 1,188
  • sloc: ruby: 8,446; xml: 172; makefile: 36; java: 36
file content (37 lines) | stat: -rw-r--r-- 849 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env ruby

require 'kwalify'
require 'yaml'

## load schema definition
schema = YAML.load_file('answers-schema.yaml')

## create validator for answers
validator = Kwalify::Validator.new(schema) { |value, rule, path, errors|
   case rule.name
   when 'Answer'
      if value['answer'] == 'bad'
         reason = value['reason']
         if !reason || reason.empty?
            msg = "reason is required when answer is 'bad'."
            errors << Kwalify::ValidationError.new(msg, path)
         end
      end
   end
}

## load YAML document
input = ARGF.read()
document = YAML.load(input)

## validate
errors = validator.validate(document)
if errors.empty?
   puts "Valid."
else
   puts "*** INVALID!"
   errors.each do |error|
      # error.class == Kwalify::ValidationError
      puts " - [#{error.path}] : #{error.message}"
   end
end