Sha256: 01f70e8ab53e3c1f8f86a8eb69299dfa4a29a5e015554eaa4c9bbcec3ac7d281
Contents?: true
Size: 1.55 KB
Versions: 3
Compression:
Stored size: 1.55 KB
Contents
# lib/lutaml/model/schema/json_schema.rb require "json" module Lutaml module Model module Schema class JsonSchema def self.generate(klass, options = {}) schema = { "$schema" => "https://json-schema.org/draft/2020-12/schema", "$id" => options[:id], "description" => options[:description], "$ref" => "#/$defs/#{klass.name}", "$defs" => generate_definitions(klass), }.compact options[:pretty] ? JSON.pretty_generate(schema) : schema.to_json end def self.generate_definitions(klass) { klass.name => { "type" => "object", "properties" => generate_properties(klass), "required" => klass.attributes.keys, }, } end def self.generate_properties(klass) klass.attributes.transform_values do |attr| generate_property_schema(attr) end end def self.generate_property_schema(attr) { "type" => get_json_type(attr.type) } end def self.get_json_type(type) { Lutaml::Model::Type::String => "string", Lutaml::Model::Type::Integer => "integer", Lutaml::Model::Type::Boolean => "boolean", Lutaml::Model::Type::Float => "number", Lutaml::Model::Type::Array => "array", Lutaml::Model::Type::Hash => "object", }[type] || "string" # Default to string for unknown types end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
lutaml-model-0.3.1 | lib/lutaml/model/schema/json_schema.rb |
lutaml-model-0.3.0 | lib/lutaml/model/schema/json_schema.rb |
lutaml-model-0.2.1 | lib/lutaml/model/schema/json_schema.rb |