easy_talk 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da6e2b2c29860236280b654d18e4df4af908ab0246f01e1e5e376805f8359d37
4
- data.tar.gz: 1a36d4e08f82069391386c3b2bd03becfeaea198657b8db05ff2ebda793d4ec8
3
+ metadata.gz: cac3597b203d800e1c15c79ea019a6d81080dd5c5ed669dc96500e262a32a505
4
+ data.tar.gz: 626bb67445db8ffa2d41df7a0b82ceba30db827ed0cb610f4d1b382d161a5191
5
5
  SHA512:
6
- metadata.gz: 9026006c5e296adf2f3a7c826757ffe4b17a1387c3621907442ac47293d2d7d89eb4779fe3863ef24f6a8fc34174a7b4e7e22d2aaf0a26a4534ddc65f08ab482
7
- data.tar.gz: d3cd70fb2ac65241cf395606d0eae55e2282006578f4f78096f9e03256ae6fa91bd371b0874d20aa797667b4972be4ff91c5b38b4396ee1be89a3b4d057d4681
6
+ metadata.gz: e58629e7e0b4aff314f04d0a0d14f3129e0af8b54c23c1d061d57464648aa97caaf2db514353fcf34713d1d14113eb13c599ccc28b6106a3c5f06cd4197671af
7
+ data.tar.gz: f3ba1ec6c87ff2028a5bd26f22ebade4446717c8d9e27702c94f5e4080fd951885dd033a0d4f88078d44849cb7b251fc1dbb303f9da84b2a592bf85c1daaf9ef
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## [0.1.9] - 2024-04-29
2
+ - Added the ability to describe an object schema withing the define_schema block. Example:
3
+ ```ruby
4
+ ...
5
+ property :email, :object do
6
+ property :address, :string
7
+ property :verified, :boolean
8
+ end
9
+ ```
10
+
1
11
  ## [0.1.8] - 2024-04-24
2
12
  - mostly refactoring without changes to the public API.
3
13
 
data/README.md CHANGED
@@ -12,7 +12,10 @@ class User
12
12
  title "User"
13
13
  description "A user of the system"
14
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"
15
+ property :email, :object do
16
+ property :address, String, format: "email", description: "The user's email", title: "Email Address"
17
+ property :verified, T::Boolean, description: "Whether the email is verified"
18
+ end
16
19
  property :group, String, enum: [1, 2, 3], default: 1, description: "The user's group"
17
20
  property :age, Integer, minimum: 18, maximum: 100, description: "The user's age"
18
21
  property :tags, T::Array[String], min_items: 1, unique_item: true, description: "The user's tags"
@@ -34,10 +37,23 @@ Calling `User.json_schema` will return the JSON Schema for the User class:
34
37
  "type": "string"
35
38
  },
36
39
  "email": {
37
- "title": "Email Address",
38
- "description": "The user's email",
39
- "type": "string",
40
- "format": "email"
40
+ "type": "object",
41
+ "properties": {
42
+ "address": {
43
+ "title": "Email Address",
44
+ "description": "The user's email",
45
+ "type": "string",
46
+ "format": "email"
47
+ },
48
+ "verified": {
49
+ "type": "boolean",
50
+ "description": "Whether the email is verified"
51
+ }
52
+ },
53
+ "required": [
54
+ "address",
55
+ "verified"
56
+ ]
41
57
  },
42
58
  "group": {
43
59
  "type": "number",
data/easy_talk.gemspec ADDED
@@ -0,0 +1,45 @@
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', '~> 7.0'
35
+ spec.add_dependency 'json-schema', '~> 4'
36
+ spec.add_dependency 'sorbet-runtime', '~> 0.5'
37
+ spec.add_development_dependency 'pry-byebug', '>= 3.10'
38
+ spec.add_development_dependency 'rake', '~> 13.1'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'rspec-json_expectations', '~> 2.0'
41
+ spec.add_development_dependency 'rspec-mocks', '~> 3.13'
42
+ spec.add_development_dependency 'rubocop', '~> 1.21'
43
+ spec.add_development_dependency 'rubocop-rake', '~> 0.6'
44
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.29'
45
+ end
@@ -11,7 +11,7 @@ module EasyTalk
11
11
  attr_reader :schema
12
12
 
13
13
  VALID_OPTIONS = {
14
- properties: { type: T::Hash[Symbol, T.untyped], key: :properties },
14
+ properties: { type: T::Hash[T.any(Symbol, String), T.untyped], key: :properties },
15
15
  additional_properties: { type: T::Boolean, key: :additionalProperties },
16
16
  subschemas: { type: T::Array[T.untyped], key: :subschemas },
17
17
  required: { type: T::Array[Symbol], key: :required },
@@ -33,42 +33,58 @@ module EasyTalk
33
33
 
34
34
  private
35
35
 
36
- def properties_from_schema_definition(properties)
36
+ def properties_from_schema_definition
37
+ properties = schema.delete(:properties) || {}
37
38
  properties.each_with_object({}) do |(property_name, options), context|
38
- @required_properties << property_name unless options[:type].respond_to?(:nilable?) && options[:type].nilable?
39
- context[property_name] = Property.new(property_name, options[:type], options[:constraints])
39
+ add_required_property(property_name, options)
40
+ context[property_name] = build_property(property_name, options)
40
41
  end
41
42
  end
42
43
 
43
- def subschemas_from_schema_definition(subschemas)
44
+ def add_required_property(property_name, options)
45
+ return unless options.is_a?(Hash) && !(options[:type].respond_to?(:nilable?) && options[:type].nilable?)
46
+
47
+ @required_properties << property_name
48
+ end
49
+
50
+ def build_property(property_name, options)
51
+ if options.is_a?(EasyTalk::SchemaDefinition)
52
+ ObjectBuilder.new(options).build
53
+ else
54
+ Property.new(property_name, options[:type], options[:constraints])
55
+ end
56
+ end
57
+
58
+ def subschemas_from_schema_definition
59
+ subschemas = schema.delete(:subschemas) || []
44
60
  subschemas.each do |subschema|
45
- definitions = subschema.items.each_with_object({}) do |item, hash|
46
- hash[item.name] = item.schema
47
- end
48
- schema[:defs] = definitions
49
- references = subschema.items.map do |item|
50
- { '$ref': item.ref_template }
51
- end
52
- schema[subschema.name] = references
61
+ add_definitions(subschema)
62
+ add_references(subschema)
63
+ end
64
+ end
65
+
66
+ def add_definitions(subschema)
67
+ definitions = subschema.items.each_with_object({}) do |item, hash|
68
+ hash[item.name] = item.schema
69
+ end
70
+ schema[:defs] = definitions
71
+ end
72
+
73
+ def add_references(subschema)
74
+ references = subschema.items.map do |item|
75
+ { '$ref': item.ref_template }
53
76
  end
77
+ schema[subschema.name] = references
54
78
  end
55
79
 
56
80
  def options
57
- subschemas_from_schema_definition(subschemas)
58
81
  @options = schema
59
- @options[:properties] = properties_from_schema_definition(properties)
82
+ subschemas_from_schema_definition
83
+ @options[:properties] = properties_from_schema_definition
60
84
  @options[:required] = @required_properties
61
85
  @options.reject! { |_key, value| [nil, [], {}].include?(value) }
62
86
  @options
63
87
  end
64
-
65
- def properties
66
- schema.delete(:properties) || {}
67
- end
68
-
69
- def subschemas
70
- schema.delete(:subschemas) || []
71
- end
72
88
  end
73
89
  end
74
90
  end
@@ -32,10 +32,19 @@ module EasyTalk
32
32
  @schema[:subschemas] += subschemas
33
33
  end
34
34
 
35
- sig { params(name: Symbol, type: T.untyped, constraints: T.untyped).void }
36
- def property(name, type, **constraints)
35
+ sig do
36
+ params(name: T.any(Symbol, String), type: T.untyped, constraints: T.untyped, blk: T.nilable(T.proc.void)).void
37
+ end
38
+ def property(name, type, **constraints, &blk)
37
39
  @schema[:properties] ||= {}
38
- @schema[:properties][name] = { type:, constraints: }
40
+
41
+ if block_given?
42
+ property_schema = SchemaDefinition.new(name)
43
+ property_schema.instance_eval(&blk)
44
+ @schema[:properties][name] = property_schema
45
+ else
46
+ @schema[:properties][name] = { type:, constraints: }
47
+ end
39
48
  end
40
49
  end
41
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyTalk
4
- VERSION = '0.1.8'
4
+ VERSION = '0.1.9'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_talk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Bayona
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-24 00:00:00.000000000 Z
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -177,6 +177,7 @@ files:
177
177
  - LICENSE.txt
178
178
  - README.md
179
179
  - Rakefile
180
+ - easy_talk.gemspec
180
181
  - lib/easy_talk.rb
181
182
  - lib/easy_talk/builders/all_of_builder.rb
182
183
  - lib/easy_talk/builders/any_of_builder.rb