json_schematize 0.3.1 → 0.4.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/README.md +15 -0
- data/lib/json_schematize/field.rb +2 -2
- data/lib/json_schematize/generator.rb +39 -19
- data/lib/json_schematize/introspect.rb +27 -0
- data/lib/json_schematize/version.rb +1 -1
- 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: 92b634c916946aab36db45a5c037f31fc03452d77c3aca4a1f9ca14109cc74aa
|
|
4
|
+
data.tar.gz: d3a3b4d1085ce8a415bc03e871ada8e1c6e250df096b566fd9dc1a9aac902204
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e0d9a3d86984c6ce6339546b12461cea1374f507d1356f2cc2ebf675f90bb954bad2559a43f9e6a014219120eaf20537bf187dfeb486ba449036de030e5a02b0
|
|
7
|
+
data.tar.gz: c6c8546e21abccee5ec36746aa2e12dd5d7cd1dde148a5d1c82ac72abd1252b03dfb815c947b04e0914e9639014a063533d9b736dbde475a32a97b01a02f8ed8
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -85,6 +85,21 @@ required -- Default is true. When not set, each instance class can optionally de
|
|
|
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
87
|
```
|
|
88
|
+
|
|
89
|
+
### Schema defaults
|
|
90
|
+
|
|
91
|
+
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.
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
class SchemaWithDefaults < JsonSchematize::Generator
|
|
95
|
+
schema_default option: :dig_type, value: :string
|
|
96
|
+
|
|
97
|
+
add_field name: :internals, type: InternalBody, array_of_types: true
|
|
98
|
+
add_field name: :id, type: Integer
|
|
99
|
+
add_field name: :status, type: Symbol
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
88
103
|
### Custom Classes
|
|
89
104
|
|
|
90
105
|
```ruby
|
|
@@ -5,7 +5,7 @@ require 'json_schematize/field_validators'
|
|
|
5
5
|
|
|
6
6
|
class JsonSchematize::Field
|
|
7
7
|
|
|
8
|
-
attr_reader :name, :types, :dig, :symbol, :validator, :acceptable_types, :required, :converter, :array_of_types
|
|
8
|
+
attr_reader :name, :types, :dig, :dig_type, :symbol, :validator, :acceptable_types, :required, :converter, :array_of_types
|
|
9
9
|
|
|
10
10
|
EXPECTED_DIG_TYPE = [DIG_SYMBOL = :symbol, DEFAULT_DIG = DIG_NONE =:none, DIG_STRING = :string]
|
|
11
11
|
|
|
@@ -23,7 +23,7 @@ class JsonSchematize::Field
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def setup!
|
|
26
|
-
# validations must be done
|
|
26
|
+
# validations must be done before transformations
|
|
27
27
|
valiadtions!
|
|
28
28
|
transformations!
|
|
29
29
|
end
|
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "json_schematize/field"
|
|
4
|
-
|
|
5
|
-
# SchemafiedJSON
|
|
6
|
-
# JSONSchematize
|
|
4
|
+
require "json_schematize/introspect"
|
|
7
5
|
|
|
8
6
|
class JsonSchematize::Generator
|
|
9
|
-
EMPTY_VALIDATOR = ->(_transformed_value,_raw_value) { true }
|
|
7
|
+
EMPTY_VALIDATOR = ->(_transformed_value, _raw_value) { true }
|
|
10
8
|
PROTECTED_METHODS = [:assign_values!, :convenience_methods, :validate_required!, :validate_optional!, :validate_value]
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
include JsonSchematize::Introspect
|
|
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)
|
|
13
13
|
field_params = {
|
|
14
|
-
converter: converter,
|
|
15
|
-
dig: dig,
|
|
16
|
-
dig_type: dig_type,
|
|
14
|
+
converter: converter || schema_defaults[:converter],
|
|
15
|
+
dig: dig || schema_defaults[:dig],
|
|
16
|
+
dig_type: dig_type || schema_defaults[:dig_type],
|
|
17
17
|
name: name,
|
|
18
|
-
required: required,
|
|
19
|
-
type: type,
|
|
20
|
-
types: types,
|
|
21
|
-
validator: validator,
|
|
18
|
+
required: (required.nil? ? schema_defaults.fetch(:required, true) : required),
|
|
19
|
+
type: type || schema_defaults[:type],
|
|
20
|
+
types: types || schema_defaults.fetch(:types, []),
|
|
21
|
+
validator: validator || schema_defaults.fetch(:validator, EMPTY_VALIDATOR),
|
|
22
|
+
array_of_types: (array_of_types.nil? ? schema_defaults.fetch(:array_of_types, false) : array_of_types),
|
|
22
23
|
}
|
|
24
|
+
|
|
23
25
|
field = JsonSchematize::Field.new(**field_params)
|
|
24
26
|
field.setup!
|
|
25
27
|
|
|
26
|
-
if required
|
|
28
|
+
if field_params[:required] == true
|
|
27
29
|
required_fields << field
|
|
28
30
|
else
|
|
29
31
|
optional_fields << field
|
|
@@ -31,6 +33,18 @@ class JsonSchematize::Generator
|
|
|
31
33
|
convenience_methods(field: field)
|
|
32
34
|
end
|
|
33
35
|
|
|
36
|
+
def self.schema_default(option:, value:)
|
|
37
|
+
if fields.length > 0
|
|
38
|
+
::Kernel.warn("Default [#{option}] set after fields #{fields.map(&:name)} created. #{option} default will behave inconsistently")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
schema_defaults[option.to_sym] = value
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.schema_defaults
|
|
45
|
+
@schema_defaults ||= {}
|
|
46
|
+
end
|
|
47
|
+
|
|
34
48
|
def self.fields
|
|
35
49
|
required_fields + optional_fields
|
|
36
50
|
end
|
|
@@ -65,18 +79,24 @@ class JsonSchematize::Generator
|
|
|
65
79
|
end
|
|
66
80
|
end
|
|
67
81
|
|
|
68
|
-
attr_reader :__raw_params, :raise_on_error
|
|
82
|
+
attr_reader :__raw_params, :raise_on_error, :values_assigned
|
|
69
83
|
|
|
70
84
|
# stringified_params allows for params with stringed keys
|
|
71
|
-
def initialize(stringified_params =
|
|
72
|
-
@
|
|
85
|
+
def initialize(stringified_params = nil, raise_on_error: true, **params)
|
|
86
|
+
@values_assigned = false
|
|
87
|
+
@__params = stringified_params.nil? ? params : stringified_params
|
|
88
|
+
@__raw_params = @__params
|
|
73
89
|
@raise_on_error = raise_on_error
|
|
74
90
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
91
|
+
if @__params
|
|
92
|
+
validate_required!
|
|
93
|
+
validate_optional!
|
|
94
|
+
assign_values!
|
|
95
|
+
@values_assigned = true
|
|
96
|
+
end
|
|
78
97
|
end
|
|
79
98
|
|
|
99
|
+
|
|
80
100
|
private
|
|
81
101
|
|
|
82
102
|
def assign_values!
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
"#<#{self.class} - required fields: #{self.class.required_fields.map(&:name)}; optional fields: #{self.class.optional_fields.map(&:name)}>"
|
|
26
|
+
end
|
|
27
|
+
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.4.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: 2022-03-
|
|
11
|
+
date: 2022-03-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: pry-byebug
|
|
@@ -94,6 +94,7 @@ files:
|
|
|
94
94
|
- lib/json_schematize/field_transformations.rb
|
|
95
95
|
- lib/json_schematize/field_validators.rb
|
|
96
96
|
- lib/json_schematize/generator.rb
|
|
97
|
+
- lib/json_schematize/introspect.rb
|
|
97
98
|
- lib/json_schematize/version.rb
|
|
98
99
|
homepage: https://github.com/matt-taylor/json_schematize
|
|
99
100
|
licenses:
|