json_schematize 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/json_schematize/field.rb +14 -4
- data/lib/json_schematize/field_transformations.rb +7 -1
- data/lib/json_schematize/generator.rb +4 -2
- data/lib/json_schematize/introspect_class_methods.rb +55 -0
- data/lib/json_schematize/introspect_instance_methods.rb +39 -0
- data/lib/json_schematize/version.rb +1 -1
- metadata +4 -4
- data/.ruby-version +0 -1
- data/lib/json_schematize/introspect.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0109f08060b1b724a2eac694e1cc96c54c3b7bf777b34c247cda4d4e23a4285
|
4
|
+
data.tar.gz: e97e0ac77e38320befb81841f3ef46162d89aba94563cc5b0c51c7cae8f72642
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68d0341cf925a9571bd132e03fe75741dc1bd70de88bc706133c29be46b71b22da623c4963c810c33490dcc553d54c7711ac9390d776a52e4161b7a743e405a9
|
7
|
+
data.tar.gz: 8df6e7e5bd39c186cfa5d41649fda427ac86a470fabe766d886a02f7f9165c3b6bd68ce18cfd54809be3dbabd0805be6e62a2b392c691bbbd6516bc3e27fe42e
|
data/Gemfile.lock
CHANGED
@@ -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)
|
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.
|
112
|
-
|
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
|
|
@@ -20,7 +20,13 @@ module JsonSchematize::FieldTransformations
|
|
20
20
|
|
21
21
|
@converter = DEFAULT_CONVERTERS[@acceptable_types[0]]
|
22
22
|
if @converter.nil?
|
23
|
-
@converter =
|
23
|
+
@converter = Proc.new do |val|
|
24
|
+
if @acceptable_types[0] < JsonSchematize::Generator && @acceptable_types[0] === val
|
25
|
+
val
|
26
|
+
else
|
27
|
+
@acceptable_types[0].new(val)
|
28
|
+
end
|
29
|
+
end
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
@@ -2,13 +2,15 @@
|
|
2
2
|
|
3
3
|
require "json_schematize/cache"
|
4
4
|
require "json_schematize/field"
|
5
|
-
require "json_schematize/
|
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
|
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.
|
4
|
+
version: 0.12.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:
|
11
|
+
date: 2025-02-27 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/
|
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
|