Module: AnyVali::Interchange::Exporter

Defined in:
lib/anyvali/interchange/exporter.rb

Class Method Summary collapse

Class Method Details

.export(schema, mode: :portable, definitions: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/anyvali/interchange/exporter.rb', line 10

def export(schema, mode: :portable, definitions: {})
  if mode == :portable && !schema.portable?
    raise ValidationError, [
      ValidationIssue.new(
        code: IssueCodes::CUSTOM_VALIDATION_NOT_PORTABLE,
        expected: "portable schema",
        received: "schema with custom validators"
      )
    ]
  end

  doc = {
    "anyvaliVersion" => AnyValiDocument::ANYVALI_VERSION,
    "schemaVersion" => AnyValiDocument::SCHEMA_VERSION,
    "root" => schema_to_node(schema),
    "definitions" => definitions.transform_values { |v| schema_to_node(v) },
    "extensions" => {}
  }
  doc
end

.schema_to_node(schema) ⇒ Object



35
36
37
# File 'lib/anyvali/interchange/exporter.rb', line 35

def schema_to_node(schema)
  schema.to_node
end

.to_json(schema, mode: :portable, definitions: {}) ⇒ Object



31
32
33
# File 'lib/anyvali/interchange/exporter.rb', line 31

def to_json(schema, mode: :portable, definitions: {})
  JSON.generate(export(schema, mode: mode, definitions: definitions))
end