json_schematize 0.3.1 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/json_schematize/empty_value.rb +6 -0
- data/lib/json_schematize/field.rb +13 -4
- data/lib/json_schematize/field_validators.rb +1 -1
- data/lib/json_schematize/generator.rb +40 -19
- data/lib/json_schematize/introspect.rb +28 -0
- data/lib/json_schematize/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7c2935672acaea1a2d0dd6cb77478ba381b3b59af9f6e67006e950a11a31d31
|
4
|
+
data.tar.gz: aa3af45a457f4c0e842c4d39366158f4ec833f123ef85fddb137706918a20359
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a02efdb0960ba3bbfa9369a1e6a4d0b987bd414d676f1b701c5f2890e59153312dd490751f802fb0f27fbd51686c86fe00e6c6a5ab6ed83cfee2477aaf689fb
|
7
|
+
data.tar.gz: 4c0b7cae2ffed160d64c925c60ac00004bc9a688476021615ccd8889ecc45839d225648d581b5a258644869b7f528a215855e7db250de1b3dcc4054ba42e913d
|
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,7 +84,23 @@ 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
|
88
|
+
```
|
89
|
+
|
90
|
+
### Schema defaults
|
91
|
+
|
92
|
+
Defaults can be added for all fields for any of the available options. This can be useful for returned API calls when the body is parsed as a Hash with String keys.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
class SchemaWithDefaults < JsonSchematize::Generator
|
96
|
+
schema_default option: :dig_type, value: :string
|
97
|
+
|
98
|
+
add_field name: :internals, type: InternalBody, array_of_types: true
|
99
|
+
add_field name: :id, type: Integer
|
100
|
+
add_field name: :status, type: Symbol, required: false, empty_value: "empty"
|
101
|
+
end
|
87
102
|
```
|
103
|
+
|
88
104
|
### Custom Classes
|
89
105
|
|
90
106
|
```ruby
|
@@ -5,11 +5,12 @@ require 'json_schematize/field_validators'
|
|
5
5
|
|
6
6
|
class JsonSchematize::Field
|
7
7
|
|
8
|
-
attr_reader :name, :types, :dig, :
|
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
|
-
# validations must be done
|
27
|
-
|
28
|
+
# validations must be done before transformations
|
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
|
@@ -1,29 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "json_schematize/empty_value"
|
3
4
|
require "json_schematize/field"
|
4
|
-
|
5
|
-
# SchemafiedJSON
|
6
|
-
# JSONSchematize
|
5
|
+
require "json_schematize/introspect"
|
7
6
|
|
8
7
|
class JsonSchematize::Generator
|
9
|
-
EMPTY_VALIDATOR = ->(_transformed_value,_raw_value) { true }
|
8
|
+
EMPTY_VALIDATOR = ->(_transformed_value, _raw_value) { true }
|
10
9
|
PROTECTED_METHODS = [:assign_values!, :convenience_methods, :validate_required!, :validate_optional!, :validate_value]
|
11
10
|
|
12
|
-
|
11
|
+
include JsonSchematize::Introspect
|
12
|
+
|
13
|
+
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
14
|
field_params = {
|
14
|
-
converter: converter,
|
15
|
-
dig: dig,
|
16
|
-
dig_type: dig_type,
|
15
|
+
converter: converter || schema_defaults[:converter],
|
16
|
+
dig: dig || schema_defaults[:dig],
|
17
|
+
dig_type: dig_type || schema_defaults[:dig_type],
|
17
18
|
name: name,
|
18
|
-
required: required,
|
19
|
-
type: type,
|
20
|
-
types: types,
|
21
|
-
|
19
|
+
required: (required.nil? ? schema_defaults.fetch(:required, true) : required),
|
20
|
+
type: type || schema_defaults[:type],
|
21
|
+
types: types || schema_defaults.fetch(:types, []),
|
22
|
+
empty_value: empty_value || schema_defaults.fetch(:empty_value, JsonSchematize::EmptyValue),
|
23
|
+
validator: validator || schema_defaults.fetch(:validator, EMPTY_VALIDATOR),
|
24
|
+
array_of_types: (array_of_types.nil? ? schema_defaults.fetch(:array_of_types, false) : array_of_types),
|
22
25
|
}
|
26
|
+
|
23
27
|
field = JsonSchematize::Field.new(**field_params)
|
24
28
|
field.setup!
|
25
29
|
|
26
|
-
if required
|
30
|
+
if field_params[:required] == true
|
27
31
|
required_fields << field
|
28
32
|
else
|
29
33
|
optional_fields << field
|
@@ -31,6 +35,18 @@ class JsonSchematize::Generator
|
|
31
35
|
convenience_methods(field: field)
|
32
36
|
end
|
33
37
|
|
38
|
+
def self.schema_default(option:, value:)
|
39
|
+
if fields.length > 0
|
40
|
+
::Kernel.warn("Default [#{option}] set after fields #{fields.map(&:name)} created. #{option} default will behave inconsistently")
|
41
|
+
end
|
42
|
+
|
43
|
+
schema_defaults[option.to_sym] = value
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.schema_defaults
|
47
|
+
@schema_defaults ||= {}
|
48
|
+
end
|
49
|
+
|
34
50
|
def self.fields
|
35
51
|
required_fields + optional_fields
|
36
52
|
end
|
@@ -65,16 +81,21 @@ class JsonSchematize::Generator
|
|
65
81
|
end
|
66
82
|
end
|
67
83
|
|
68
|
-
attr_reader :__raw_params, :raise_on_error
|
84
|
+
attr_reader :__raw_params, :raise_on_error, :values_assigned
|
69
85
|
|
70
86
|
# stringified_params allows for params with stringed keys
|
71
|
-
def initialize(stringified_params =
|
72
|
-
@
|
87
|
+
def initialize(stringified_params = nil, raise_on_error: true, **params)
|
88
|
+
@values_assigned = false
|
89
|
+
@__params = stringified_params.nil? ? params : stringified_params
|
90
|
+
@__raw_params = @__params
|
73
91
|
@raise_on_error = raise_on_error
|
74
92
|
|
75
|
-
|
76
|
-
|
77
|
-
|
93
|
+
if @__params
|
94
|
+
validate_required!
|
95
|
+
validate_optional!
|
96
|
+
assign_values!
|
97
|
+
@values_assigned = true
|
98
|
+
end
|
78
99
|
end
|
79
100
|
|
80
101
|
private
|
@@ -0,0 +1,28 @@
|
|
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
|
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.1
|
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-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-byebug
|
@@ -90,10 +90,12 @@ 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
|
96
97
|
- lib/json_schematize/generator.rb
|
98
|
+
- lib/json_schematize/introspect.rb
|
97
99
|
- lib/json_schematize/version.rb
|
98
100
|
homepage: https://github.com/matt-taylor/json_schematize
|
99
101
|
licenses:
|