Module: Textus::Protocol::EntryConstraint

Defined in:
lib/textus/protocol/entry_constraint.rb

Class Method Summary collapse

Class Method Details

.validate_format!(payload, mentry, format: nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/textus/protocol/entry_constraint.rb', line 44

def validate_format!(payload, mentry, format: nil)
  fmt_name = format || mentry.format
  fmt = Textus::Protocol::Format.for(fmt_name)
  fmt.validate_raw_entry!(
    { "_meta" => payload.meta, "content" => payload.content },
    mentry.lane,
  )
end

.validate_naming!(key, mentry) ⇒ Object



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

def validate_naming!(key, mentry)
  naming = mentry.naming
  return unless naming
  return unless key.start_with?("#{mentry.key}.")

  last = key.split(".").last

  case naming
  when "sequential"
    return if last.match?(/\A\d{4}-[a-z][a-z0-9-]*\z/)

    raise UsageError.new(
      "naming violation: '#{key}' last segment must match NNNN-slug " \
      "(entry '#{mentry.key}' has naming: sequential)",
    )
  when "dated"
    return if last.match?(/\A\d{4}-\d{2}-\d{2}-[a-z][a-z0-9-]*\z/)

    raise UsageError.new(
      "naming violation: '#{key}' last segment must match YYYY-MM-DD-slug " \
      "(entry '#{mentry.key}' has naming: dated)",
    )
  end
end

.validate_schema!(payload, mentry, schemas:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/textus/protocol/entry_constraint.rb', line 31

def validate_schema!(payload, mentry, schemas:)
  schema_name = mentry.schema
  return unless schema_name && schemas

  schema = schemas.fetch_or_nil(schema_name)
  return unless schema
  return if mentry.lane == "scratchpad"

  schema.validate!(payload.meta || {})
rescue Textus::SchemaViolation
  raise if mentry.lane != "scratchpad"
end