Module: JSONValidate

Defined in:
lib/json_validate.rb,
lib/json_validate/version.rb

Defined Under Namespace

Classes: BooleanValidator, CustomValidator, ValidationError

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.pathstr(path) ⇒ Object



7
8
9
# File 'lib/json_validate.rb', line 7

def self.pathstr(path)
  "/"+path.join("/")
end

.validate(json, validator = nil, path = [], &block) ⇒ Object

Validate the parsed JSON object with the validator validator can be a hash or an array

e.g. {id: Fixnum, subject: String, message: String, receipients: [{email: String, name: String}]}
e.g. [String]

This throws a ValidationError if the validation failed. If a block is given, validator is ignored and the block is evaluated as the validator to be used



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/json_validate.rb', line 41

def self.validate(json, validator=nil, path=[], &block)
  return validate(json, JSONValidate.instance_eval(&block), path) if block_given?

  case validator
  when Hash # validate each pair of the hash
    validateHash(json, validator, path)
  when Array # validate each elements of the array
    validateArray(json, validator, path)
  when Class # validate the type of the value
    raise ValidationError.new(pathstr(path)) unless json.kind_of? validator
  when CustomValidator
    raise ValidationError.new(pathstr(path)) unless validator.validate(json)
  when Regexp
    raise ValidationError.new(pathstr(path)) unless validator =~ json
  when nil
    raise ValidationError.new(pathstr(path)) unless json.nil?
  else
    raise ValidationError.new("Validator is expected to be Hash, Array, or Class.")
  end
end

.validateArray(array, validator, path = []) ⇒ Object

Validate each elements of an array by the first element of validator(validator) This expects validator is an array containing only one element. Other than the first element will be ignored

Raises:



24
25
26
27
28
29
30
31
32
33
# File 'lib/json_validate.rb', line 24

def self.validateArray(array, validator, path=[])
  raise ValidationError.new(pathstr(path) + " : Expected to be an Array.") unless array.kind_of? Array
  parent = path.pop || "" # to make the path like "/hoge/foo/parent[1]""
  array.each_index do |i|
    path.push(parent+"[#{i}]")
    validate(array[i], validator[0], path)
    path.pop
  end
  path.push parent
end

.validateHash(hash, validator, path = []) ⇒ Object

Validate each elements of a hash by the validator.

Raises:



12
13
14
15
16
17
18
19
20
# File 'lib/json_validate.rb', line 12

def self.validateHash(hash, validator, path=[])
    raise ValidationError.new(pathstr(path) + " : Expected to be a Hash.") unless hash.kind_of? Hash
    validator.each do |key, value|
      raise ValidationError.new(pathstr(path) + " : Key '#{key}' is expected to be exists.") unless hash.include? key.to_s
      path.push(key.to_s)
      validate(hash[key.to_s], value, path)
      path.pop
    end
end