json_schematize 0.10.0 → 0.11.0

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: f142af20c373eb4cc7c13c09311cb6c5ebfd3bd4d8de2f31bad7e162b1c7f38e
4
- data.tar.gz: 208e364c6afbd566d55b81ebc7f57ea38c96e963afbecd9c2bac79da08710d45
3
+ metadata.gz: 4a137c2d8b5ad986a86ac234f6c03802af2c175b06fc85ff69cf57794a378cca
4
+ data.tar.gz: b449ba15bc26b4f3026d8904bb25a76bcbd26e8ac50ab40deee36f6c1e165544
5
5
  SHA512:
6
- metadata.gz: 8341c39c1d7bfd71e7bf42fc5ad85266084a401c464d3a73362d020e61aaeb2fb46563ecf366397c71e6e23a411763407f8dd45fede1164f0b0cccb0f302afa7
7
- data.tar.gz: 37a700647afa8ad98de45e9563205293ec1a6b7f6c0eaa463fe58cdc98b88d031ed56b7c8f1862f4e3a5ac8bcf9c826803b9c38348bf7b89e30b044f4f031ec7
6
+ metadata.gz: 18739039d4c14428e0ec8ad33ed4b229669cceb8cecb11fa851e13a7ca04eb73a78c2faf0bfd34b49957cea90494d1fecd0d31c94c76ea67afc798007ca08288
7
+ data.tar.gz: 8b51b98b196a7f1a106c409fb957aa259a5751e07d7ae18b823a1439f877987f359a91eb53bd6ff2d1a597fd9e91dafa307f3eb7beef890427eef44267701b5f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schematize (0.10.0)
4
+ json_schematize (0.11.0)
5
5
  class_composer (>= 1.0)
6
6
 
7
7
  GEM
@@ -5,7 +5,7 @@ require 'json_schematize/field_validators'
5
5
 
6
6
  class JsonSchematize::Field
7
7
 
8
- attr_reader :name, :types, :dig, :dig_type, :symbol, :validator, :empty_value
8
+ attr_reader :name, :types, :type, :dig, :dig_type, :symbol, :validator, :empty_value
9
9
  attr_reader :acceptable_types, :required, :converter, :array_of_types
10
10
 
11
11
  EXPECTED_DIG_TYPE = [DIG_SYMBOL = :symbol, DEFAULT_DIG = DIG_NONE =:none, DIG_STRING = :string]
@@ -89,7 +89,11 @@ class JsonSchematize::Field
89
89
  private
90
90
 
91
91
  def validate_acceptable_types(val:)
92
- (all_allowed_types + @acceptable_types).include?(val.class)
92
+ types = (all_allowed_types + @acceptable_types)
93
+ return true if types.include?(val.class)
94
+
95
+ # Allow inheritance here as well -- this only works when a custom converter is defined
96
+ Class === val ? types.any? { _1 > val } : false
93
97
  end
94
98
 
95
99
  def all_allowed_types
@@ -108,8 +112,14 @@ class JsonSchematize::Field
108
112
  raise JsonSchematize::InvalidFieldByArrayOfTypes, ":#{name} expected to be an array based on :array_of_types flag. Given #{value.class}"
109
113
  end
110
114
 
111
- value.map do |val|
112
- raw_converter_call(value: val)
115
+ if value.all? { JsonSchematize::Generator === _1 }
116
+ # We have already done the work to convert it into a Schematizable object
117
+ # Return the array and allow the remaining validations to take place
118
+ value
119
+ else
120
+ value.map do |val|
121
+ raw_converter_call(value: val)
122
+ end
113
123
  end
114
124
  end
115
125
 
@@ -2,13 +2,15 @@
2
2
 
3
3
  require "json_schematize/cache"
4
4
  require "json_schematize/field"
5
- require "json_schematize/introspect"
5
+ require "json_schematize/introspect_instance_methods"
6
+ require "json_schematize/introspect_class_methods"
6
7
 
7
8
  class JsonSchematize::Generator
8
9
  EMPTY_VALIDATOR = ->(_transformed_value, _raw_value) { true }
9
10
  PROTECTED_METHODS = [:assign_values!, :convenience_methods, :validate_required!, :validate_optional!, :validate_value]
10
11
 
11
- include JsonSchematize::Introspect
12
+ include JsonSchematize::Introspect::InstanceMethods
13
+ extend JsonSchematize::Introspect::ClassMethods
12
14
 
13
15
  def self.add_field(name:, type: nil, types: nil, dig_type: nil, dig: nil, validator: nil, required: nil, converter: nil, array_of_types: nil, empty_value: nil, hash_of_types: nil, hash_of_types_key: "name")
14
16
  require "json_schematize/empty_value"
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonSchematize::Introspect
4
+ module ClassMethods
5
+ def introspect(deep: false, prepend_dig: [], introspection: {})
6
+ fields.each do |field|
7
+ naming = (prepend_dig + field.dig).compact
8
+ type_metadata = __field_type__(field)
9
+ introspection[naming.join(".")] = {
10
+ required: field.required,
11
+ allowed: type_metadata[:humanize],
12
+ }
13
+
14
+ if deep && type_metadata[:deep_introspection]
15
+ prepended_naming = if field.array_of_types
16
+ naming.dup.tap { _1[-1] = "#{_1[-1]}[x]"}
17
+ else
18
+ naming.dup
19
+ end
20
+ type_metadata[:types][0].introspect(deep:, prepend_dig: prepended_naming, introspection: introspection)
21
+ end
22
+ end
23
+
24
+ introspection
25
+ end
26
+
27
+ def __field_type__(field)
28
+ types = Array(field.type || field.types)
29
+ deep_introspection = false
30
+ type_string = if types.length == 0
31
+ "Anything"
32
+ elsif types.length == 1
33
+ type = types.first
34
+ if JsonSchematize::Generator > type
35
+ deep_introspection = true
36
+ if field.array_of_types
37
+ "Array of #{type}"
38
+ else
39
+ type
40
+ end
41
+ else
42
+ type
43
+ end
44
+ else
45
+ "One of [#{types}]"
46
+ end
47
+
48
+ {
49
+ humanize: type_string,
50
+ types:,
51
+ deep_introspection:,
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonSchematize::Introspect
4
+ module InstanceMethods
5
+ def to_h
6
+ self.class.fields.map do |field|
7
+ value = method(:"#{field.name}").()
8
+ if field.array_of_types
9
+ [field.name, value.map(&:to_h)]
10
+ elsif value.class == Class
11
+ [field.name, value.to_s]
12
+ elsif JsonSchematize::Generator > value.class
13
+ [field.name, value.to_h]
14
+ else
15
+ [field.name, value]
16
+ end
17
+ end.to_h
18
+ end
19
+ alias :to_hash :to_h
20
+
21
+ def deep_inspect(with_raw_params: false, with_field: false)
22
+ self.class.fields.map do |field|
23
+ value = {
24
+ required: field.required,
25
+ acceptable_types: field.acceptable_types,
26
+ value: instance_variable_get(:"@#{field.name}"),
27
+ }
28
+ value[:field] = field if with_field
29
+ value[:raw_params] = @__raw_params if with_raw_params
30
+ [field.name, value]
31
+ end.to_h
32
+ end
33
+
34
+ def inspect
35
+ stringify = to_h.map { |k, v| "#{k}:#{v}" }.join(", ")
36
+ "#<#{self.class} - required fields: #{self.class.required_fields.map(&:name)}; #{stringify}>"
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonSchematize
4
- VERSION = "0.10.0"
4
+ VERSION = "0.11.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schematize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-27 00:00:00.000000000 Z
11
+ date: 2024-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: class_composer
@@ -90,7 +90,6 @@ files:
90
90
  - ".circleci/config.yml"
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
- - ".ruby-version"
94
93
  - CHANGELOG.md
95
94
  - CODEOWNERS
96
95
  - Dockerfile
@@ -116,7 +115,8 @@ files:
116
115
  - lib/json_schematize/field_transformations.rb
117
116
  - lib/json_schematize/field_validators.rb
118
117
  - lib/json_schematize/generator.rb
119
- - lib/json_schematize/introspect.rb
118
+ - lib/json_schematize/introspect_class_methods.rb
119
+ - lib/json_schematize/introspect_instance_methods.rb
120
120
  - lib/json_schematize/version.rb
121
121
  homepage: https://github.com/matt-taylor/json_schematize
122
122
  licenses:
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.7.4
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JsonSchematize::Introspect
4
- def to_h
5
- self.class.fields.map do |field|
6
- [field.name, instance_variable_get(:"@#{field.name}")]
7
- end.to_h
8
- end
9
- alias :to_hash :to_h
10
-
11
- def deep_inspect(with_raw_params: false, with_field: false)
12
- self.class.fields.map do |field|
13
- value = {
14
- required: field.required,
15
- acceptable_types: field.acceptable_types,
16
- value: instance_variable_get(:"@#{field.name}"),
17
- }
18
- value[:field] = field if with_field
19
- value[:raw_params] = @__raw_params if with_raw_params
20
- [field.name, value]
21
- end.to_h
22
- end
23
-
24
- def inspect
25
- stringify = to_h.map { |k, v| "#{k}:#{v}" }.join(", ")
26
- "#<#{self.class} - required fields: #{self.class.required_fields.map(&:name)}; #{stringify}>"
27
- end
28
- end