Class: Kwalify::Validator

Inherits:
Object
  • Object
show all
Includes:
ErrorHelper
Defined in:
lib/kwalify/validator.rb

Overview

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

Direct Known Subclasses

MetaValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorHelper

_build_message, #assert_error, #schema_error, validate_error

Constructor Details

#initialize(hash_or_rule, &block) ⇒ Validator

Returns a new instance of Validator.



44
45
46
47
48
# 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

Instance Attribute Details

#ruleObject (readonly)

Returns the value of attribute rule.



49
50
51
# File 'lib/kwalify/validator.rb', line 49

def rule
  @rule
end

Instance Method Details

#_inspectObject



52
53
54
# File 'lib/kwalify/validator.rb', line 52

def _inspect
  @rule._inspect
end

#_validate(value, rule, path, errors, done, uniq_table, recursive = true) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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_mapping_required_keys(hash, map_rule, path, errors) ⇒ Object

:nodoc:



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/kwalify/validator.rb', line 161

def _validate_mapping_required_keys(hash, map_rule, path, errors)  #:nodoc:
  map_rule.mapping.each do |key, rule|
    #next unless rule.required
    #val = hash.is_a?(Hash) ? hash[key] : hash.instance_variable_get("@#{key}")
    #if val.nil?
    if rule.required && hash[key].nil?  # or !hash.key?(key)
      #* key=:required_nokey  msg="key '%s:' is required."
      errors << validate_error(:required_nokey, rule, path, hash, [key])
    end
  end
end

#_validate_unique(value, rule, path, errors, uniq_table) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# 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) ⇒ Object



57
58
59
60
61
# 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