Class: Textus::Domain::Policy::Predicates::SchemaValid

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



6
7
8
# File 'lib/textus/domain/policy/predicates/schema_valid.rb', line 6

def reason
  @reason
end

Instance Method Details

#call(entry:, store:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/textus/domain/policy/predicates/schema_valid.rb', line 12

def call(entry:, store:)
  return true if entry.nil? || store.nil?

  target_key = entry.dig("_meta", "proposal", "target_key")
  return true unless target_key

  mentry, = store.manifest.resolve(target_key)
  schema_ref = mentry&.schema
  return true unless schema_ref

  schema = store.schema_for(schema_ref)
  return true unless schema

  frontmatter = entry.dig("_meta", "frontmatter") || {}
  begin
    schema.validate!(frontmatter)
  rescue Textus::SchemaViolation => e
    @reason = e.message.dup
    d = e.details
    if d.is_a?(Hash)
      if d["missing"]
        @reason = "missing required fields: #{Array(d["missing"]).join(", ")}"
      elsif d["field"]
        @reason = "field '#{d["field"]}': #{d["reason"]}"
      end
    end
    return false
  end

  true
rescue StandardError => e
  @reason = "schema validation error: #{e.message}"
  false
end

#nameObject



8
9
10
# File 'lib/textus/domain/policy/predicates/schema_valid.rb', line 8

def name
  "schema_valid"
end