Class | Kwalify::Validator |
In: |
kwalify/validator.rb
|
Parent: | Object |
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
rule | [R] |
# File 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
# File 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
# File 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
# File 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