Class: RestKat::Validator

Inherits:
Kwalify::Validator
  • Object
show all
Defined in:
lib/rest_kat/validator.rb

Overview

validator class for answers

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Validator

Returns a new instance of Validator.



15
16
17
# File 'lib/rest_kat/validator.rb', line 15

def initialize type
  super (@resource_schema=type)
end

Class Method Details

.find_resource(resource, schema) ⇒ Object



19
20
21
22
23
# File 'lib/rest_kat/validator.rb', line 19

def self.find_resource resource, schema
  schema["resources"].find do |r|
    r["name"].to_s == resource.to_s
  end
end

.for_resource(resource, file) ⇒ Object

Raises:

  • (Exception)


25
26
27
28
29
30
# File 'lib/rest_kat/validator.rb', line 25

def self.for_resource resource, file
    schema = load_schema file
    type = find_resource(resource, schema)["type"]
    raise Exception.new("Can't find resource '#{resource}'") unless type
    Validator.new type
end

.for_resource_collection(resource, file) ⇒ Object



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

def self.for_resource_collection resource, file
  schema = load_schema file
  type = find_resource(resource, schema)["type"]

  collection_type = {
    "type" => "seq",
    "sequence" => [ type ]
  }
  Validator.new collection_type
end

.for_type(type, file) ⇒ Object

Raises:

  • (Exception)


32
33
34
35
36
37
# File 'lib/rest_kat/validator.rb', line 32

def self.for_type type, file
  schema = load_schema file
  type = schema[type.to_s]
  raise Exception.new("Can't find resource '#{resource}'") unless type
  Validator.new type
end

.load_schema(file) ⇒ Object



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

def self.load_schema file
  Kwalify::Yaml.load_file file
end

Instance Method Details

#schemaObject



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

def schema
  @schema
end