Class: Saphyr::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/saphyr/validator.rb

Overview

Base class used to define a validator.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator



70
71
72
73
# File 'lib/saphyr/validator.rb', line 70

def initialize()
  @proc_idx = 0
  @ctx = nil
end

Class Attribute Details

.cache_configObject

Returns the value of attribute cache_config.



9
10
11
# File 'lib/saphyr/validator.rb', line 9

def cache_config
  @cache_config
end

.proc_idxObject

Returns the value of attribute proc_idx.



9
10
11
# File 'lib/saphyr/validator.rb', line 9

def proc_idx
  @proc_idx
end

Class Method Details

.cast(field, method) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/saphyr/validator.rb', line 52

def cast(field, method)
  if method.is_a? Proc
    m = "_proc_#{self.internal_proc_index}".to_sym
    self.send :define_method, m, method
    method = m
    self.proc_idx += 1
  end
  config.cast field, method
end

.conditional(cond, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/saphyr/validator.rb', line 38

def conditional(cond, &block)
  #
  # TODO: 'cond' must be a method Symbol or a proc / lambda
  #
  method = cond
  if cond.is_a? Proc
    m = "_proc_#{self.internal_proc_index}".to_sym
    self.send :define_method, m, cond
    method = m
    self.proc_idx += 1
  end
  config.conditional method, &block
end

.configObject



11
12
13
# File 'lib/saphyr/validator.rb', line 11

def config()
  self.cache_config ||= Saphyr::Schema.new
end

.field(name, type, **opts) ⇒ Object



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

def field(name, type, **opts)
  if config.root_array? and name != :_root_
    raise Saphyr::Error.new "Can only define ':_root_' field when root is ':array'"
  end
  if name == :_root_ and type != :array
    raise Saphyr::Error.new "Field ':_root_' must be of type ':array'"
  end
  config.field name, type, **opts
end

.root(value) ⇒ Object



20
21
22
# File 'lib/saphyr/validator.rb', line 20

def root(value)
  config.root value
end

.schema(name, &block) ⇒ Object



34
35
36
# File 'lib/saphyr/validator.rb', line 34

def schema(name, &block)
  config.schema name, &block
end

.strict(value) ⇒ Object

—– Proxy all following method calls to Saphyr::Schema instance



16
17
18
# File 'lib/saphyr/validator.rb', line 16

def strict(value)
  config.strict value
end

Instance Method Details

#dataHash

Get the parsed JSON data.



90
91
92
93
# File 'lib/saphyr/validator.rb', line 90

def data()
  return nil if @ctx.nil?
  @ctx.data
end

#errorsArray

Get the validation errors



116
117
118
119
# File 'lib/saphyr/validator.rb', line 116

def errors()
  return [] if @ctx.nil?
  @ctx.errors
end

#find_schema(name) ⇒ Saphyr::Schema

Find a local schema



84
85
86
# File 'lib/saphyr/validator.rb', line 84

def find_schema(name)
  self.class.config.find_schema name
end

#get(*args) ⇒ Object

Get a field from the data to validate.

Use variadic arguments to access deep fields.

Examples:

data = {
  "id" => 3465,
  "info" => {
    "suffix" => ["gif", "jpg", "png"]
    "size" => 34056
  }
}

get("id")                    # => 3435
get("info", "suffix", 1)     # => "jpg"
get("info", "size")          # => 34056
get("name")                  # => raise an exception

Raises:



142
143
144
145
146
# File 'lib/saphyr/validator.rb', line 142

def get(*args)
  status, value = get_safe(*args)
  raise Saphyr::Error.new 'Requested field does not exists' if status == :err
  value
end

#get_configSaphyr::Schema

Get the validator configuration (ie: the attached schema)



77
78
79
# File 'lib/saphyr/validator.rb', line 77

def get_config()
  self.class.config
end

#get_safe(*args) ⇒ Object

Get a field from the data to validate. (Same as get() but never raise an exception).

Use variadic arguments to access deep fields.

Examples:

data = {
  "id" => 3465,
  "info" => {
    "suffix" => ["gif", "jpg", "png"]
    "size" => 34056
  }
}

get("id")                    # => [:ok, 3435]
get("info", "suffix", 1)     # => [:ok, "jpg"]
get("info", "size")          # => [:ok, 34056]
get("name")                  # => [:err, :not_exists]
get("info", "suffix", 5)     # => [:err, :not_index]
get("info", 3)               # => [:err, :not_array]


171
172
173
# File 'lib/saphyr/validator.rb', line 171

def get_safe(*args)
  do_get_safe @data, args.reverse
end

#parse_and_validate(json) ⇒ Boolean

Parse and validate a JSON document.



110
111
112
# File 'lib/saphyr/validator.rb', line 110

def parse_and_validate(json)
  validate JSON.parse(json)
end

#validate(data) ⇒ Boolean

Validate an already parsed JSON document.



100
101
102
103
104
105
# File 'lib/saphyr/validator.rb', line 100

def validate(data)
  @ctx = Saphyr::Engine::Context.new [self], get_config, data, nil, '//'
  engine = Saphyr::Engine.new @ctx
  engine.validate
  @ctx.errors.size == 0
end