easy_talk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +14 -0
  3. data/CHANGELOG.md +0 -0
  4. data/CONSTRAINTS.md +70 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +186 -0
  7. data/Rakefile +12 -0
  8. data/easy_talk.gemspec +38 -0
  9. data/lib/easy_talk/builder.rb +51 -0
  10. data/lib/easy_talk/builders/all_of_builder.rb +11 -0
  11. data/lib/easy_talk/builders/any_of_builder.rb +11 -0
  12. data/lib/easy_talk/builders/base_builder.rb +58 -0
  13. data/lib/easy_talk/builders/boolean_builder.rb +24 -0
  14. data/lib/easy_talk/builders/composition_builder.rb +64 -0
  15. data/lib/easy_talk/builders/date_builder.rb +18 -0
  16. data/lib/easy_talk/builders/datetime_builder.rb +18 -0
  17. data/lib/easy_talk/builders/integer_builder.rb +28 -0
  18. data/lib/easy_talk/builders/null_builder.rb +16 -0
  19. data/lib/easy_talk/builders/number_builder.rb +27 -0
  20. data/lib/easy_talk/builders/object_builder.rb +72 -0
  21. data/lib/easy_talk/builders/one_of_builder.rb +11 -0
  22. data/lib/easy_talk/builders/ref_array_builder.rb +25 -0
  23. data/lib/easy_talk/builders/string_builder.rb +27 -0
  24. data/lib/easy_talk/builders/time_builder.rb +16 -0
  25. data/lib/easy_talk/builders/typed_array_builder.rb +51 -0
  26. data/lib/easy_talk/builders/union_builder.rb +35 -0
  27. data/lib/easy_talk/keywords.rb +37 -0
  28. data/lib/easy_talk/model.rb +105 -0
  29. data/lib/easy_talk/property.rb +80 -0
  30. data/lib/easy_talk/schema_definition.rb +42 -0
  31. data/lib/easy_talk/sorbet_extension.rb +15 -0
  32. data/lib/easy_talk/types/all_of.rb +32 -0
  33. data/lib/easy_talk/types/any_of.rb +39 -0
  34. data/lib/easy_talk/types/base_composer.rb +23 -0
  35. data/lib/easy_talk/types/one_of.rb +31 -0
  36. data/lib/easy_talk/version.rb +5 -0
  37. data/lib/easy_talk.rb +28 -0
  38. metadata +139 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6fcc0c59463e51453f3a5242447abf42b06ea093e6573b3c887136e3036ad963
4
+ data.tar.gz: 62aea206f5db132804b9ceae927f3de2f2eefc429e137ff121b325b689eb458f
5
+ SHA512:
6
+ metadata.gz: 8996f94f698b255160f318add11bab15b4b182a1127919885196d5ace961ca41f092a4d1f28eed553ed6848b36b3ae75ae49bbe82b5dbb60c32c89b75263b1c7
7
+ data.tar.gz: 4b81dc4128e4f9455d7bf7c814957e5ed38989ce5cd3de63ed5d2deb789706a360af1df16ded68f6ae0cbf1ec1ab728cac9216f71e3e9cda6250953620b5c235
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.2
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - 'spec/**/*'
7
+
8
+ Lint/ConstantDefinitionInBlock:
9
+ Exclude:
10
+ - 'spec/**/*'
11
+
12
+ Layout/LineLength:
13
+ Exclude:
14
+ - 'spec/**/*'
data/CHANGELOG.md ADDED
File without changes
data/CONSTRAINTS.md ADDED
@@ -0,0 +1,70 @@
1
+ The following constraints are supported by the JSON Schema generator:
2
+
3
+ ## String Properties
4
+
5
+ | Constraint | Description |
6
+ |---------------|---------------|
7
+ | format | Specifies the format that the string should match (e.g., email, uuid). |
8
+ | pattern | A regular expression pattern that the string must match. |
9
+ | min_length | The minimum number of characters for the string. |
10
+ | max_length | The maximum number of characters for the string. |
11
+ | enum | An array that specifies the enumerated values the string can take. |
12
+ | const | Specifies a single constant value the string must be equal to. |
13
+ | default | The default value for the string. |
14
+
15
+ ## Integer and Number Properties
16
+ | Constraint | Description |
17
+ |-----------------------|---------------|
18
+ | minimum | The minimum value the integer can be. |
19
+ | maximum | The maximum value the integer can be. |
20
+ | exclusive_minimum | If true, the value must be strictly greater than the minimum value. |
21
+ | exclusive_maximum | If true, the value must be strictly less than the maximum value. |
22
+ | multiple_of | A number that the integer must be a multiple of. |
23
+ | enum | An array that specifies the enumerated values the integer can take. |
24
+ | const | Specifies a single constant value the integer must be equal to. |
25
+ | default | The default value for the integer. |
26
+
27
+
28
+ ## Array Properties
29
+ | Constraint | Description |
30
+ |-----------------------|---------------|
31
+ | min_items | The minimum number of items in the array. |
32
+ | max_items | The maximum number of items in the array. |
33
+ | unique_items | If true, all items in the array must be unique. |
34
+ | items | An object that specifies the schema for each item in the array. |
35
+ | enum | An array that specifies the enumerated values the array can take. |
36
+ | const | Specifies a single constant value the array must be equal to. |
37
+ | default | The default value for the array. |
38
+
39
+
40
+ ## Boolean Properties
41
+ | Constraint | Description |
42
+ |-----------------------|---------------|
43
+ | enum | An array that specifies the enumerated values the boolean can take. |
44
+ | const | Specifies a single constant value the boolean must be equal to. |
45
+ | default | The default value for the boolean. |
46
+
47
+ ## Object Properties
48
+
49
+ | Constraint | Description |
50
+ |-----------------------|---------------|
51
+ | properties | An object that specifies the schema for each property in the object. |
52
+ | required | An array that specifies the required properties in the object. |
53
+ | min_properties | The minimum number of properties in the object. |
54
+ | max_properties | The maximum number of properties in the object. |
55
+ | additional_properties | An object that specifies the schema for additional properties in the object. |
56
+ | pattern_properties | An object that specifies the schema for properties that match a regular expression pattern. |
57
+
58
+ ## Null Properties
59
+ | Constraint | Description |
60
+ |-----------------------|---------------|
61
+ | enum | An array that specifies the enumerated values the null can take. |
62
+ | const | Specifies a single constant value the null must be equal to. |
63
+ | default | The default value for the null. |
64
+
65
+
66
+ ## All Properties
67
+ | Constraint | Description |
68
+ |---------------|---------------|
69
+ | title | A short summary of what the property represents. |
70
+ | description | A detailed description of what the property represents. |
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Sergio Bayona
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # EasyTalk
2
+
3
+ EasyTalk is a Ruby library for defining and generating JSON Schema.
4
+
5
+ Example Use:
6
+
7
+ ```ruby
8
+ class User
9
+ include EasyTalk::Model
10
+
11
+ define_schema do
12
+ title "User"
13
+ description "A user of the system"
14
+ property :name, String, description: "The user's name", title: "Full Name"
15
+ property :email, String, description: "The user's email", format: "email", title: "Email Address"
16
+ property :group, String, enum: [1, 2, 3], default: 1, description: "The user's group"
17
+ property :age, Integer, minimum: 18, maximum: 100, description: "The user's age"
18
+ property :tags, T::Array[String], min_items: 1, unique_item: true, description: "The user's tags"
19
+ end
20
+ end
21
+ ```
22
+
23
+ Calling `User.json_schema` will return the JSON Schema for the User class:
24
+
25
+ ```json
26
+ {
27
+ "title": "User",
28
+ "description": "A user of the system",
29
+ "type": "object",
30
+ "properties": {
31
+ "name": {
32
+ "title": "Full Name",
33
+ "description": "The user's name",
34
+ "type": "string"
35
+ },
36
+ "email": {
37
+ "title": "Email Address",
38
+ "description": "The user's email",
39
+ "type": "string",
40
+ "format": "email"
41
+ },
42
+ "group": {
43
+ "type": "number",
44
+ "enum": [1, 2, 3],
45
+ "default": 1,
46
+ "description": "The user's group"
47
+ },
48
+ "age": {
49
+ "type": "integer",
50
+ "minimum": 18,
51
+ "maximum": 100,
52
+ "description": "The user's age"
53
+ },
54
+ "tags": {
55
+ "type": "array",
56
+ "items": {
57
+ "type": "string"
58
+ },
59
+ "minItems": 1,
60
+ "uniqueItems": true,
61
+ "description": "The user's tags"
62
+ }
63
+ },
64
+ "required:": [
65
+ "name",
66
+ "email",
67
+ "group",
68
+ "age",
69
+ "tags"
70
+ ]
71
+ }
72
+ ```
73
+
74
+ ## Installation
75
+
76
+ install the gem by running the following command in your terminal:
77
+
78
+ $ gem install easy_talk
79
+
80
+ ## Usage
81
+
82
+ Simply include the `EasyTalk::Model` module in your Ruby class, define the schema using the `define_schema` block and call the `json_schema` class method to generate the JSON Schema document.
83
+
84
+
85
+ ## Schema Definition
86
+
87
+ In the example above, the `define_schema` method is used to add a description and a title to the schema document. The `property` method is used to define the properties of the schema document. The `property` method accepts the name of the property as a symbol, the type, which can be a generic Ruby type or a [Sorbet type](https://sorbet.org/docs/stdlib-generics), and a hash of constraints as options.
88
+
89
+ ## Property Constraints
90
+
91
+ Property constraints are type-dependent. Refer to the [CONSTRAINTS.md](CONSTRAINTS.md) file for a list of constraints supported by the JSON Schema generator.
92
+
93
+
94
+ ## Schema Composition
95
+
96
+ EasyTalk supports schema composition. You can define a schema for a nested object by defining a new class and including the `EasyTalk::Model` module. You can then reference the nested schema in the parent schema using the following special types:
97
+
98
+ - T::OneOf[Model1, Model2, ...] - The property must match at least one of the specified schemas.
99
+ - T::AnyOf[Model1, Model2, ...] - The property can match any of the specified schemas.
100
+ - T::AllOf[Model1, Model2, ...] - The property must match all of the specified schemas.
101
+
102
+ Here is an example where we define a schema for a payment object that can be a credit card, a PayPal account, or a bank transfer. The first three classes represent the schemas for the different payment methods. The `Payment` class represents the schema for the payment object where the `Details` property can be any of the payment method schemas.
103
+
104
+ ```ruby
105
+ class CreditCard
106
+ include EasyTalk::Model
107
+
108
+ define_schema do
109
+ property :CardNumber, String
110
+ property :CardType, String, enum: %w[Visa MasterCard AmericanExpress]
111
+ property :CardExpMonth, Integer, minimum: 1, maximum: 12
112
+ property :CardExpYear, Integer, minimum: Date.today.year, maximum: Date.today.year + 10
113
+ property :CardCVV, String, pattern: '^[0-9]{3,4}$'
114
+ additional_properties false
115
+ end
116
+ end
117
+
118
+ class Paypal
119
+ include EasyTalk::Model
120
+
121
+ define_schema do
122
+ property :PaypalEmail, String, format: 'email'
123
+ property :PaypalPasswordEncrypted, String
124
+ additional_properties false
125
+ end
126
+ end
127
+
128
+ class BankTransfer
129
+ include EasyTalk::Model
130
+
131
+ define_schema do
132
+ property :BankName, String
133
+ property :AccountNumber, String
134
+ property :RoutingNumber, String
135
+ property :AccountType, String, enum: %w[Checking Savings]
136
+ additional_properties false
137
+ end
138
+ end
139
+
140
+ class Payment
141
+ include EasyTalk::Model
142
+
143
+ define_schema do
144
+ title 'Payment'
145
+ description 'Payment info'
146
+ property :PaymentMethod, String, enum: %w[CreditCard Paypal BankTransfer]
147
+ property :Details, T::AnyOf[CreditCard, Paypal, BankTransfer]
148
+ end
149
+ end
150
+
151
+ ```
152
+
153
+ ## Type Checking and Schema Constraints
154
+
155
+ EasyTalk uses [Sorbet](https://sorbet.org/) to perform type checking on the property constraint values. The `property` method accepts a type as the second argument. The type can be a Ruby class or a Sorbet type. For example, `String`, `Integer`, `T::Array[String]`, etc.
156
+
157
+ EasyTalk raises an error if the constraint values do not match the property type. For example, if you specify the `enum` constraint with the values [1,2,3], but the property type is `String`, EasyTalk will raise a type error.
158
+
159
+ EasyTalk also raises an error if the constraints are not valid for the property type. For example, if you define a property with a `minimum` or a `maximum` constraint, but the type is `String`, EasyTalk will raise an error.
160
+
161
+ ## Schema Validation
162
+
163
+ EasyTalk does not yet perform JSON validation. So far, it only aims to generate a valid JSON Schema document. You can use the `json_schema` method to generate the JSON Schema and use a JSON Schema validator library like [JSONSchemer](https://github.com/davishmcclurg/json_schemer) to validate JSON against. See https://json-schema.org/implementations#validators-ruby for a list of JSON Schema validator libraries for Ruby.
164
+
165
+ The goal is to introduce JSON validation in the near future.
166
+
167
+ ## JSON Schema Specifications
168
+
169
+ EasyTalk is currently very loose about JSON Schema specifications. It does not enforce the use of the latest JSON Schema specifications. Support for the dictionary of JSON Schema keywords varies depending on the keyword. The goal is to have robust support for the latest JSON Schema specifications in the near future.
170
+
171
+ To learn about the current EasyTalk capabilities, take a look at the [spec/easy_talk/examples](https://github.com/sergiobayona/easy_talk/tree/main/spec/easy_talk/examples) folder. The examples are used to test the JSON Schema generation.
172
+
173
+ ## Development
174
+
175
+ 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.
176
+
177
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
178
+
179
+ ## Contributing
180
+
181
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sergiobayona/easy_talk.
182
+
183
+ ## License
184
+
185
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
186
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/easy_talk.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/easy_talk/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'easy_talk'
7
+ spec.version = EasyTalk::VERSION
8
+ spec.authors = ['Sergio Bayona']
9
+ spec.email = ['bayona.sergio@gmail.com']
10
+
11
+ spec.summary = 'Generate json-schema from Ruby classes.'
12
+ spec.description = 'Generate json-schema from plain Ruby classes.'
13
+ spec.homepage = 'https://github.com/sergiobayona/easy_talk'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.2'
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/sergiobayona/easy_talk'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/sergiobayona/easy_talk/blob/main/CHANGELOG.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ spec/ .git .github Gemfile])
29
+ end
30
+ end
31
+
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_dependency 'activesupport', '~> 6.0'
35
+ spec.add_dependency 'sorbet-runtime'
36
+ spec.add_development_dependency 'pry-byebug', '>= 3.10.1'
37
+ spec.add_development_dependency 'rake', '~> 13.1'
38
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'property'
4
+ require_relative 'builders/object_builder'
5
+
6
+ module EasyTalk
7
+ # The Builder class is responsible for building a schema for a class.
8
+ class Builder
9
+ extend T::Sig
10
+
11
+ sig { params(schema_definition: SchemaDefinition).void }
12
+ # Initializes a new instance of the Builder class.
13
+ #
14
+ # @param schema_definition [SchemaDefinition] The schema definition.
15
+ def initialize(schema_definition)
16
+ @schema_definition = schema_definition
17
+ end
18
+
19
+ sig { returns(Hash) }
20
+ # Retrieves the schema document.
21
+ #
22
+ # @return [Hash] The schema document.
23
+ def schema
24
+ @schema = schema_document
25
+ end
26
+
27
+ sig { returns(String) }
28
+ # Returns the JSON representation of the schema document.
29
+ #
30
+ # @return [String] The JSON schema.
31
+ def json_schema
32
+ @json_schema ||= schema_document.to_json
33
+ end
34
+
35
+ sig { returns(Hash) }
36
+ # Returns the schema document, building it if necessary.
37
+ #
38
+ # @return [Hash] The schema document.
39
+ def schema_document
40
+ @schema_document ||= build_schema
41
+ end
42
+
43
+ sig { returns(Hash) }
44
+ # Builds the schema using the schema definition.
45
+ #
46
+ # Returns the built schema.
47
+ def build_schema
48
+ Builders::ObjectBuilder.new(@schema_definition).build
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'composition_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # Builder class for integer properties.
8
+ class AllOfBuilder < CompositionBuilder
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'composition_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # Builder class for integer properties.
8
+ class AnyOfBuilder < CompositionBuilder
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module EasyTalk
5
+ module Builders
6
+ # BaseBuilder is a class that provides a common structure for building schema properties
7
+ class BaseBuilder
8
+ extend T::Sig
9
+
10
+ # BaseBuilder is a class that provides a common structure for building objects
11
+ # representing schema properties.
12
+ COMMON_OPTIONS = {
13
+ title: { type: T.nilable(String), key: :title },
14
+ description: { type: T.nilable(String), key: :description }
15
+ }.freeze
16
+
17
+ attr_reader :name, :schema, :options
18
+
19
+ sig do
20
+ params(
21
+ name: Symbol,
22
+ schema: T::Hash[Symbol, T.untyped],
23
+ options: T::Hash[Symbol, String],
24
+ valid_options: T::Hash[Symbol, T.untyped]
25
+ ).void
26
+ end
27
+ # Initializes a new instance of the BaseBuilder class.
28
+ #
29
+ # @param name [Symbol] The name of the property.
30
+ # @param schema [Hash] A hash representing a json schema object.
31
+ # @param options [Hash] The options for the builder (default: {}).
32
+ # @param valid_options [Hash] The acceptable options for the given property type (default: {}).
33
+ def initialize(name, schema, options = {}, valid_options = {})
34
+ @valid_options = COMMON_OPTIONS.merge(valid_options)
35
+ options.assert_valid_keys(@valid_options.keys)
36
+ @name = name
37
+ @schema = schema
38
+ @options = options
39
+ end
40
+
41
+ # Builds the schema object based on the provided options.
42
+ sig { returns(T::Hash[Symbol, T.untyped]) }
43
+ def build
44
+ @valid_options.each_with_object(schema) do |(key, value), obj|
45
+ next if @options[key].nil?
46
+
47
+ # Work around for Sorbet's default inability to type check the items inside an array
48
+
49
+ if value[:type].respond_to?(:recursively_valid?) && !value[:type].recursively_valid?(@options[key])
50
+ raise TypeError, "Invalid type for #{key}"
51
+ end
52
+
53
+ obj[value[:key]] = T.let(@options[key], value[:type])
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # Builder class for boolean properties.
8
+ class BooleanBuilder < BaseBuilder
9
+ extend T::Sig
10
+
11
+ # VALID_OPTIONS defines the valid options for a boolean property.
12
+ VALID_OPTIONS = {
13
+ enum: { type: T::Array[T::Boolean], key: :enum },
14
+ const: { type: T::Boolean, key: :const },
15
+ default: { type: T::Boolean, key: :default }
16
+ }.freeze
17
+
18
+ sig { params(name: Symbol, _type: T.untyped, constraints: Hash).void }
19
+ def initialize(name, _type = nil, constraints = {})
20
+ super(name, { type: 'boolean' }, constraints, VALID_OPTIONS)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # This class represents a builder for composing JSON schemas using the "allOf", "anyOf", or "oneOf" keywords.
8
+ class CompositionBuilder
9
+ extend T::Sig
10
+
11
+ COMPOSER_TO_KEYWORD = {
12
+ 'AllOfBuilder' => 'allOf',
13
+ 'AnyOfBuilder' => 'anyOf',
14
+ 'OneOfBuilder' => 'oneOf'
15
+ }.freeze
16
+
17
+ sig { params(name: Symbol, type: T.untyped, _constraints: Hash).void }
18
+ # Initializes a new instance of the CompositionBuilder class.
19
+ #
20
+ # @param name [Symbol] The name of the composition.
21
+ # @param type [Class] The type of the composition.
22
+ # @param _constraints [Hash] The constraints for the composition (not used in this method).
23
+ def initialize(name, type, _constraints)
24
+ @composer_type = self.class.name.split('::').last
25
+ @name = name
26
+ @type = type
27
+ @context = {}
28
+ end
29
+
30
+ # Builds the composed JSON schema.
31
+ #
32
+ # @return [void]
33
+ def build
34
+ @context[@name.to_sym] = {
35
+ type: 'object',
36
+ composer_keyword => schemas
37
+ }
38
+ end
39
+
40
+ # Returns the composer keyword based on the composer type.
41
+ #
42
+ # @return [String] The composer keyword.
43
+ def composer_keyword
44
+ COMPOSER_TO_KEYWORD[@composer_type]
45
+ end
46
+
47
+ # Returns an array of schemas for the composed JSON schema.
48
+ #
49
+ # @return [Array<Hash>] The array of schemas.
50
+ def schemas
51
+ items.map do |type|
52
+ type.respond_to?(:schema) ? type.schema : { type: type.to_s.downcase }
53
+ end
54
+ end
55
+
56
+ # Returns the items of the type.
57
+ #
58
+ # @return [T.untyped] The items of the type.
59
+ def items
60
+ @type.items
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # Builder class for date properties.
8
+ class DateBuilder < StringBuilder
9
+ # Modifies the schema to include the format constraint for a date property.
10
+ sig { returns(T::Hash[Symbol, T.untyped]) }
11
+ def schema
12
+ super.tap do |schema|
13
+ schema[:format] = 'date'
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # Builder class for datetime properties.
8
+ class DatetimeBuilder < StringBuilder
9
+ # Modifies the schema to include the format constraint for a datetime property.
10
+ sig { returns(T::Hash[Symbol, T.untyped]) }
11
+ def schema
12
+ super.tap do |schema|
13
+ schema[:format] = 'date-time'
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # Builder class for integer properties.
8
+ class IntegerBuilder < BaseBuilder
9
+ extend T::Sig
10
+ VALID_OPTIONS = {
11
+ minimum: { type: Integer, key: :minimum },
12
+ maximum: { type: Integer, key: :maximum },
13
+ exclusive_minimum: { type: Integer, key: :exclusiveMinimum },
14
+ exclusive_maximum: { type: Integer, key: :exclusiveMaximum },
15
+ multiple_of: { type: Integer, key: :multipleOf },
16
+ enum: { type: T::Array[Integer], key: :enum },
17
+ const: { type: Integer, key: :const },
18
+ default: { type: Integer, key: :default }
19
+ }.freeze
20
+
21
+ # Initializes a new instance of the IntegerBuilder class.
22
+ sig { params(name: Symbol, _type: T.untyped, constraints: Hash).void }
23
+ def initialize(name, _type = nil, constraints = {})
24
+ super(name, { type: 'integer' }, constraints, VALID_OPTIONS)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base_builder'
4
+
5
+ module EasyTalk
6
+ module Builders
7
+ # builder class for Null properties.
8
+ class NullBuilder < BaseBuilder
9
+ # Initializes a new instance of the NullBuilder class.
10
+ sig { params(name: Symbol, _type: T.untyped, _constraints: Hash).void }
11
+ def initialize(name, _type = nil, _constraints = {})
12
+ super(name, { type: 'null' }, {}, {})
13
+ end
14
+ end
15
+ end
16
+ end