Module: TRMNLP::FormField

Defined in:
lib/trmnlp/form_field.rb

Overview

Validates entries in a plugin’s settings.yml custom_fields list.

The required keys and field-type allowlist are vendored from the hosted service into db/data/form_fields.yml — the same db/data convention FrameworkVersion uses. See that file’s header for the refresh policy.

Validation is intentionally lenient: it mirrors the hosted service’s live form-field validation (presence checks only) and does NOT reject unknown keys — the hosted form views read far more keys than they validate.

Constant Summary collapse

DATA_PATH =
File.expand_path('../../db/data/form_fields.yml', __dir__)

Class Method Summary collapse

Class Method Details

.field_typesObject



21
# File 'lib/trmnlp/form_field.rb', line 21

def self.field_types = schema.fetch('field_types')

.multi_select?(field) ⇒ Boolean

Returns:

  • (Boolean)


23
# File 'lib/trmnlp/form_field.rb', line 23

def self.multi_select?(field) = field['field_type'] == 'select' && field['multiple']

.required_keysObject



20
# File 'lib/trmnlp/form_field.rb', line 20

def self.required_keys = schema.fetch('required_keys')

.schemaObject



19
# File 'lib/trmnlp/form_field.rb', line 19

def self.schema = @schema ||= YAML.safe_load_file(DATA_PATH).freeze

.validate(field) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/trmnlp/form_field.rb', line 25

def self.validate(field)
  warnings = []

  required_keys.each do |key|
    warnings << "missing required key: #{key}" unless field.key?(key) || field.key?(key.to_sym)
  end

  type = field['field_type'] || field[:field_type]
  warnings << "unknown field_type: #{type}" if type && !field_types.include?(type)

  warnings
end

.validate_all(fields) ⇒ Object



38
39
40
# File 'lib/trmnlp/form_field.rb', line 38

def self.validate_all(fields)
  Array(fields).flat_map { |field| validate(field) }
end