diff --git a/Gemfile.lock b/Gemfile.lock index 6facdc2..0a1d86e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: simple_json_schema_builder (0.1.0) + multi_json (~> 1.0) GEM remote: https://rubygems.org/ @@ -10,6 +11,7 @@ GEM diff-lcs (1.5.1) json (2.7.2) language_server-protocol (3.17.0.3) + multi_json (1.15.0) parallel (1.26.3) parser (3.3.5.0) ast (~> 2.4.1) diff --git a/lib/simple_json_schema_builder/base.rb b/lib/simple_json_schema_builder/base.rb index 4e98fcc..d097f03 100644 --- a/lib/simple_json_schema_builder/base.rb +++ b/lib/simple_json_schema_builder/base.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "multi_json" + module SimpleJsonSchemaBuilder class Base def self.object(&block) @@ -12,7 +14,7 @@ def self.schema end def self.to_json - schema.to_json + MultiJson.dump(schema) end def initialize diff --git a/simple_json_schema_builder.gemspec b/simple_json_schema_builder.gemspec index 94ca2d3..cb22b36 100644 --- a/simple_json_schema_builder.gemspec +++ b/simple_json_schema_builder.gemspec @@ -10,15 +10,15 @@ Gem::Specification.new do |spec| spec.summary = "JSON Schema builder with simple DSL." spec.description = "Build JSON Schema with simple ruby code, with objects and clean DSL." - spec.homepage = "TODO: Put your gem's website or public repo URL here." + spec.homepage = "https://github.com/mooktakim/simple_json_schema_builder" spec.license = "MIT" spec.required_ruby_version = ">= 3.0.0" - spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" + # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" spec.metadata["homepage_uri"] = spec.homepage - spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here." - spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." + spec.metadata["source_code_uri"] = "https://github.com/mooktakim/simple_json_schema_builder" + # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. @@ -35,6 +35,7 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem # spec.add_dependency "example-gem", "~> 1.0" + spec.add_dependency "multi_json", "~> 1.0" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/spec/simple_json_schema_builder/base_spec.rb b/spec/simple_json_schema_builder/base_spec.rb index 7137ff5..8eaaa41 100644 --- a/spec/simple_json_schema_builder/base_spec.rb +++ b/spec/simple_json_schema_builder/base_spec.rb @@ -95,88 +95,98 @@ class TestSubSchema < SimpleJsonSchemaBuilder::Base subject { TestSchema.schema } let(:properties) { subject[:properties] } - it "correctly formats schema" do - expect(subject[:type]).to eq("object") - expect(subject[:required]).to eq(%i[string1 string3 integer1 integer3 number1 number3 boolean1 object1 object3]) - - # String - expect(properties[:string1][:type]).to eq("string") - expect(properties[:string1][:title]).to eq("String1 Title") - expect(properties[:string1][:description]).to eq("String1 Description") - expect(properties[:string1][:examples]).to eq(["Test1 Example"]) - - expect(properties[:string2][:type]).to eq("array") - expect(properties[:string2][:items][:type]).to eq("string") - - expect(properties[:string3][:type]).to eq("string") - expect(properties[:string3][:enum]).to eq(["Test3 A", "Test3 B"]) - - expect(properties[:string4][:type]).to eq("array") - expect(properties[:string4][:items][:type]).to eq("string") - expect(properties[:string4][:items][:enum]).to eq(["Test4 A", "Test4 B"]) - - # Integer - expect(properties[:integer1][:type]).to eq("integer") - expect(properties[:integer1][:title]).to eq("Integer1 Title") - expect(properties[:integer1][:description]).to eq("Integer1 Description") - expect(properties[:integer1][:examples]).to eq([1, 2, 3]) - - expect(properties[:integer2][:type]).to eq("array") - expect(properties[:integer2][:items][:type]).to eq("integer") - - expect(properties[:integer3][:type]).to eq("integer") - expect(properties[:integer3][:enum]).to eq([4, 5, 6]) - - expect(properties[:integer4][:type]).to eq("array") - expect(properties[:integer4][:items][:type]).to eq("integer") - expect(properties[:integer4][:items][:enum]).to eq([7, 8, 9]) - - # Number - expect(properties[:number1][:type]).to eq("number") - expect(properties[:number1][:title]).to eq("Number1 Title") - expect(properties[:number1][:description]).to eq("Number1 Description") - expect(properties[:number1][:examples]).to eq([1, -2, 4]) - - expect(properties[:number2][:type]).to eq("array") - expect(properties[:number2][:items][:type]).to eq("number") - - expect(properties[:number3][:type]).to eq("number") - expect(properties[:number3][:enum]).to eq([1, -1, 4, 1]) - - expect(properties[:number4][:type]).to eq("array") - expect(properties[:number4][:items][:type]).to eq("number") - expect(properties[:number4][:items][:enum]).to eq([-1, -2, -3]) - - # Boolean - expect(properties[:boolean1][:type]).to eq("boolean") - expect(properties[:boolean1][:title]).to eq("Boolean1 Title") - expect(properties[:boolean1][:description]).to eq("Boolean1 Description") - expect(properties[:boolean1][:examples]).to eq([true, false]) - - expect(properties[:boolean2][:type]).to eq("array") - expect(properties[:boolean2][:items][:type]).to eq("boolean") - - # Object - expect(properties[:object1][:type]).to eq("object") - expect(properties[:object1][:properties][:obj_test1][:type]).to eq("string") - expect(properties[:object1][:properties][:obj_test2][:type]).to eq("string") - expect(properties[:object1][:required]).to eq([:obj_test2]) - - expect(properties[:object2][:type]).to eq("array") - expect(properties[:object2][:items][:type]).to eq("object") - expect(properties[:object2][:items][:properties][:obj_test1][:type]).to eq("string") - expect(properties[:object2][:items][:properties][:obj_test2][:type]).to eq("string") - expect(properties[:object2][:items][:required]).to eq([:obj_test2]) - - expect(properties[:object3][:type]).to eq("object") - expect(properties[:object3][:properties][:test1][:type]).to eq("string") - expect(properties[:object3][:properties][:test2][:type]).to eq("integer") - expect(properties[:object3][:required]).to eq([:test2]) - - expect(properties[:object4][:type]).to eq("array") - expect(properties[:object4][:items][:type]).to eq("object") - expect(properties[:object4][:items][:properties][:test1][:type]).to eq("string") - expect(properties[:object4][:items][:properties][:test2][:type]).to eq("integer") - expect(properties[:object4][:items][:required]).to eq([:test2]) + describe ".schema" do + it "correctly formats schema" do + expect(subject[:type]).to eq("object") + expect(subject[:required]).to eq(%i[string1 string3 integer1 integer3 number1 number3 boolean1 object1 object3]) + + # String + expect(properties[:string1][:type]).to eq("string") + expect(properties[:string1][:title]).to eq("String1 Title") + expect(properties[:string1][:description]).to eq("String1 Description") + expect(properties[:string1][:examples]).to eq(["Test1 Example"]) + + expect(properties[:string2][:type]).to eq("array") + expect(properties[:string2][:items][:type]).to eq("string") + + expect(properties[:string3][:type]).to eq("string") + expect(properties[:string3][:enum]).to eq(["Test3 A", "Test3 B"]) + + expect(properties[:string4][:type]).to eq("array") + expect(properties[:string4][:items][:type]).to eq("string") + expect(properties[:string4][:items][:enum]).to eq(["Test4 A", "Test4 B"]) + + # Integer + expect(properties[:integer1][:type]).to eq("integer") + expect(properties[:integer1][:title]).to eq("Integer1 Title") + expect(properties[:integer1][:description]).to eq("Integer1 Description") + expect(properties[:integer1][:examples]).to eq([1, 2, 3]) + + expect(properties[:integer2][:type]).to eq("array") + expect(properties[:integer2][:items][:type]).to eq("integer") + + expect(properties[:integer3][:type]).to eq("integer") + expect(properties[:integer3][:enum]).to eq([4, 5, 6]) + + expect(properties[:integer4][:type]).to eq("array") + expect(properties[:integer4][:items][:type]).to eq("integer") + expect(properties[:integer4][:items][:enum]).to eq([7, 8, 9]) + + # Number + expect(properties[:number1][:type]).to eq("number") + expect(properties[:number1][:title]).to eq("Number1 Title") + expect(properties[:number1][:description]).to eq("Number1 Description") + expect(properties[:number1][:examples]).to eq([1, -2, 4]) + + expect(properties[:number2][:type]).to eq("array") + expect(properties[:number2][:items][:type]).to eq("number") + + expect(properties[:number3][:type]).to eq("number") + expect(properties[:number3][:enum]).to eq([1, -1, 4, 1]) + + expect(properties[:number4][:type]).to eq("array") + expect(properties[:number4][:items][:type]).to eq("number") + expect(properties[:number4][:items][:enum]).to eq([-1, -2, -3]) + + # Boolean + expect(properties[:boolean1][:type]).to eq("boolean") + expect(properties[:boolean1][:title]).to eq("Boolean1 Title") + expect(properties[:boolean1][:description]).to eq("Boolean1 Description") + expect(properties[:boolean1][:examples]).to eq([true, false]) + + expect(properties[:boolean2][:type]).to eq("array") + expect(properties[:boolean2][:items][:type]).to eq("boolean") + + # Object + expect(properties[:object1][:type]).to eq("object") + expect(properties[:object1][:properties][:obj_test1][:type]).to eq("string") + expect(properties[:object1][:properties][:obj_test2][:type]).to eq("string") + expect(properties[:object1][:required]).to eq([:obj_test2]) + + expect(properties[:object2][:type]).to eq("array") + expect(properties[:object2][:items][:type]).to eq("object") + expect(properties[:object2][:items][:properties][:obj_test1][:type]).to eq("string") + expect(properties[:object2][:items][:properties][:obj_test2][:type]).to eq("string") + expect(properties[:object2][:items][:required]).to eq([:obj_test2]) + + expect(properties[:object3][:type]).to eq("object") + expect(properties[:object3][:properties][:test1][:type]).to eq("string") + expect(properties[:object3][:properties][:test2][:type]).to eq("integer") + expect(properties[:object3][:required]).to eq([:test2]) + + expect(properties[:object4][:type]).to eq("array") + expect(properties[:object4][:items][:type]).to eq("object") + expect(properties[:object4][:items][:properties][:test1][:type]).to eq("string") + expect(properties[:object4][:items][:properties][:test2][:type]).to eq("integer") + expect(properties[:object4][:items][:required]).to eq([:test2]) + end + end + + describe ".to_json" do + it "returns correct JSON" do + json = TestSchema.to_json + hash = MultiJson.load(json) + expect(hash["type"]).to eq("object") + end end end