Class: Synthra::Export::JsonSchema

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/export/json_schema.rb

Overview

Exports schema as JSON Schema (draft 2020-12)

Examples:

Export a schema

exporter = JsonSchema.new(schema, registry: registry)
json_schema = exporter.export

Constant Summary collapse

SCHEMA_VERSION =

JSON Schema draft version

"https://json-schema.org/draft/2020-12/schema"
TYPE_MAPPINGS =

Type mappings from DSL types to JSON Schema types

{
  # Strings with formats
  "uuid" => { "type" => "string", "format" => "uuid" },
  "ulid" => { "type" => "string", "pattern" => "^[0-7][0-9A-HJKMNP-TV-Z]{25}$" },
  "email" => { "type" => "string", "format" => "email" },
  "url" => { "type" => "string", "format" => "uri" },
  "ip" => { "type" => "string", "format" => "ipv4" },
  "ipv6" => { "type" => "string", "format" => "ipv6" },
  "date" => { "type" => "string", "format" => "date" },
  "date_between" => { "type" => "string", "format" => "date" },
  "past_date" => { "type" => "string", "format" => "date" },
  "future_date" => { "type" => "string", "format" => "date" },
  "mobile_device_release_date" => { "type" => "string", "format" => "date" },
  "now" => { "type" => "string", "format" => "date" },
  "timestamp" => { "type" => "string", "format" => "date-time" },
  "datetime" => { "type" => "string", "format" => "date-time" },

  # Plain strings
  "text" => { "type" => "string" },
  "name" => { "type" => "string" },
  "full_name" => { "type" => "string" },
  "first_name" => { "type" => "string" },
  "last_name" => { "type" => "string" },
  "phone" => { "type" => "string" },
  "city" => { "type" => "string" },
  "country" => { "type" => "string" },
  "country_code" => { "type" => "string", "minLength" => 2, "maxLength" => 3 },
  "postal_code" => { "type" => "string" },
  "state" => { "type" => "string" },
  "street" => { "type" => "string" },
  "address" => { "type" => "string" },
  "username" => { "type" => "string" },
  "social_handle" => { "type" => "string" },
  "domain" => { "type" => "string", "format" => "hostname" },
  "user_agent" => { "type" => "string" },
  "mac_address" => { "type" => "string" },
  "iban" => { "type" => "string" },
  "credit_card" => { "type" => "string" },
  "currency" => { "type" => "string" },
  "currency_code" => { "type" => "string", "minLength" => 3, "maxLength" => 3 },
  "paragraph" => { "type" => "string" },
  "sentence" => { "type" => "string" },
  "word" => { "type" => "string" },
  "message_text" => { "type" => "string" },
  "hashtag_text" => { "type" => "string" },

  # Numbers (integers)
  "number" => { "type" => "integer" },
  "integer" => { "type" => "integer" },
  "age" => { "type" => "integer", "minimum" => 0, "maximum" => 150 },
  "airport_elevation" => { "type" => "integer" },
  "discount" => { "type" => "integer" },
  "formula" => { "type" => "integer" },
  "row_number" => { "type" => "integer" },
  "sequence" => { "type" => "integer" },
  "id_sequence" => { "type" => "integer" },

  # Numbers (floats)
  "float" => { "type" => "number" },
  "money" => { "type" => "number" },
  "latitude" => { "type" => "number", "minimum" => -90, "maximum" => 90 },
  "longitude" => { "type" => "number", "minimum" => -180, "maximum" => 180 },
  "airport_latitude" => { "type" => "number", "minimum" => -90, "maximum" => 90 },
  "airport_longitude" => { "type" => "number", "minimum" => -180, "maximum" => 180 },
  "product_price" => { "type" => "number" },
  "tax_rate" => { "type" => "number" },
  "transaction_amount" => { "type" => "number" },

  # Numeric-looking strings
  "snowflake_id" => { "type" => "string", "pattern" => "^[0-9]+$" },
  "numeric_string_id" => { "type" => "string", "pattern" => "^[0-9]+$" },

  # Boolean
  "boolean" => { "type" => "boolean" },

  # Objects
  "object" => { "type" => "object" },
  "map_by_field" => { "type" => "object" },

  # Arrays
  "array" => { "type" => "array" },
  "empty_array" => { "type" => "array", "maxItems" => 0 },
  "json_array" => { "type" => "array" },
  "indices_pair" => { "type" => "array", "items" => { "type" => "integer" }, "minItems" => 2, "maxItems" => 2 }
}.freeze

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Synthra::Export::Base

Instance Method Details

#build_array_schema(args) ⇒ Object (private)



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/synthra/export/json_schema.rb', line 238

def build_array_schema(args)
  element = args[:element]
  size = args[:size]

  prop = { "type" => "array" }

  if element
    if schema_reference?(element.to_s)
      prop["items"] = { "$ref" => "#/$defs/#{element}" }
    elsif TYPE_MAPPINGS[element.to_s]
      prop["items"] = TYPE_MAPPINGS[element.to_s].dup
    else
      prop["items"] = { "type" => "string" }
    end
  end

  if size.is_a?(Range)
    prop["minItems"] = size.min
    prop["maxItems"] = size.max
  elsif size.is_a?(Hash)
    prop["minItems"] = size[:min] if size[:min]
    prop["maxItems"] = size[:max] if size[:max]
  elsif size.is_a?(Integer)
    prop["minItems"] = size
    prop["maxItems"] = size
  end

  prop
end

#build_const_schema(args) ⇒ Object (private)



234
235
236
# File 'lib/synthra/export/json_schema.rb', line 234

def build_const_schema(args)
  { "const" => args[:value] }
end

#build_definitionsObject (private)



141
142
143
144
145
146
147
# File 'lib/synthra/export/json_schema.rb', line 141

def build_definitions
  defs = {}
  collect_referenced_schemas.each do |ref_schema|
    defs[ref_schema.name] = build_schema_definition(ref_schema)
  end
  defs
end

#build_descriptionObject (private)



134
135
136
137
138
139
# File 'lib/synthra/export/json_schema.rb', line 134

def build_description
  parts = ["Generated from Synthra schema: #{schema.name}"]
  parts << "Version: #{schema.version}" if schema.version
  parts << "DEPRECATED: #{schema.deprecation_message}" if schema.deprecated?
  parts.join(". ")
end

#build_enum_schema(args) ⇒ Object (private)



229
230
231
232
# File 'lib/synthra/export/json_schema.rb', line 229

def build_enum_schema(args)
  values = args[:values]&.map { |v| v.respond_to?(:value) ? v.value : v } || []
  { "type" => "string", "enum" => values }
end

#build_map_by_field_schema(args) ⇒ Object (private)



281
282
283
284
285
286
# File 'lib/synthra/export/json_schema.rb', line 281

def build_map_by_field_schema(args)
  {
    "type" => "object",
    "additionalProperties" => { "type" => "object" }
  }
end

#build_one_of_schema(args) ⇒ Object (private)



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/synthra/export/json_schema.rb', line 268

def build_one_of_schema(args)
  schemas = args[:schemas] || []
  one_of = schemas.map do |s|
    schema_name = s.is_a?(Hash) ? (s[:schema] || s["schema"]) : s.to_s
    if schema_reference?(schema_name)
      { "$ref" => "#/$defs/#{schema_name}" }
    else
      { "type" => "object" }
    end
  end
  { "oneOf" => one_of }
end

#build_root_schemaObject (private)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/synthra/export/json_schema.rb', line 110

def build_root_schema
  result = {
    "$schema" => SCHEMA_VERSION,
    "$id" => "#{schema.name.downcase}.json",
    "title" => schema.name,
    "description" => build_description,
    "type" => "object",
    "properties" => {},
    "required" => []
  }

  # Add $defs for referenced schemas
  defs = build_definitions
  result["$defs"] = defs unless defs.empty?

  # Add properties
  schema.fields.each do |field|
    result["properties"][field.name] = field_to_schema(field)
    result["required"] << field.name unless field.optional?
  end

  result
end

#build_schema_definition(ref_schema) ⇒ Object (private)



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/synthra/export/json_schema.rb', line 174

def build_schema_definition(ref_schema)
  result = {
    "type" => "object",
    "properties" => {},
    "required" => []
  }

  ref_schema.fields.each do |field|
    result["properties"][field.name] = field_to_schema(field)
    result["required"] << field.name unless field.optional?
  end

  result
end

#collect_field_schemas(field, schemas) ⇒ Object (private)



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/synthra/export/json_schema.rb', line 157

def collect_field_schemas(field, schemas)
  type_name = field.type_name

  if schema_reference?(type_name)
    ref_schema = get_schema(type_name)
    schemas << ref_schema if ref_schema && !schemas.any? { |s| s.name == ref_schema.name }
  end

  if type_name == "array"
    element = field.type_args[:element]
    if element && schema_reference?(element.to_s)
      ref_schema = get_schema(element.to_s)
      schemas << ref_schema if ref_schema && !schemas.any? { |s| s.name == ref_schema.name }
    end
  end
end

#collect_referenced_schemasObject (private)



149
150
151
152
153
154
155
# File 'lib/synthra/export/json_schema.rb', line 149

def collect_referenced_schemas
  schemas = []
  schema.fields.each do |field|
    collect_field_schemas(field, schemas)
  end
  schemas.uniq(&:name)
end

#exportObject



103
104
105
106
# File 'lib/synthra/export/json_schema.rb', line 103

def export
  json_schema = build_root_schema
  JSON.pretty_generate(json_schema)
end

#field_to_schema(field) ⇒ Object (private)



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/synthra/export/json_schema.rb', line 189

def field_to_schema(field)
  type_name = field.type_name
  type_args = field.type_args

  prop = case type_name
         when "enum"
           build_enum_schema(type_args)
         when "const"
           build_const_schema(type_args)
         when "array"
           build_array_schema(type_args)
         when "one_of"
           build_one_of_schema(type_args)
         when "map_by_field"
           build_map_by_field_schema(type_args)
         else
           if TYPE_MAPPINGS[type_name]
             TYPE_MAPPINGS[type_name].dup
           elsif schema_reference?(type_name)
             { "$ref" => "#/$defs/#{type_name}" }
           else
             { "type" => "string" }
           end
         end

  # Add range constraints
  if type_args[:range] && prop["type"] == "integer"
    range = type_args[:range]
    prop["minimum"] = range.min if range.respond_to?(:min)
    prop["maximum"] = range.max if range.respond_to?(:max)
  end

  # Handle nullable
  if field.nullable?
    prop = { "oneOf" => [prop, { "type" => "null" }] }
  end

  prop
end