json_schematize 0.1.0 → 0.3.1
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/.circleci/config.yml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +7 -0
- data/json_schematize.gemspec +2 -2
- data/lib/json_schematize/base.rb +7 -0
- data/lib/json_schematize/boolean.rb +19 -0
- data/lib/json_schematize/field.rb +14 -2
- data/lib/json_schematize/generator.rb +3 -3
- data/lib/json_schematize/version.rb +1 -1
- data/lib/json_schematize.rb +6 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 545006e10e9c73489b4a2db37d7b787a49c4427dd58d541ac938bad4c73954ce
|
4
|
+
data.tar.gz: 38f4d89c9cb2ffc8b9863531d8745eae5acd2b2bc1379ea6e9f459d66550525e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d640bd68de88a3e6e3b4a73fd8688e9c017bbb8948a73abb3a2f4bf36a87c3ba1bd31bb08f052b8fd36bca9a7758f825f32448ae39930610eaae7323ccd336fb
|
7
|
+
data.tar.gz: 344f9dd96df21d5b5f8f8b6daca078281403606c5b75dcc958d999186380e83a56ddb592f071b271656882ad9d4a117ce7211ca050cb901e4f57fbb90fe2355e
|
data/.circleci/config.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -85,7 +85,14 @@ 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
|
+
### Custom Classes
|
88
89
|
|
90
|
+
```ruby
|
91
|
+
class CustomClasses < JsonSchematize::Generator
|
92
|
+
# JsonSchematize::Boolean can be used as a type when expecting a conversion of possible true or false values converted into a TrueClass or FalseClass
|
93
|
+
add_field name: :internals, type: JsonSchematize::Boolean
|
94
|
+
end
|
95
|
+
```
|
89
96
|
|
90
97
|
## Development
|
91
98
|
|
data/json_schematize.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Matt Taylor"]
|
9
9
|
spec.email = ["mattius.taylor@gmail.com"]
|
10
10
|
|
11
|
-
spec.summary = "
|
12
|
-
spec.description = "
|
11
|
+
spec.summary = "This gem gives you the ability to turn API results into a standardized schema's that can be easily grocked"
|
12
|
+
spec.description = "Take standardized API results and turn them into a Schema"
|
13
13
|
spec.homepage = "https://github.com/matt-taylor/json_schematize"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -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
|
@@ -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
|
|
@@ -67,9 +67,9 @@ class JsonSchematize::Generator
|
|
67
67
|
|
68
68
|
attr_reader :__raw_params, :raise_on_error
|
69
69
|
|
70
|
-
|
71
|
-
def initialize(raise_on_error: true, **params)
|
72
|
-
@__params = params
|
70
|
+
# stringified_params allows for params with stringed keys
|
71
|
+
def initialize(stringified_params = {}, raise_on_error: true, **params)
|
72
|
+
@__params = stringified_params.empty? ? params : stringified_params
|
73
73
|
@raise_on_error = raise_on_error
|
74
74
|
|
75
75
|
validate_required!
|
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.1
|
4
|
+
version: 0.3.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-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-byebug
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 0.17.0
|
69
|
-
description:
|
69
|
+
description: Take standardized API results and turn them into a Schema
|
70
70
|
email:
|
71
71
|
- mattius.taylor@gmail.com
|
72
72
|
executables: []
|
@@ -88,6 +88,8 @@ 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
|
@@ -117,5 +119,6 @@ requirements: []
|
|
117
119
|
rubygems_version: 3.2.3
|
118
120
|
signing_key:
|
119
121
|
specification_version: 4
|
120
|
-
summary:
|
122
|
+
summary: This gem gives you the ability to turn API results into a standardized schema's
|
123
|
+
that can be easily grocked
|
121
124
|
test_files: []
|