json_schematize 0.4.0 → 0.5.2
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/README.md +3 -2
- data/lib/json_schematize/empty_value.rb +9 -0
- data/lib/json_schematize/field.rb +12 -3
- data/lib/json_schematize/field_validators.rb +1 -1
- data/lib/json_schematize/generator.rb +2 -2
- data/lib/json_schematize/introspect.rb +2 -1
- data/lib/json_schematize/version.rb +1 -1
- data/lib/json_schematize.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 848f3c00ddb189bb1e82dc00336d599962a0f3b883318de678a14b88a3b09eb0
|
4
|
+
data.tar.gz: 647b9efcb2b29a9392103397f6902b2d4bd24320392f1fadff4f5856469ddb14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b44776ceb76244451c8121c1c3a306789b4d917f71176e813a0bc477c35424dd0d6414a006ff3d17ca15efb42af412028a336303ee9dc08519c1570447118960
|
7
|
+
data.tar.gz: 82db9b87e549ef31f658dce84e589372811269ab8249abe9e3913a8b6c73834c2c39fe6da1674b397e76225ea9c7283be837d15e75f24aa729ba2e0e9640e79e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -74,7 +74,7 @@ schema.id #=> 999999
|
|
74
74
|
```
|
75
75
|
|
76
76
|
### Field options:
|
77
|
-
```
|
77
|
+
```
|
78
78
|
name -- Name of the field. Field name can be accessed from the instance
|
79
79
|
type -- Class of the expected field type
|
80
80
|
types -- To be used when you want the field to have multiple types. Useful for similar classes like DateTime, Date, Time (converter must be supplied when multiple types are given)
|
@@ -84,6 +84,7 @@ validator -- Proc value to validate the data found in the params. Proc given (tr
|
|
84
84
|
required -- Default is true. When not set, each instance class can optionally decide if they want to raise when an this is set to false.
|
85
85
|
converter -- Proc return is set to the field value. No furter validation is done. Given (value) as a parameter
|
86
86
|
array_of_types -- Detailed example above. Set this value to true when the dig param is to an array and you want all values in array to be parsed the given type
|
87
|
+
empty_value -- When required is false, this value is used to fill the field. By default it is JsonSchematize::EmptyValue, but can be changed to anything
|
87
88
|
```
|
88
89
|
|
89
90
|
### Schema defaults
|
@@ -96,7 +97,7 @@ class SchemaWithDefaults < JsonSchematize::Generator
|
|
96
97
|
|
97
98
|
add_field name: :internals, type: InternalBody, array_of_types: true
|
98
99
|
add_field name: :id, type: Integer
|
99
|
-
add_field name: :status, type: Symbol
|
100
|
+
add_field name: :status, type: Symbol, required: false, empty_value: "empty"
|
100
101
|
end
|
101
102
|
```
|
102
103
|
|
@@ -5,11 +5,12 @@ require 'json_schematize/field_validators'
|
|
5
5
|
|
6
6
|
class JsonSchematize::Field
|
7
7
|
|
8
|
-
attr_reader :name, :types, :dig, :dig_type, :symbol, :validator, :
|
8
|
+
attr_reader :name, :types, :dig, :dig_type, :symbol, :validator, :empty_value
|
9
|
+
attr_reader :acceptable_types, :required, :converter, :array_of_types
|
9
10
|
|
10
11
|
EXPECTED_DIG_TYPE = [DIG_SYMBOL = :symbol, DEFAULT_DIG = DIG_NONE =:none, DIG_STRING = :string]
|
11
12
|
|
12
|
-
def initialize(name:, types:, dig:, dig_type:, validator:, type:, required:, converter:, array_of_types: false)
|
13
|
+
def initialize(name:, types:, dig:, dig_type:, validator:, type:, required:, converter:, empty_value:, array_of_types: false)
|
13
14
|
@name = name
|
14
15
|
@types = types
|
15
16
|
@type = type
|
@@ -19,13 +20,15 @@ class JsonSchematize::Field
|
|
19
20
|
@validator = validator
|
20
21
|
@acceptable_types = []
|
21
22
|
@converter = converter
|
23
|
+
@empty_value = empty_value
|
22
24
|
@array_of_types = array_of_types
|
23
25
|
end
|
24
26
|
|
25
27
|
def setup!
|
26
28
|
# validations must be done before transformations
|
27
|
-
|
29
|
+
validations!
|
28
30
|
transformations!
|
31
|
+
@acceptable_types << ((empty_value.class == Class) ? empty_value : empty_value.class)
|
29
32
|
end
|
30
33
|
|
31
34
|
def value_transform(value:)
|
@@ -102,9 +105,15 @@ class JsonSchematize::Field
|
|
102
105
|
end
|
103
106
|
|
104
107
|
def raw_converter_call(value:)
|
108
|
+
return convert_empty_value if value.nil? && (required == false)
|
109
|
+
|
105
110
|
converter.call(value)
|
106
111
|
end
|
107
112
|
|
113
|
+
def convert_empty_value
|
114
|
+
empty_value.class == Class ? empty_value.new : empty_value
|
115
|
+
end
|
116
|
+
|
108
117
|
include JsonSchematize::FieldTransformations
|
109
118
|
include JsonSchematize::FieldValidators
|
110
119
|
end
|
@@ -9,7 +9,7 @@ class JsonSchematize::Generator
|
|
9
9
|
|
10
10
|
include JsonSchematize::Introspect
|
11
11
|
|
12
|
-
def self.add_field(name:, type: nil, types: nil, dig_type: nil, dig: nil, validator: nil, required: nil, converter: nil, array_of_types: nil)
|
12
|
+
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)
|
13
13
|
field_params = {
|
14
14
|
converter: converter || schema_defaults[:converter],
|
15
15
|
dig: dig || schema_defaults[:dig],
|
@@ -18,6 +18,7 @@ class JsonSchematize::Generator
|
|
18
18
|
required: (required.nil? ? schema_defaults.fetch(:required, true) : required),
|
19
19
|
type: type || schema_defaults[:type],
|
20
20
|
types: types || schema_defaults.fetch(:types, []),
|
21
|
+
empty_value: empty_value || schema_defaults.fetch(:empty_value, JsonSchematize::EmptyValue),
|
21
22
|
validator: validator || schema_defaults.fetch(:validator, EMPTY_VALIDATOR),
|
22
23
|
array_of_types: (array_of_types.nil? ? schema_defaults.fetch(:array_of_types, false) : array_of_types),
|
23
24
|
}
|
@@ -96,7 +97,6 @@ class JsonSchematize::Generator
|
|
96
97
|
end
|
97
98
|
end
|
98
99
|
|
99
|
-
|
100
100
|
private
|
101
101
|
|
102
102
|
def assign_values!
|
@@ -22,6 +22,7 @@ module JsonSchematize::Introspect
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def inspect
|
25
|
-
|
25
|
+
stringify = to_h.map { |k, v| "#{k}:#{v}" }.join(", ")
|
26
|
+
"#<#{self.class} - required fields: #{self.class.required_fields.map(&:name)}; #{stringify}>"
|
26
27
|
end
|
27
28
|
end
|
data/lib/json_schematize.rb
CHANGED
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.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-byebug
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/json_schematize.rb
|
91
91
|
- lib/json_schematize/base.rb
|
92
92
|
- lib/json_schematize/boolean.rb
|
93
|
+
- lib/json_schematize/empty_value.rb
|
93
94
|
- lib/json_schematize/field.rb
|
94
95
|
- lib/json_schematize/field_transformations.rb
|
95
96
|
- lib/json_schematize/field_validators.rb
|