Sha256: ed9e0e59037d0be161ef1ef70e7641d9baab00a9b58964a878dcda2dabd90ea4

Contents?: true

Size: 849 Bytes

Versions: 9

Compression:

Stored size: 849 Bytes

Contents

#!/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

Version data entries

9 entries across 7 versions & 4 rubygems

Version Path
dc-kwalify-0.7.2 test/data/users-guide/validate08.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/kwalify-0.7.2/test/data/users-guide/validate08.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/kwalify-0.7.2/test/data/users-guide/validate08.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/kwalify-0.7.2/test/data/users-guide/validate08.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/kwalify-0.7.2/test/data/users-guide/validate08.rb
kwalify-0.7.2 test/data/users-guide/validate08.rb
ddao-kwalify-0.7.1 test/data/users-guide/validate08.rb
kwalify-0.7.1 test/data/users-guide/validate08.rb
kwalify-0.7.0 test/data/users-guide/validate08.rb