Class: Textus::Manifest::Policy::Predicates::SchemaValid

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/manifest/policy/predicates/schema_valid.rb

Class Method Summary collapse

Class Method Details

.call(manifest:, actor:, action:, key:, schemas: nil, envelope: nil, extra: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/textus/manifest/policy/predicates/schema_valid.rb', line 6

def self.call(manifest:, actor:, action:, key:, schemas: nil, envelope: nil, extra: {})
  return { pass: true } unless envelope
  return { pass: true } if key.nil?

  mentry = manifest.resolver.resolve(key).entry
  schema_ref = mentry.schema
  return { pass: true } unless schema_ref
  return { pass: true } unless schemas

  schema = schemas.fetch_or_nil(schema_ref)
  return { pass: true } unless schema

  frontmatter = envelope.meta&.dig("_meta") || envelope.meta || {}
  begin
    schema.validate!(frontmatter)
    { pass: true }
  rescue Textus::SchemaViolation => e
    { pass: false, reason: schema_reason(e) }
  end
rescue Textus::UnknownKey
  { pass: true }
end

.schema_reason(err) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/textus/manifest/policy/predicates/schema_valid.rb', line 29

def self.schema_reason(err)
  d = err.details
  return err.message.dup unless d.is_a?(Hash)
  return "missing required fields: #{Array(d["missing"]).join(", ")}" if d["missing"]
  return "field '#{d["field"]}': #{d["reason"]}" if d["field"]

  err.message.dup
end