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

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

Overview

Predicate: the entry’s effective frontmatter satisfies the schema bound to the target key. For accept, the frontmatter lives under envelope.meta; for a direct put it is envelope.meta.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schemas:) ⇒ SchemaValid

Returns a new instance of SchemaValid.



13
14
15
# File 'lib/textus/domain/policy/predicates/schema_valid.rb', line 13

def initialize(schemas:)
  @schemas = schemas
end

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



11
12
13
# File 'lib/textus/domain/policy/predicates/schema_valid.rb', line 11

def reason
  @reason
end

Instance Method Details

#call(eval) ⇒ Object



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 19

def call(eval)
  manifest = eval.manifest
  return true if eval.envelope.nil? || manifest.nil? || @schemas.nil?

  target_key = eval.target
  return true unless target_key

  mentry = manifest.resolver.resolve(target_key).entry
  schema_ref = mentry&.schema
  return true unless schema_ref

  schema = @schemas.fetch_or_nil(schema_ref)
  return true unless schema

  frontmatter =
    eval.envelope.meta&.dig("frontmatter") || eval.envelope.meta || {}
  begin
    schema.validate!(frontmatter)
    true
  rescue Textus::SchemaViolation => e
    @reason = humanize(e)
    false
  end
rescue StandardError => e
  @reason = "schema validation error: #{e.message}"
  false
end

#nameObject



17
# File 'lib/textus/domain/policy/predicates/schema_valid.rb', line 17

def name = "schema_valid"