json_schematize 0.2.0 → 0.4.0
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 +22 -0
- data/lib/json_schematize/base.rb +7 -0
- data/lib/json_schematize/boolean.rb +19 -0
- data/lib/json_schematize/field.rb +16 -4
- data/lib/json_schematize/generator.rb +40 -20
- data/lib/json_schematize/introspect.rb +27 -0
- data/lib/json_schematize/version.rb +1 -1
- data/lib/json_schematize.rb +6 -1
- metadata +5 -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
@@ -86,6 +86,28 @@ converter -- Proc return is set to the field value. No furter validation is done
|
|
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
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
|
+
|
103
|
+
### Custom Classes
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class CustomClasses < JsonSchematize::Generator
|
107
|
+
# JsonSchematize::Boolean can be used as a type when expecting a conversion of possible true or false values converted into a TrueClass or FalseClass
|
108
|
+
add_field name: :internals, type: JsonSchematize::Boolean
|
109
|
+
end
|
110
|
+
```
|
89
111
|
|
90
112
|
## Development
|
91
113
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json_schematize/base"
|
4
|
+
|
5
|
+
class JsonSchematize::Boolean < JsonSchematize::Base
|
6
|
+
FALSE_VALUES = ["false", "f", "0", false]
|
7
|
+
TRUE_VALUES = ["true", "t", "1", true]
|
8
|
+
|
9
|
+
def self.new(val)
|
10
|
+
return false if FALSE_VALUES.include?(val)
|
11
|
+
return true if TRUE_VALUES.include?(val)
|
12
|
+
|
13
|
+
raise JsonSchematize::UndefinedBoolean, "#{val} is not a valid #{self.class}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.acceptable_types
|
17
|
+
[TrueClass, FalseClass]
|
18
|
+
end
|
19
|
+
end
|
@@ -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
|
@@ -36,9 +36,9 @@ class JsonSchematize::Field
|
|
36
36
|
|
37
37
|
def acceptable_value?(transformed_value:, raise_on_error:)
|
38
38
|
if array_of_types
|
39
|
-
boolean = transformed_value.all? { |val|
|
39
|
+
boolean = transformed_value.all? { |val| validate_acceptable_types(val: val) }
|
40
40
|
else
|
41
|
-
boolean =
|
41
|
+
boolean = validate_acceptable_types(val: transformed_value)
|
42
42
|
end
|
43
43
|
|
44
44
|
if raise_on_error && (boolean==false)
|
@@ -77,6 +77,18 @@ class JsonSchematize::Field
|
|
77
77
|
|
78
78
|
private
|
79
79
|
|
80
|
+
def validate_acceptable_types(val:)
|
81
|
+
(all_allowed_types + @acceptable_types).include?(val.class)
|
82
|
+
end
|
83
|
+
|
84
|
+
def all_allowed_types
|
85
|
+
@all_allowed_types ||= begin
|
86
|
+
@acceptable_types.map do |t|
|
87
|
+
t.acceptable_types if t.ancestors.include?(JsonSchematize::Base)
|
88
|
+
end.compact.flatten
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
80
92
|
def iterate_array_of_types(value:)
|
81
93
|
return raw_converter_call(value: value) unless array_of_types
|
82
94
|
|
@@ -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
|
-
|
71
|
-
def initialize(raise_on_error: true, **params)
|
72
|
-
@
|
84
|
+
# stringified_params allows for params with stringed keys
|
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
|
data/lib/json_schematize.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "json_schematize/
|
3
|
+
require "json_schematize/base"
|
4
|
+
require "json_schematize/boolean"
|
4
5
|
require "json_schematize/generator"
|
6
|
+
require "json_schematize/version"
|
5
7
|
|
6
8
|
module JsonSchematize
|
7
9
|
class Error < StandardError; end
|
@@ -10,4 +12,7 @@ module JsonSchematize
|
|
10
12
|
class InvalidFieldByValidator < InvalidField; end
|
11
13
|
class InvalidFieldByType < InvalidField; end
|
12
14
|
class InvalidFieldByArrayOfTypes < InvalidField; end
|
15
|
+
|
16
|
+
## Customized class errors
|
17
|
+
class UndefinedBoolean < Error; end
|
13
18
|
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
|
@@ -88,10 +88,13 @@ files:
|
|
88
88
|
- docker-compose.yml
|
89
89
|
- json_schematize.gemspec
|
90
90
|
- lib/json_schematize.rb
|
91
|
+
- lib/json_schematize/base.rb
|
92
|
+
- lib/json_schematize/boolean.rb
|
91
93
|
- lib/json_schematize/field.rb
|
92
94
|
- lib/json_schematize/field_transformations.rb
|
93
95
|
- lib/json_schematize/field_validators.rb
|
94
96
|
- lib/json_schematize/generator.rb
|
97
|
+
- lib/json_schematize/introspect.rb
|
95
98
|
- lib/json_schematize/version.rb
|
96
99
|
homepage: https://github.com/matt-taylor/json_schematize
|
97
100
|
licenses:
|