Class Kwalify::Validator
In: kwalify/validator.rb
Parent: Object

ex.

  schema = YAML.load_file('schema.yaml')
  validator = Kwalify::Validator.new(schema)
  document = YAML.load_file('document.yaml')
  error_list = validator.validate(document)
  unless error_list.empty?
    error_list.each do |error|
      puts "- [#{error.path}] #{error.message}"
    end
  end

Methods

Included Modules

Kwalify::ErrorHelper

Attributes

rule  [R] 

Public Class methods

[Source]

# File kwalify/validator.rb, line 29
      def initialize(hash, &block)
         @rule  = Rule.new(hash)
         @block = block
      end

Public Instance methods

[Source]

# File kwalify/validator.rb, line 36
      def _inspect
         @rule._inspect
      end

[Source]

# File kwalify/validator.rb, line 41
      def validate(value)
         path = "";  errors = [];  done = {}
         _validate(value, @rule, path, errors, done)
         return errors
      end

Protected Instance methods

[Source]

# File kwalify/validator.rb, line 55
      def _validate(value, rule, path, errors, done)
         if Types.collection?(value)
            return if done[value.__id__]     # avoid infinite loop
            done[value.__id__] = true
         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)
            #* key=:type_unmatch  msg="not a %s."
            errors << validate_error(:type_unmatch, rule, path, value, [Kwalify.word(rule.type)])
            return
         end
         #
         n = errors.length
         if rule.sequence
            _validate_sequence(value, rule, path, errors, done)
         elsif rule.mapping
            _validate_mapping(value, rule, path, errors, done)
         else
            _validate_scalar(value, rule, path, errors, done)
         end
         return unless errors.length == n
         #
         validate_hook(value, rule, path, errors)
         @block.call(value, rule, path, errors) if @block
      end

[Source]

# File kwalify/validator.rb, line 51
      def validate_hook(value, rule, path, errors)
      end

[Validate]