Module: AnyVali::Interchange::Importer
- Defined in:
- lib/anyvali/interchange/importer.rb
Class Method Summary collapse
- .build_array(node) ⇒ Object
- .build_bool(node) ⇒ Object
- .build_int(node, kind) ⇒ Object
- .build_intersection(node) ⇒ Object
- .build_nullable(node) ⇒ Object
- .build_number(node, kind) ⇒ Object
- .build_object(node) ⇒ Object
- .build_record(node) ⇒ Object
- .build_string(node) ⇒ Object
- .build_tuple(node) ⇒ Object
- .build_union(node) ⇒ Object
- .import(doc) ⇒ Object
- .import_schema(doc) ⇒ Object
- .node_to_schema(node) ⇒ Object
Class Method Details
.build_array(node) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/anyvali/interchange/importer.rb', line 146 def build_array(node) # Accept "items", "item", and "array.items" keys for compatibility items_node = node["items"] || node["item"] || node["array.items"] items = node_to_schema(items_node) constraints = {} %w[minItems maxItems].each do |k| constraints[k] = node[k] if node.key?(k) end ArraySchema.new(items: items, constraints: constraints) end |
.build_bool(node) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/anyvali/interchange/importer.rb', line 135 def build_bool(node) coerce = node["coerce"] default_val = node["default"] has_default = node.key?("default") BoolSchema.new( coerce_config: coerce, default_value: default_val, has_default: has_default ) end |
.build_int(node, kind) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/anyvali/interchange/importer.rb', line 118 def build_int(node, kind) constraints = {} %w[min max exclusiveMin exclusiveMax multipleOf].each do |k| constraints[k] = node[k] if node.key?(k) end coerce = node["coerce"] default_val = node["default"] has_default = node.key?("default") IntSchema.new( kind: kind, constraints: constraints, coerce_config: coerce, default_value: default_val, has_default: has_default ) end |
.build_intersection(node) ⇒ Object
189 190 191 192 |
# File 'lib/anyvali/interchange/importer.rb', line 189 def build_intersection(node) all_of = node["allOf"].map { |v| node_to_schema(v) } IntersectionSchema.new(all_of: all_of) end |
.build_nullable(node) ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/anyvali/interchange/importer.rb', line 194 def build_nullable(node) inner = node_to_schema(node["schema"]) default_val = node["default"] has_default = node.key?("default") NullableSchema.new( schema: inner, default_value: default_val, has_default: has_default ) end |
.build_number(node, kind) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/anyvali/interchange/importer.rb', line 101 def build_number(node, kind) constraints = {} %w[min max exclusiveMin exclusiveMax multipleOf].each do |k| constraints[k] = node[k] if node.key?(k) end coerce = node["coerce"] default_val = node["default"] has_default = node.key?("default") NumberSchema.new( kind: kind, constraints: constraints, coerce_config: coerce, default_value: default_val, has_default: has_default ) end |
.build_object(node) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/anyvali/interchange/importer.rb', line 162 def build_object(node) props = {} (node["properties"] || {}).each do |k, v| props[k] = node_to_schema(v) end required = node["required"] || [] unknown_keys = node["unknownKeys"] || "strip" ObjectSchema.new( properties: props, required: required, unknown_keys: unknown_keys, unknown_keys_explicit: true ) end |
.build_record(node) ⇒ Object
177 178 179 180 |
# File 'lib/anyvali/interchange/importer.rb', line 177 def build_record(node) values = node_to_schema(node["values"]) RecordSchema.new(values: values) end |
.build_string(node) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/anyvali/interchange/importer.rb', line 85 def build_string(node) constraints = {} %w[minLength maxLength pattern startsWith endsWith includes format].each do |k| constraints[k] = node[k] if node.key?(k) end coerce = node["coerce"] default_val = node["default"] has_default = node.key?("default") StringSchema.new( constraints: constraints, coerce_config: coerce, default_value: default_val, has_default: has_default ) end |
.build_tuple(node) ⇒ Object
157 158 159 160 |
# File 'lib/anyvali/interchange/importer.rb', line 157 def build_tuple(node) elements = node["elements"].map { |e| node_to_schema(e) } TupleSchema.new(elements: elements) end |
.build_union(node) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/anyvali/interchange/importer.rb', line 182 def build_union(node) # Accept "variants", "schemas", and "union.variants" keys for compatibility variants_data = node["variants"] || node["schemas"] || node["union.variants"] || [] variants = variants_data.map { |v| node_to_schema(v) } UnionSchema.new(variants: variants) end |
.import(doc) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/anyvali/interchange/importer.rb', line 10 def import(doc) doc = JSON.parse(doc) if doc.is_a?(String) # Build definitions first (needed for refs) definitions = {} if doc["definitions"] doc["definitions"].each do |name, node| definitions[name] = node_to_schema(node) end end root_schema = node_to_schema(doc["root"]) context = ValidationContext.new(definitions: definitions) [root_schema, context, definitions] end |
.import_schema(doc) ⇒ Object
26 27 28 29 |
# File 'lib/anyvali/interchange/importer.rb', line 26 def import_schema(doc) schema, context, definitions = import(doc) schema end |
.node_to_schema(node) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/anyvali/interchange/importer.rb', line 31 def node_to_schema(node) kind = node["kind"] case kind when "string" build_string(node) when "number", "float32", "float64" build_number(node, kind) when "int", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64" build_int(node, kind) when "bool" build_bool(node) when "null" NullSchema.new when "any" AnySchema.new when "unknown" UnknownSchema.new when "never" NeverSchema.new when "literal" LiteralSchema.new(value: node["value"]) when "enum" EnumSchema.new(values: node["values"]) when "array" build_array(node) when "tuple" build_tuple(node) when "object" build_object(node) when "record" build_record(node) when "union" build_union(node) when "intersection" build_intersection(node) when "optional" OptionalSchema.new(schema: node_to_schema(node["schema"])) when "nullable" build_nullable(node) when "ref" RefSchema.new(ref: node["ref"]) else raise ValidationError, [ ValidationIssue.new( code: IssueCodes::UNSUPPORTED_SCHEMA_KIND, expected: "known schema kind", received: kind.to_s ) ] end end |