pair-kit-json-schema-dsl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +4 -0
  3. data/Gemfile +7 -0
  4. data/LICENSE +20 -0
  5. data/README.md +43 -0
  6. data/Rakefile +21 -0
  7. data/lib/pair_kit/json_schema/dsl/nodes/array_dsl.rb +50 -0
  8. data/lib/pair_kit/json_schema/dsl/nodes/array_item_dsl.rb +32 -0
  9. data/lib/pair_kit/json_schema/dsl/nodes/boolean_dsl.rb +12 -0
  10. data/lib/pair_kit/json_schema/dsl/nodes/number_dsl.rb +13 -0
  11. data/lib/pair_kit/json_schema/dsl/nodes/object_dsl.rb +51 -0
  12. data/lib/pair_kit/json_schema/dsl/nodes/property_dsl.rb +25 -0
  13. data/lib/pair_kit/json_schema/dsl/nodes/schema_dsl.rb +15 -0
  14. data/lib/pair_kit/json_schema/dsl/nodes/string_dsl.rb +36 -0
  15. data/lib/pair_kit/json_schema/dsl/partials/common_partial_dsl.rb +17 -0
  16. data/lib/pair_kit/json_schema/dsl/partials/const_partial_dsl.rb +16 -0
  17. data/lib/pair_kit/json_schema/dsl/partials/core_partial_dsl.rb +48 -0
  18. data/lib/pair_kit/json_schema/dsl/partials/enum_partial_dsl.rb +18 -0
  19. data/lib/pair_kit/json_schema/dsl/partials/meta_data_partial_dsl.rb +48 -0
  20. data/lib/pair_kit/json_schema/dsl/partials/null_partial_dsl.rb +17 -0
  21. data/lib/pair_kit/json_schema/dsl/partials/numeric_partial_dsl.rb +40 -0
  22. data/lib/pair_kit/json_schema/dsl/partials/structural_partial_dsl.rb +66 -0
  23. data/lib/pair_kit/json_schema/dsl/partials/type_partial_dsl.rb +73 -0
  24. data/lib/pair_kit/json_schema/dsl/scaffolds/array_scaffold.rb +39 -0
  25. data/lib/pair_kit/json_schema/dsl/scaffolds/hash_scaffold.rb +28 -0
  26. data/lib/pair_kit/json_schema/dsl/scaffolds/scaffold.rb +16 -0
  27. data/lib/pair_kit/json_schema/dsl/scaffolds/scalar_scaffold.rb +26 -0
  28. data/lib/pair_kit/json_schema/dsl/version.rb +9 -0
  29. data/lib/pair_kit/json_schema/dsl.rb +51 -0
  30. metadata +274 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dfd42349dd23c72480ba3c5b4f8a2f0dafa44da7fd727631d4436adf7f53ca94
4
+ data.tar.gz: 6fe039181406355731f7ce88f1253ceb27b19eb98e5e3c68b21156e0cb68bdd4
5
+ SHA512:
6
+ metadata.gz: 82bf6dd7b02c0d69a4640fb607d041e0bd6dd0d0a1f64b4c0f30d46caeab6d9cd6fb08a7960aec30cb7e621332b9008c4816440dcf151338a6059a410f6340a7
7
+ data.tar.gz: a0e9242e02fa8433d1086b6f9d86f225d9dd51a8a6ba28aeea26697c4bb2734bd251c7299b5e3e1ca28b8688bfa67cd44f13cb2e17a2c482915078338f598257
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (01-Jul-24)
4
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 PairFinance team, Dmitry Sharkov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # PairKit::JsonSchema::Dsl
2
+
3
+ Ruby DSL to build Json Schemas
4
+
5
+ ---
6
+ **NOTE**
7
+
8
+ This gem is under heavy developmetn. Don't use it now, wait until v.0.1.0
9
+
10
+ ---
11
+
12
+
13
+ ## Usage
14
+
15
+ In your ```Gemfile```:
16
+
17
+ ```ruby
18
+ gem 'pair_kit_json_schema_dsl'
19
+ ```
20
+
21
+ In your code:
22
+
23
+ ```ruby
24
+ require 'pari_kit/json_schema/dsl'
25
+ ```
26
+
27
+ ### Example
28
+
29
+ ```ruby
30
+ dsl = PairKit::JsonSchema::Dsl.new
31
+
32
+ array_of_strings = dsl.build { arr { str } }
33
+
34
+ simple_object = dsl.build do
35
+ object do
36
+ prop(:first_name).required.str
37
+ prop(:last_name).required.str
38
+ prop(:middle_name).str.null
39
+ end
40
+ end
41
+
42
+ ```
43
+
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ Bundler::GemHelper.install_tasks
6
+ rescue LoadError
7
+ puts 'although not required, bundler is recommended for running the tests'
8
+ end
9
+
10
+ task default: :spec
11
+
12
+ require 'rspec/core/rake_task'
13
+
14
+ RSpec::Core::RakeTask.new(:spec)
15
+
16
+ require 'rubocop/rake_task'
17
+
18
+ RuboCop::RakeTask.new do |task|
19
+ task.requires << 'rubocop-performance'
20
+ task.requires << 'rubocop-rspec'
21
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module ArrayDsl
7
+ include CommonPartialDsl
8
+ include(TypePartialDsl.apply_to { @subject['items', {}] })
9
+
10
+ def items(&block)
11
+ @dsl.call(@subject, builder: :array_item, name: 'items', min_max: 'Items', &block)
12
+ end
13
+
14
+ def prefix_item(&block)
15
+ subject['prefixItems', []] << {}
16
+ @dsl.call(@subject['prefixItems'].last, builder: :schema, &block)
17
+ end
18
+
19
+ def contains(&block)
20
+ @dsl.call(@subject, builder: :array_item, name: 'contains', min_max: 'Contains', &block)
21
+ end
22
+
23
+ def min_items(val)
24
+ @subject['minItems'] = val
25
+ end
26
+
27
+ def max_items(val)
28
+ @subject['maxItems'] = val
29
+ end
30
+
31
+ def max_contains(val)
32
+ @subject['maxContains'] = val
33
+ end
34
+
35
+ def min_contains(val)
36
+ @subject['minContains'] = val
37
+ end
38
+
39
+ def unique_items
40
+ @subject['uniqueItems'] = true
41
+ end
42
+
43
+ def duplicated_items
44
+ @subject['uniqueItems'] = false
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ ##
7
+ # This makes is possible to write something like
8
+ ##
9
+ # array.items.string
10
+ # array.items.min(10).max(20).string
11
+ # array.items { string }.min_items(10).max_items(20)
12
+ # array.items { string; min(10); max(20) }.unique
13
+ ##
14
+ # array.contains.string
15
+ # array.contains.min(10).max(20).string
16
+ # array.contains { string }.min_contains(10).max_contains(20)
17
+ # array.contains { string; min(10); max(20) }.unique
18
+ #
19
+ module ArrayItemDsl
20
+ include(TypePartialDsl.apply_to { @subject[@options[:name]] })
21
+
22
+ def max(val)
23
+ @subject["max#{@options[:min_max]}"] = val
24
+ end
25
+
26
+ def min(val)
27
+ @subject["max#{@options[:min_max]}"] = val
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module BooleanDsl
7
+ include CommonPartialDsl
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module NumberDsl
7
+ include CommonPartialDsl
8
+ include NumericPartialDsl
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://www.learnjsonschema.com/2020-12/applicator/
7
+ module ObjectDsl
8
+ include CommonPartialDsl
9
+
10
+ def prop(name, &block)
11
+ @subject['properties', {}][name] = {}
12
+ @dsl.call(@subject, builder: :property, name: name, &block)
13
+ end
14
+
15
+ def pattern_props
16
+
17
+ end
18
+
19
+ def additional_properties
20
+
21
+ end
22
+
23
+ def prop_names
24
+
25
+ end
26
+
27
+ def dependent_schemas
28
+
29
+ end
30
+
31
+ def min_props(val)
32
+ @subject['minLength'] = val
33
+ end
34
+
35
+ def dependency(name, *dependent)
36
+ @subject['dependentRequired', {}][name, []] += dependent
37
+ end
38
+
39
+ def max_props(val)
40
+ @subject['maxLength'] = val
41
+ end
42
+
43
+ def required(*props)
44
+ @subject['required', []] += props
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://json-schema.org/understanding-json-schema/reference/object
7
+ module PropertyDsl
8
+ include TypePartialDsl.apply_to { @subject['properties'][@options[:name]] }
9
+
10
+ def required
11
+ @subject['required', []] << Miniflector.camel_case(@options[:name])
12
+ end
13
+
14
+ def optional
15
+ @subject['required', []].delete(Miniflector.camel_case(@options[:name]))
16
+ end
17
+
18
+ def dependent_on(*dependencies)
19
+ @subject['dependentRequired', {}][@options[:name], []] += dependencies
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://json-schema.org/understanding-json-schema/reference/object
7
+ module SchemaDsl
8
+ include CommonPartialDsl
9
+ include(TypePartialDsl.apply_to { @subject })
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://www.learnjsonschema.com/2020-12/validation/
7
+ module StringDsl
8
+ include CommonPartialDsl
9
+
10
+ def min_len(val)
11
+ @subject['minLength'] = val
12
+ end
13
+
14
+ alias len_gteq min_len
15
+
16
+ def pattern(re)
17
+ @subject['pattern'] = re
18
+ end
19
+
20
+ alias re pattern
21
+
22
+ def max_len(val)
23
+ @subject['maxLength'] = val
24
+ end
25
+
26
+ alias len_lteq max_len
27
+
28
+ def len_eq(val)
29
+ min_len(val)
30
+ max_len(val)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module CommonPartialDsl
7
+ include CorePartialDsl
8
+ include MetaDataPartialDsl
9
+ include ConstPartialDsl
10
+ include EnumPartialDsl
11
+ include StructuralPartialDsl
12
+ include NullPartialDsl
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module ConstPartialDsl
7
+ def const(value)
8
+ @subject['const'] = value
9
+ end
10
+
11
+ alias eq const
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module CorePartialDsl
7
+ def schema(schema)
8
+ @subject['$schema'] = schema
9
+ end
10
+
11
+ def id(id)
12
+ @subject['$id'] = id
13
+ end
14
+
15
+ def ref(ref)
16
+ @subject['$ref'] = ref
17
+ end
18
+
19
+ def comment(comment)
20
+ @subject['$comment'] = comment
21
+ end
22
+
23
+ def define(name, &block)
24
+ @dsl.(@subject['$defs', {}][name, {}], &block)
25
+ end
26
+
27
+ alias dfn define
28
+
29
+ def anchor(anchor)
30
+ @subject['$anchor'] = anchor
31
+ end
32
+
33
+ def dynamic_anchor(anchor)
34
+ @subject['$dynamicAnchor'] = anchor
35
+ end
36
+
37
+ def dynamic_ref(ref)
38
+ @subject['$dynamicRef'] = ref
39
+ end
40
+
41
+ def vocabulary(vocab)
42
+ @subject['$vocabulary', {}][vocab] = true
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://www.learnjsonschema.com/2020-12/validation/enum/
7
+ module EnumPartialDsl
8
+ def enum(*args)
9
+ @subject['enum', []] += args
10
+ end
11
+
12
+ alias in enum
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://www.learnjsonschema.com/2020-12/meta-data/
7
+ module MetaDataPartialDsl
8
+ def title(title)
9
+ @subject['title'] = title
10
+ end
11
+
12
+ def description(description = nil, &block)
13
+ @subject['description'] = description || block&.call
14
+ end
15
+
16
+ def default(val)
17
+ @subject['default'] = val
18
+ end
19
+
20
+ def deprecated
21
+ @subject['deprecated'] = true
22
+ end
23
+
24
+ def example(example = nil, &block)
25
+ @subject['examples', []] << example || block&.call
26
+ end
27
+
28
+ def read_only
29
+ @subject['readOnly'] = true
30
+ end
31
+
32
+ def writable
33
+ @subject['readOnly'] = false
34
+ end
35
+
36
+ def write_only
37
+ @subject['writeOnly'] = true
38
+ end
39
+
40
+ def readable
41
+ @subject['writeOnly'] = false
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ # See https://www.learnjsonschema.com/2020-12/validation/const/
7
+
8
+ module NullPartialDsl
9
+ def null
10
+ @subject['type'] << 'null'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module NumericPartialDsl
7
+ def gt(val)
8
+ @subject['exclusiveMinimum'] = val
9
+ end
10
+
11
+ alias exclusive_minimum gt
12
+
13
+ def gteq(val)
14
+ @subject['minimum'] = val
15
+ end
16
+
17
+ alias minimum gteq
18
+
19
+ def lt(val)
20
+ @subject['exclusiveMaximum'] = val
21
+ end
22
+
23
+ alias exclusive_maximum lt
24
+
25
+ def lteq(val)
26
+ @subject['maximum'] = val
27
+ end
28
+
29
+ alias maximum lteq
30
+
31
+ def mult_of(val)
32
+ @subject['multipleOf'] = val
33
+ end
34
+
35
+ alias multiple_of mult_of
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module StructuralPartialDsl
7
+ def any_of(&block)
8
+ append_applicator('anyOf', &block)
9
+ end
10
+
11
+ alias may_be any_of
12
+ alias or any_of
13
+
14
+ def one_of(&block)
15
+ append_applicator('anyOf', &block)
16
+ end
17
+
18
+ alias may_be_once one_of
19
+ alias xor one_of
20
+
21
+ def all_of(&block)
22
+ append_applicator('allOf', &block)
23
+ end
24
+
25
+ alias must_be all_of
26
+ alias and all_of
27
+
28
+ def _then(&block)
29
+ use_applicator('then', &block)
30
+ end
31
+
32
+ alias then_check _then
33
+
34
+ def _if(&block)
35
+ use_applicator('if', &block)
36
+ end
37
+
38
+ alias if_fits _if
39
+
40
+ def _else(&block)
41
+ use_applicator('else', &block)
42
+ end
43
+
44
+ alias else_check _else
45
+
46
+ def _not(&block)
47
+ use_applicator('not', &block)
48
+ end
49
+
50
+ alias must_not_be _not
51
+ alias prohibit _not
52
+
53
+ private
54
+
55
+ def append_applicator(name, &block)
56
+ @dsl.call(@subject[name, []] << {}, builder: @options[:builder], &block)
57
+ end
58
+
59
+ def use_applicator(name, &block)
60
+ @dsl.call(@subject[name, {}], builder: @options[:builder], &block)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ module TypePartialDsl
7
+ module Base
8
+ def str(&block)
9
+ type!('string', &block)
10
+ end
11
+
12
+ alias string str
13
+
14
+ def int(&block)
15
+ type!('integer', &block)
16
+ end
17
+
18
+ alias integer int
19
+
20
+ def bool(&block)
21
+ type!('boolean', &block)
22
+ end
23
+
24
+ alias boolean bool
25
+
26
+ def num(&block)
27
+ type!('number', &block)
28
+ end
29
+
30
+ alias number num
31
+ alias float num
32
+ alias flt num
33
+
34
+ def obj(&block)
35
+ type!('object', &block)
36
+ end
37
+
38
+ alias object obj
39
+ alias struct obj
40
+
41
+ def arr(&block)
42
+ type!('array', &block)
43
+ end
44
+
45
+ alias array arr
46
+
47
+ def null
48
+ type!('null')
49
+ end
50
+
51
+ private
52
+
53
+ def type!(type, &block)
54
+ _subject_['type'] << type
55
+ @dsl.call(_subject_, builder: type, &block) if block
56
+ end
57
+ end
58
+
59
+ def self.apply_to(&subject_resolver)
60
+ Module.new do
61
+ include Base
62
+
63
+ private
64
+
65
+ define_method(:_subject_, &subject_resolver)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+
@@ -0,0 +1,39 @@
1
+ module PairKit
2
+ class JsonSchema
3
+ class Dsl
4
+ class ArrayScaffold < Scaffold
5
+ def initialize(val)
6
+ @array = val.is_a?(ArrayScaffold) ? val.array : val.map { |x| Scaffold.wrap(x) }
7
+ end
8
+
9
+
10
+ def <<(item)
11
+ @array << Scaffold.wrap(item)
12
+ end
13
+
14
+ def to_schema
15
+ @array.map(&:to_schema)
16
+ end
17
+
18
+ def first
19
+ @array.first
20
+ end
21
+
22
+ def last
23
+ @array.last
24
+ end
25
+
26
+ def +(arr)
27
+ ArrayScaffold.new([]).tap do |scf|
28
+ scf.array = array + ArrayScaffold.new(arr).array
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ attr_accessor :array
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,28 @@
1
+ module PairKit
2
+ class JsonSchema
3
+ class Dsl
4
+ class HashScaffold < Scaffold
5
+ def initialize(val)
6
+ @hash = val
7
+ end
8
+
9
+ def [](name, default = nil)
10
+ name = Miniflector.camel_case(name)
11
+ return @hash[name] if @hash.key?(name)
12
+
13
+ @hash[name] = Scaffold.wrap(default)
14
+ end
15
+
16
+ def []=(name, val)
17
+ name = Miniflector.camel_case(name)
18
+ @hash[name] = Scaffold.wrap(val)
19
+ end
20
+
21
+ def to_schema
22
+ @hash.transform_values(&:to_schema)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,16 @@
1
+ module PairKit
2
+ class JsonSchema
3
+ class Dsl
4
+ class Scaffold
5
+ def self.wrap(val)
6
+ case val
7
+ when Scaffold then val
8
+ when Hash then HashScaffold.new(val)
9
+ when Array then ArrayScaffold.new(val)
10
+ else ScalarScaffold.new(val)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module PairKit
2
+ class JsonSchema
3
+ class Dsl
4
+ class ScalarScaffold < Scaffold
5
+ def initialize(val)
6
+ @value = val
7
+ end
8
+
9
+ def <<(val)
10
+ if @value
11
+ @value.is_a?(Array) ? @value << val : @value = [@value, val]
12
+ else
13
+ @value = val
14
+ end
15
+ end
16
+
17
+ def to_schema
18
+ @value
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ VERSION = '0.0.1'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PairKit
4
+ class JsonSchema
5
+ class Dsl
6
+ require 'pair_kit/dsl_factory'
7
+ require 'pair_kit/miniflector'
8
+
9
+ require_relative 'dsl/version'
10
+
11
+ require_relative 'dsl/scaffolds/scaffold'
12
+ require_relative 'dsl/scaffolds/hash_scaffold'
13
+ require_relative 'dsl/scaffolds/array_scaffold'
14
+ require_relative 'dsl/scaffolds/scalar_scaffold'
15
+
16
+ require_relative 'dsl/partials/const_partial_dsl'
17
+ require_relative 'dsl/partials/core_partial_dsl'
18
+ require_relative 'dsl/partials/enum_partial_dsl'
19
+ require_relative 'dsl/partials/meta_data_partial_dsl'
20
+ require_relative 'dsl/partials/null_partial_dsl'
21
+ require_relative 'dsl/partials/numeric_partial_dsl'
22
+ require_relative 'dsl/partials/structural_partial_dsl'
23
+ require_relative 'dsl/partials/type_partial_dsl'
24
+ require_relative 'dsl/partials/common_partial_dsl'
25
+
26
+ require_relative 'dsl/nodes/array_dsl'
27
+ require_relative 'dsl/nodes/array_item_dsl'
28
+ require_relative 'dsl/nodes/boolean_dsl'
29
+ require_relative 'dsl/nodes/number_dsl'
30
+ require_relative 'dsl/nodes/object_dsl'
31
+ require_relative 'dsl/nodes/property_dsl'
32
+ require_relative 'dsl/nodes/schema_dsl'
33
+ require_relative 'dsl/nodes/string_dsl'
34
+
35
+ BASE_DSL = DslFactory.new do
36
+ configure_builder :schema, SchemaDsl
37
+ configure_builder :array, ArrayDsl
38
+ configure_builder :object, ObjectDsl
39
+ configure_builder :number, NumberDsl
40
+ configure_builder :integer, NumberDsl
41
+ configure_builder :string, StringDsl
42
+ configure_builder :property, PropertyDsl
43
+ configure_builder :array_item, ArrayItemDsl
44
+ end
45
+
46
+ def build(&block)
47
+ BASE_DSL.build(Scaffold.wrap({}), &block).to_schema
48
+ end
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,274 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pair-kit-json-schema-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Sharkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pair-kit-dsl-factory
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: pair-kit-miniflector
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.0.1
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.0.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: pry-byebug
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.10'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.10.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.10'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.10.1
73
+ - !ruby/object:Gem::Dependency
74
+ name: pry-alias
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.0'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.0.1
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.0'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.0.1
93
+ - !ruby/object:Gem::Dependency
94
+ name: rubocop
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '1.64'
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - "~>"
105
+ - !ruby/object:Gem::Version
106
+ version: '1.64'
107
+ - !ruby/object:Gem::Dependency
108
+ name: rubocop-performance
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '1.20'
114
+ type: :development
115
+ prerelease: false
116
+ version_requirements: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - "~>"
119
+ - !ruby/object:Gem::Version
120
+ version: '1.20'
121
+ - !ruby/object:Gem::Dependency
122
+ name: rubocop-rspec
123
+ requirement: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '3.0'
128
+ type: :development
129
+ prerelease: false
130
+ version_requirements: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - "~>"
133
+ - !ruby/object:Gem::Version
134
+ version: '3.0'
135
+ - !ruby/object:Gem::Dependency
136
+ name: rake
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '13.0'
142
+ type: :development
143
+ prerelease: false
144
+ version_requirements: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - "~>"
147
+ - !ruby/object:Gem::Version
148
+ version: '13.0'
149
+ - !ruby/object:Gem::Dependency
150
+ name: rspec
151
+ requirement: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - "~>"
154
+ - !ruby/object:Gem::Version
155
+ version: '3.4'
156
+ type: :development
157
+ prerelease: false
158
+ version_requirements: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - "~>"
161
+ - !ruby/object:Gem::Version
162
+ version: '3.4'
163
+ - !ruby/object:Gem::Dependency
164
+ name: rspec-rails
165
+ requirement: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '3.4'
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 3.4.2
173
+ type: :development
174
+ prerelease: false
175
+ version_requirements: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: '3.4'
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: 3.4.2
183
+ - !ruby/object:Gem::Dependency
184
+ name: simplecov
185
+ requirement: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - "~>"
188
+ - !ruby/object:Gem::Version
189
+ version: '0.19'
190
+ type: :development
191
+ prerelease: false
192
+ version_requirements: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - "~>"
195
+ - !ruby/object:Gem::Version
196
+ version: '0.19'
197
+ - !ruby/object:Gem::Dependency
198
+ name: simplecov-cobertura
199
+ requirement: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - "~>"
202
+ - !ruby/object:Gem::Version
203
+ version: '2.1'
204
+ type: :development
205
+ prerelease: false
206
+ version_requirements: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - "~>"
209
+ - !ruby/object:Gem::Version
210
+ version: '2.1'
211
+ description: This gem helps to build JSON schemas
212
+ email:
213
+ - dmitry.sharkov@gmail.com
214
+ - dmitry.sharkov@pairfinance.com
215
+ executables: []
216
+ extensions: []
217
+ extra_rdoc_files:
218
+ - README.md
219
+ files:
220
+ - CHANGELOG.md
221
+ - Gemfile
222
+ - LICENSE
223
+ - README.md
224
+ - Rakefile
225
+ - lib/pair_kit/json_schema/dsl.rb
226
+ - lib/pair_kit/json_schema/dsl/nodes/array_dsl.rb
227
+ - lib/pair_kit/json_schema/dsl/nodes/array_item_dsl.rb
228
+ - lib/pair_kit/json_schema/dsl/nodes/boolean_dsl.rb
229
+ - lib/pair_kit/json_schema/dsl/nodes/number_dsl.rb
230
+ - lib/pair_kit/json_schema/dsl/nodes/object_dsl.rb
231
+ - lib/pair_kit/json_schema/dsl/nodes/property_dsl.rb
232
+ - lib/pair_kit/json_schema/dsl/nodes/schema_dsl.rb
233
+ - lib/pair_kit/json_schema/dsl/nodes/string_dsl.rb
234
+ - lib/pair_kit/json_schema/dsl/partials/common_partial_dsl.rb
235
+ - lib/pair_kit/json_schema/dsl/partials/const_partial_dsl.rb
236
+ - lib/pair_kit/json_schema/dsl/partials/core_partial_dsl.rb
237
+ - lib/pair_kit/json_schema/dsl/partials/enum_partial_dsl.rb
238
+ - lib/pair_kit/json_schema/dsl/partials/meta_data_partial_dsl.rb
239
+ - lib/pair_kit/json_schema/dsl/partials/null_partial_dsl.rb
240
+ - lib/pair_kit/json_schema/dsl/partials/numeric_partial_dsl.rb
241
+ - lib/pair_kit/json_schema/dsl/partials/structural_partial_dsl.rb
242
+ - lib/pair_kit/json_schema/dsl/partials/type_partial_dsl.rb
243
+ - lib/pair_kit/json_schema/dsl/scaffolds/array_scaffold.rb
244
+ - lib/pair_kit/json_schema/dsl/scaffolds/hash_scaffold.rb
245
+ - lib/pair_kit/json_schema/dsl/scaffolds/scaffold.rb
246
+ - lib/pair_kit/json_schema/dsl/scaffolds/scalar_scaffold.rb
247
+ - lib/pair_kit/json_schema/dsl/version.rb
248
+ homepage: https://github.com/pair-finance/pair_kit_json_schemas_dsl
249
+ licenses:
250
+ - MIT
251
+ metadata:
252
+ homepage_uri: https://github.com/pair-finance/pair_kit_json_schemas_dsl
253
+ source_code_uri: https://github.com/pair-finance/pair_kit_json_schemas_dsl
254
+ changelog_uri: https://github.com/pair-finance/pair_kit_json_schemas_dsl/blob/main/CHANGELOG.md
255
+ post_install_message:
256
+ rdoc_options: []
257
+ require_paths:
258
+ - lib
259
+ required_ruby_version: !ruby/object:Gem::Requirement
260
+ requirements:
261
+ - - ">="
262
+ - !ruby/object:Gem::Version
263
+ version: 2.5.0
264
+ required_rubygems_version: !ruby/object:Gem::Requirement
265
+ requirements:
266
+ - - ">="
267
+ - !ruby/object:Gem::Version
268
+ version: '0'
269
+ requirements: []
270
+ rubygems_version: 3.1.6
271
+ signing_key:
272
+ specification_version: 4
273
+ summary: Ruby DSL to build Json Schemas
274
+ test_files: []