Module: SnippetCli::MatchValidator

Defined in:
lib/snippet_cli/match_validator.rb

Overview

Validates an Espanso match entry (as a Ruby hash) against the merged schema embedded at lib/snippet_cli/Espanso_Merged_Matchfile_Schema.json. Uses json_schemer which supports JSON Schema draft-07 (required for the if/then conditionals used in the Espanso match schema).

Class Method Summary collapse

Class Method Details

.errors(data) ⇒ Object

Returns an array of human-readable error strings. Empty array means the data is valid.



22
23
24
25
26
# File 'lib/snippet_cli/match_validator.rb', line 22

def self.errors(data)
  SchemaValidator.validate(wrap(HashUtils.stringify_keys_deep(data))).map do |error|
    error['error'] || error.fetch('type', 'validation error')
  end
end

.valid?(data) ⇒ Boolean

Returns true if the data is valid against the Espanso match schema. Accepts symbol or string keys — keys are stringified before validation. The merged schema validates a full matchfile, so the entry is wrapped in a { “matches” => […] } envelope before validation.

Returns:

  • (Boolean)


16
17
18
# File 'lib/snippet_cli/match_validator.rb', line 16

def self.valid?(data)
  SchemaValidator.valid?(wrap(HashUtils.stringify_keys_deep(data)))
end