Module: Prmd::Verification

Defined in:
lib/prmd/commands/verify.rb

Overview

Schema Verification

Constant Summary collapse

SCHEMAS =

These schemas are listed manually and in order because they reference each other.

[
  'schema.json',
  'hyper-schema.json',
  'interagent-hyper-schema.json'
]

Class Method Summary collapse

Class Method Details

.document_storeJsonSchema::DocumentStore

Returns:

  • (JsonSchema::DocumentStore)


30
31
32
# File 'lib/prmd/commands/verify.rb', line 30

def self.document_store
  @document_store ||= init_document_store
end

.init_document_storeJsonSchema::DocumentStore

Returns:

  • (JsonSchema::DocumentStore)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/prmd/commands/verify.rb', line 17

def self.init_document_store
  store = JsonSchema::DocumentStore.new
  SCHEMAS.each do |file|
    file = File.expand_path("../../../../schemas/#{file}", __FILE__)
    data = JSON.parse(File.read(file))
    schema = JsonSchema::Parser.new.parse!(data)
    schema.expand_references!(store: store)
    store.add_schema(schema)
  end
  store
end

.verify(schema_data) ⇒ Array<String>

Verfies that a given schema is valid

Parameters:

  • schema_data (Hash)

Returns:

  • (Array<String>)

    errors from failed verification



73
74
75
76
77
78
79
# File 'lib/prmd/commands/verify.rb', line 73

def self.verify(schema_data)
  a = verify_schema(schema_data)
  return a unless a.empty?
  b = verify_parsable(schema_data)
  return b unless b.empty?
  []
end

.verify_meta_schema(meta_schema, schema_data) ⇒ Array<String>

Returns errors from failed verfication.

Parameters:

  • schema_data (Hash)

Returns:

  • (Array<String>)

    errors from failed verfication



50
51
52
53
54
55
# File 'lib/prmd/commands/verify.rb', line 50

def self.verify_meta_schema(meta_schema, schema_data)
  valid, errors = meta_schema.validate(schema_data)
  return JsonSchema::SchemaError.aggregate(errors) unless valid

  []
end

.verify_parsable(schema_data) ⇒ Array<String>

Returns errors from failed verfication.

Parameters:

  • schema_data (Hash)

Returns:

  • (Array<String>)

    errors from failed verfication



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/prmd/commands/verify.rb', line 36

def self.verify_parsable(schema_data)
  # for good measure, make sure that the schema parses and that its
  # references can be expanded
  schema, errors = JsonSchema.parse!(schema_data)
  return JsonSchema::SchemaError.aggregate(errors) unless schema

  valid, errors = schema.expand_references(store: document_store)
  return JsonSchema::SchemaError.aggregate(errors) unless valid

  []
end

.verify_schema(schema_data) ⇒ Array<String>

Returns errors from failed verfication.

Parameters:

  • schema_data (Hash)

Returns:

  • (Array<String>)

    errors from failed verfication



59
60
61
62
63
64
65
66
67
# File 'lib/prmd/commands/verify.rb', line 59

def self.verify_schema(schema_data)
  schema_uri = schema_data['$schema']
  return ['Missing $schema key.'] unless schema_uri

  meta_schema = document_store.lookup_schema(schema_uri)
  return ["Unknown $schema: #{schema_uri}."] unless meta_schema

  verify_meta_schema(meta_schema, schema_data)
end