json-schema-subset-dsl 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fb5013ebaff6df3b177c3aa955acadd4d3ebb6f163effa1945482f43c6ef659c
4
+ data.tar.gz: 300c9272d2771a22df0bc753b902597d2a45376bc9ba6d487dbed936c3d06ec1
5
+ SHA512:
6
+ metadata.gz: 31e72cb5f90d0db5460b396b0efca43164f2ce097e2cf9a5930fd20e8334e1595df3e6d1cf97a9636623d9653615d953d5cea8b8202f957ce0187ca760782687
7
+ data.tar.gz: b7193dc88e2e8d5f8efdb2fff2a1e545556ad6c55a63b980345005a1a097d107fdb550c41ec6dcd69f777056a312910a9336b326d58b64334e114b62747ffdb5
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - name: Set up Ruby 2.x
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.x
16
+ - name: Build and test with Rake
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ bundle exec rake
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
@@ -0,0 +1,3 @@
1
+ printWidth: 120
2
+ addTrailingCommas: true
3
+ preferSingleQuotes: false
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --require rspec-power_assert
4
+ --require spec_helper
@@ -0,0 +1,25 @@
1
+ inherit_from:
2
+ - .rubocop_airbnb.yml
3
+ inherit_gem:
4
+ rubocop-config-prettier: config/rubocop.yml
5
+ # prettier
6
+ Style/PercentLiteralDelimiters:
7
+ Description: Use `%`-literal delimiters consistently
8
+ StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#percent-literal-braces
9
+ Enabled: false
10
+ PreferredDelimiters:
11
+ ! '%': ()
12
+ ! '%i': ()
13
+ ! '%q': ()
14
+ ! '%Q': ()
15
+ ! '%r': ! '{}'
16
+ ! '%s': ()
17
+ ! '%w': ()
18
+ ! '%W': ()
19
+ ! '%x': ()
20
+ Airbnb/SimpleModifierConditional:
21
+ Enabled: false
22
+ Airbnb/OptArgParameters:
23
+ Enabled: false
24
+ RSpec/EmptyExampleGroup:
25
+ Enabled: false
@@ -0,0 +1,2 @@
1
+ require:
2
+ - rubocop-airbnb
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in json-schema-subset-dsl.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Narazaka
2
+
3
+ This software is provided 'as-is', without any express or implied
4
+ warranty. In no event will the authors be held liable for any damages
5
+ arising from the use of this software.
6
+
7
+ Permission is granted to anyone to use this software for any purpose,
8
+ including commercial applications, and to alter it and redistribute it
9
+ freely, subject to the following restrictions:
10
+
11
+ 1. The origin of this software must not be misrepresented; you must not
12
+ claim that you wrote the original software. If you use this software
13
+ in a product, an acknowledgment in the product documentation would be
14
+ appreciated but is not required.
15
+
16
+ 2. Altered source versions must be plainly marked as such, and must not be
17
+ misrepresented as being the original software.
18
+
19
+ 3. This notice may not be removed or altered from any source
20
+ distribution.
@@ -0,0 +1,79 @@
1
+ # Json::Schema::Subset::DSL
2
+
3
+ Yet another JSON Schema subset DSL.
4
+
5
+ Useful when writing a simple JSON schema.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json-schema-subset-dsl'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json-schema-subset-dsl
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ dsl = Json::Schema::Subset::DSL.new do
27
+ title! "Example"
28
+ id :integer
29
+ name :string, minLength: 1, optional: true
30
+ items :array, optional: true do
31
+ end
32
+ other_names :array, optional: true do
33
+ string!
34
+ null!
35
+ end
36
+ meta :object do
37
+ description :string
38
+ params :array do
39
+ ref! "#/components/Param"
40
+ end
41
+ opt_params :array do
42
+ cref! "OptParam"
43
+ end
44
+ uuid :ref, "#/UUID", optional: true
45
+ end
46
+ end
47
+
48
+ dsl.compile! == {
49
+ "type" => "object",
50
+ "properties" => {
51
+ "id" => { "type" => "integer" },
52
+ "name" => { "minLength" => 1, "type" => "string" },
53
+ "items" => { "items" => { "type" => "object", "properties" => {} }, "type" => "array" },
54
+ "other_names" => { "items" => { "type" => %w[string null] }, "type" => "array" },
55
+ "meta" => {
56
+ "type" => "object",
57
+ "properties" => {
58
+ "description" => { "type" => "string" },
59
+ "params" => { "items" => { "$ref" => "#/components/Param" }, "type" => "array" },
60
+ "opt_params" => { "items" => { "$ref" => "#/components/OptParam" }, "type" => "array" },
61
+ "uuid" => { "$ref" => "#/UUID" },
62
+ },
63
+ "required" => %w[description params opt_params],
64
+ },
65
+ },
66
+ "required" => %w[id meta],
67
+ "title" => "Example",
68
+ }
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Narazaka/json-schema-subset-dsl.
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "yard"
4
+ require "yard/rake/yardoc_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ YARD::Rake::YardocTask.new
8
+
9
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json/schema/subset/dsl"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/fmt ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle exec rubocop -a
7
+ bundle exec rbprettier --write "**/*.rb" "**/*.rake" "**/*.gemspec"
8
+
9
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "json/schema/subset/dsl/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "json-schema-subset-dsl"
7
+ spec.version = Json::Schema::Subset::DSL::VERSION
8
+ spec.authors = %w[Narazaka]
9
+ spec.email = %w[info@narazaka.net]
10
+ spec.licenses = %w[Zlib]
11
+
12
+ spec.summary = "Yet another JSON Schema subset DSL"
13
+ spec.description = "Useful when writing a simple JSON schema."
14
+ spec.homepage = "https://github.com/#{spec.authors.first}/#{spec.name}"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "#{spec.homepage}.git"
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
19
+ spec.metadata["documentation_uri"] = "https://www.rubydoc.info/gems/#{spec.name}/#{spec.version}"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files =
24
+ Dir.chdir(File.expand_path("..", __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = %w[lib]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2"
32
+ spec.add_development_dependency "rake", "~> 10.5"
33
+ spec.add_development_dependency "rspec", "~> 3.9"
34
+ spec.add_development_dependency "rspec-power_assert", "~> 1.1"
35
+ spec.add_development_dependency "rubocop", "~> 0.76"
36
+ spec.add_development_dependency "rubocop-airbnb", "~> 3"
37
+ spec.add_development_dependency "prettier", ">= 0.16"
38
+ spec.add_development_dependency "rubocop-config-prettier", "~> 0.1"
39
+ spec.add_development_dependency "pry-byebug", "~> 3.7"
40
+ spec.add_development_dependency "yard", "~> 0.9"
41
+ end
@@ -0,0 +1,120 @@
1
+ require "json/schema/subset/dsl/version"
2
+
3
+ module Json
4
+ module Schema
5
+ module Subset
6
+ class DSL
7
+ def initialize(type: "object", schema: {}, params: {}, ref: nil, &block)
8
+ @type = type
9
+ @schema = schema
10
+ @params = params
11
+ @ref = ref
12
+ @optionals = []
13
+ set!(&block) if block_given?
14
+ end
15
+
16
+ def set!(&block)
17
+ instance_eval(&block)
18
+ end
19
+
20
+ def compile!
21
+ return { "$ref" => @ref } if @ref
22
+
23
+ case
24
+ when Array(@type).include?("array")
25
+ ret = @schema.dup
26
+ ret["type"] = @type
27
+ ret["items"] = ret["items"].compile!
28
+ ret.merge(@params)
29
+ when Array(@type).include?("object")
30
+ required = @schema.keys - @optionals
31
+ ret = { "type" => @type, "properties" => @schema.map { |k, v| [k, v.compile!] }.to_h }
32
+ ret["required"] = required unless required.empty?
33
+ ret.merge(@params)
34
+ else
35
+ @schema.merge("type" => @type).map { |k, v| [k.to_s, v] }.to_h.merge(@params)
36
+ end
37
+ end
38
+
39
+ # toplevel
40
+
41
+ %w[string integer number boolean null].each do |type|
42
+ define_method(:"#{type}!") do |args = {}|
43
+ change_type!(type)
44
+ @schema.merge!(args)
45
+ end
46
+ end
47
+
48
+ def ref!(name)
49
+ @ref = name.to_s
50
+ end
51
+
52
+ def cref!(name)
53
+ ref! components!(name)
54
+ end
55
+
56
+ def dref!(name)
57
+ ref! definitions!(name)
58
+ end
59
+
60
+ def components!(name)
61
+ "#/components/#{name}"
62
+ end
63
+
64
+ def definitions!(name)
65
+ "#/definitions/#{name}"
66
+ end
67
+
68
+ def array!(&block)
69
+ change_type!("array")
70
+ @schema["items"] = DSL.new(&block)
71
+ end
72
+
73
+ def respond_to_missing?(name, include_private)
74
+ true
75
+ end
76
+
77
+ def method_missing(name, *args, &block)
78
+ if name.to_s.end_with?("!")
79
+ @params[name[0...-1]] = args[0]
80
+ return
81
+ end
82
+
83
+ type = args.first
84
+ opts = args.count > 1 && args.last.is_a?(Hash) ? args.last || {} : {}
85
+
86
+ optional = opts.delete(:optional)
87
+ @optionals << name.to_s if optional
88
+ type = type.is_a?(Array) ? type.map(&:to_s) : type.to_s
89
+ case
90
+ when type == "ref"
91
+ @schema[name.to_s] = DSL.new(ref: args[1])
92
+ when type == "cref"
93
+ @schema[name.to_s] = DSL.new(ref: components!(args[1]))
94
+ when type == "dref"
95
+ @schema[name.to_s] = DSL.new(ref: definitions!(args[1]))
96
+ when Array(type).include?("array")
97
+ @schema[name.to_s] = DSL.new(type: type, params: opts, schema: { "items" => DSL.new(&block) })
98
+ when Array(type).include?("object")
99
+ @schema[name.to_s] = DSL.new(type: type, params: opts, &block)
100
+ else
101
+ @schema[name.to_s] = DSL.new(type: type, schema: opts)
102
+ end
103
+ end
104
+
105
+ def change_type!(type)
106
+ case @type
107
+ when "object"
108
+ if type == "null"
109
+ @type = %w[object null]
110
+ else
111
+ @type = type
112
+ end
113
+ else
114
+ @type = Array(@type) + [type]
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,9 @@
1
+ module Json
2
+ module Schema
3
+ module Subset
4
+ class DSL
5
+ VERSION = "1.0.0".freeze
6
+ end
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-schema-subset-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Narazaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-power_assert
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.76'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.76'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-airbnb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: prettier
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0.16'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-config-prettier
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.1'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.9'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.9'
153
+ description: Useful when writing a simple JSON schema.
154
+ email:
155
+ - info@narazaka.net
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".github/workflows/ruby.yml"
161
+ - ".gitignore"
162
+ - ".prettierrc"
163
+ - ".rspec"
164
+ - ".rubocop.yml"
165
+ - ".rubocop_airbnb.yml"
166
+ - ".travis.yml"
167
+ - Gemfile
168
+ - LICENSE
169
+ - README.md
170
+ - Rakefile
171
+ - bin/console
172
+ - bin/fmt
173
+ - bin/setup
174
+ - json-schema-subset-dsl.gemspec
175
+ - lib/json/schema/subset/dsl.rb
176
+ - lib/json/schema/subset/dsl/version.rb
177
+ homepage: https://github.com/Narazaka/json-schema-subset-dsl
178
+ licenses:
179
+ - Zlib
180
+ metadata:
181
+ homepage_uri: https://github.com/Narazaka/json-schema-subset-dsl
182
+ source_code_uri: https://github.com/Narazaka/json-schema-subset-dsl.git
183
+ changelog_uri: https://github.com/Narazaka/json-schema-subset-dsl/blob/master/CHANGELOG.md
184
+ documentation_uri: https://www.rubydoc.info/gems/json-schema-subset-dsl/1.0.0
185
+ post_install_message:
186
+ rdoc_options: []
187
+ require_paths:
188
+ - lib
189
+ required_ruby_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubygems_version: 3.0.3
201
+ signing_key:
202
+ specification_version: 4
203
+ summary: Yet another JSON Schema subset DSL
204
+ test_files: []