Skip to content

Commit

Permalink
Use multi_json
Browse files Browse the repository at this point in the history
  • Loading branch information
mooktakim committed Sep 20, 2024
1 parent be92b48 commit 7bb71b8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 88 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
simple_json_schema_builder (0.1.0)
multi_json (~> 1.0)

GEM
remote: https://rubygems.org/
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/simple_json_schema_builder/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "multi_json"

module SimpleJsonSchemaBuilder
class Base
def self.object(&block)
Expand All @@ -12,7 +14,7 @@ def self.schema
end

def self.to_json
schema.to_json
MultiJson.dump(schema)
end

def initialize
Expand Down
9 changes: 5 additions & 4 deletions simple_json_schema_builder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
176 changes: 93 additions & 83 deletions spec/simple_json_schema_builder/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7bb71b8

Please sign in to comment.