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

.add_schema(store, file) ⇒ Object



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

def self.add_schema(store, file)
  data = JSON.parse(File.read(file))
  schema = JsonSchema::Parser.new.parse!(data)
  schema.expand_references!(store: store)
  store.add_schema(schema)
end

.document_storeJsonSchema::DocumentStore

Returns:

  • (JsonSchema::DocumentStore)


35
36
37
# File 'lib/prmd/commands/verify.rb', line 35

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
# 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__)
    add_schema(store, file)
  end
  add_schema(store, @custom_schema) unless @custom_schema.nil?
  store
end

.verify(schema_data, custom_schema: nil) ⇒ Array<String>

Verfies that a given schema is valid

Parameters:

  • schema_data (Hash)

Returns:

  • (Array<String>)

    errors from failed verification



78
79
80
81
82
83
84
85
# File 'lib/prmd/commands/verify.rb', line 78

def self.verify(schema_data, custom_schema: nil)
  @custom_schema = custom_schema
  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



55
56
57
58
59
60
# File 'lib/prmd/commands/verify.rb', line 55

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prmd/commands/verify.rb', line 41

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



64
65
66
67
68
69
70
71
72
# File 'lib/prmd/commands/verify.rb', line 64

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