class Kwalify::Validator

validate YAML document

ex1. validate yaml document

schema = YAML.load_file('schema.yaml')
validator = Kwalify::Validator.new(schema)
document = YAML.load_file('document.yaml')
erros = validator.validate(document)
if errors && !errors.empty?
  errors.each do |err|
    puts "- [#{err.path}] #{err.message}"
  end
end

ex2. validate with parsing

schema = YAML.load_file('schema.yaml')
validator = Kwalify::Validator.new(schema)
parser = Kwalify::Yaml::Parser.new(validator)
document = parser.parse(File.read('document.yaml'))
errors = parser.errors
if errors && errors.empty?
  errors.each do |e|
    puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
  end
end

Attributes

rule[R]

Public Class Methods

new(hash_or_rule, &block) click to toggle source
# File lib/kwalify/validator.rb, line 44
def initialize(hash_or_rule, &block)
  obj = hash_or_rule
  @rule = (obj.nil? || obj.is_a?(Rule)) ? obj : Rule.new(obj)
  @block = block
end

Public Instance Methods

_inspect() click to toggle source
# File lib/kwalify/validator.rb, line 52
def _inspect
  @rule._inspect
end
_validate(value, rule, path, errors, done, uniq_table, recursive=true) click to toggle source
# File lib/kwalify/validator.rb, line 75
def _validate(value, rule, path, errors, done, uniq_table, recursive=true)
  #if Types.collection?(value)
  if !Types.scalar?(value)
    #if done[value.__id__]
    #  rule2 = done[value.__id__]
    #  if rule2.is_a?(Rule)
    #    return if rule.equal?(rule2)
    #    done[value.__id__] = [rule2, rule]
    #  elsif rule2.is_a?(Array)
    #    return if rule2.any? {|r| r.equal?(rule)}
    #    done[value.__id__] << rule
    #  else
    #    raise "unreachable"
    #  end
    #end
    return if done[value.__id__]     # avoid infinite loop
    done[value.__id__] = rule
  end
  if rule.required && value.nil?
    #* key=:required_novalue  msg="value required but none."
    errors << validate_error(:required_novalue, rule, path, value)
    return
  end
  if rule.type_class && !value.nil? && !value.is_a?(rule.type_class)
    unless rule.classobj && value.is_a?(rule.classobj)
      #* key=:type_unmatch  msg="not a %s."
      errors << validate_error(:type_unmatch, rule, path, value, [Kwalify.word(rule.type)])
      return
    end
  end
  #
  n = errors.length
  if rule.sequence
    _validate_sequence(value, rule, path, errors, done, uniq_table, recursive)
  elsif rule.mapping
    _validate_mapping(value, rule, path, errors, done, uniq_table, recursive)
  else
    _validate_scalar(value, rule, path, errors, done, uniq_table)
  end
  return unless errors.length == n
  #
  #path_str = path.is_a?(Array) ? '/'+path.join('/') : path
  #validate_hook(value, rule, path_str, errors)
  #@block.call(value, rule, path_str, errors) if @block
  validate_hook(value, rule, path, errors)
  @block.call(value, rule, path, errors) if @block
end
_validate_unique(value, rule, path, errors, uniq_table) click to toggle source
# File lib/kwalify/validator.rb, line 188
def _validate_unique(value, rule, path, errors, uniq_table)
  assert_error "uniq_table=#{uniq_table.inspect}" unless rule.unique || rule.ident
  if uniq_table.key?(value)
    exist_at = uniq_table[value]
    exist_at = "/#{exist_at.join('/')}" if exist_at.is_a?(Array)
    #* key=:value_notunique  msg="is already used at '%s'."
    errors << validate_error(:value_notunique, rule, path, value, exist_at)
  else
    uniq_table[value] = path.dup
  end
end
validate(value) click to toggle source
# File lib/kwalify/validator.rb, line 57
def validate(value)
  path = '';  errors = [];  done = {};  uniq_table = nil
  _validate(value, @rule, path, errors, done, uniq_table)
  return errors
end

Protected Instance Methods

validate_hook(value, rule, path, errors) click to toggle source
# File lib/kwalify/validator.rb, line 67
def validate_hook(value, rule, path, errors)
  ## may be overrided by subclass
end