Module: RSMP::Schemer

Defined in:
lib/rsmp_schemer/error.rb,
lib/rsmp_schemer/schemer.rb,
lib/rsmp_schemer/version.rb

Defined Under Namespace

Classes: Error, UnknownSchemaError, UnknownSchemaTypeError, UnknownSchemaVersionError

Constant Summary collapse

VERSION =
"0.5.0"
@@schemas =
nil

Class Method Summary collapse

Class Method Details

.find_schema(type, version, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
# File 'lib/rsmp_schemer/schemer.rb', line 56

def self.find_schema type, version, options={}
  raise ArgumentError.new("version missing") unless version
  version = sanitize_version version if options[:lenient]
  if version
    schemas = find_schemas type
    return schemas[version] if schemas
  end
  nil
end

.find_schema!(type, version, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rsmp_schemer/schemer.rb', line 71

def self.find_schema! type, version, options={}
  schema = find_schema type, version, options
  raise ArgumentError.new("version missing") unless version
  version = sanitize_version version if options[:lenient]
  if version
    schemas = find_schemas! type
    schema = schemas[version]
    return schema if schema
  end
  raise UnknownSchemaVersionError.new("Unknown schema version #{type} #{version}")
end

.find_schemas(type) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
# File 'lib/rsmp_schemer/schemer.rb', line 45

def self.find_schemas type
  raise ArgumentError.new("type missing") unless type
  schemas[type.to_sym]
end

.find_schemas!(type) ⇒ Object



50
51
52
53
54
# File 'lib/rsmp_schemer/schemer.rb', line 50

def self.find_schemas! type
  schemas = find_schemas type
  raise UnknownSchemaTypeError.new("Unknown schema type #{type}") unless schemas
  schemas
end

.has_schema?(type, version, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rsmp_schemer/schemer.rb', line 83

def self.has_schema? type, version, options={}
  find_schema(type,version, options) != nil
end

.sanitize_version(version) ⇒ Object



66
67
68
69
# File 'lib/rsmp_schemer/schemer.rb', line 66

def self.sanitize_version version
  matched = /^\d+\.\d+\.\d+/.match version
  matched.to_s if matched
end

.schemasObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rsmp_schemer/schemer.rb', line 9

def self.schemas
  return @@schemas if @@schemas
  schemas_path = File.expand_path( File.join(__dir__,'..','..','schemas','schemas') )
  @@schemas = {
    core: {
      '3.1.1' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.1','rsmp.json')) ),
      '3.1.2' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.2','rsmp.json')) ),
      '3.1.3' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.3','rsmp.json')) ),
      '3.1.4' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.4','rsmp.json')) ),
      '3.1.5' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'core','3.1.5','rsmp.json')) )
    },
    # note that tlc 1.0.11 and 1.0.12 does not exist (unreleased drafts)
    tlc: {
      '1.0.7'  => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.7' ,'sxl.json')) ),
      '1.0.8'  => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.8' ,'sxl.json')) ),
      '1.0.9'  => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.9' ,'sxl.json')) ),
      '1.0.10' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.10','sxl.json')) ),
      '1.0.13' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.13','sxl.json')) ),
      '1.0.14' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.14','sxl.json')) ),
      '1.0.15' => JSONSchemer.schema( Pathname.new(File.join(schemas_path, 'tlc','1.0.15','sxl.json')) )
    }
  }
end

.validate(message, schemas, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rsmp_schemer/schemer.rb', line 87

def self.validate message, schemas, options={}
  raise ArgumentError.new("message missing") unless message
  raise ArgumentError.new("schemas missing") unless schemas
  raise ArgumentError.new("schemas must be a Hash") unless schemas.is_a?(Hash)
  raise ArgumentError.new("schemas cannot be empty") unless schemas.any?
  errors = schemas.flat_map do |type, version|
    schema = find_schema! type, version, options
    validate_using_schema(message, schema)
  end
  return nil if errors.empty?
  errors
end

.validate_using_schema(message, schema) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rsmp_schemer/schemer.rb', line 33

def self.validate_using_schema message, schema
  raise ArgumentError.new("message missing") unless message
  raise ArgumentError.new("schema missing") unless schema
  unless schema.valid? message
    schema.validate(message).map do |item|
      [item['data_pointer'],item['type'],item['details']]
    end
  else
    []
  end
end