json_schematize 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30ba73765eb1eef7075be586554ca29eddc7ae7b81cc293aa526dc47715fbb77
4
- data.tar.gz: f10b1d7668e6fecd3fc0b8c8a2dcc649df1ede93ce344e2a437209b1d94446c5
3
+ metadata.gz: 8d0fba5a93c9300806e91b5d13da3d16974a72d7598d3b180bd4f9ab13b711c1
4
+ data.tar.gz: 7361e6e1b2994abd945ab62f90f6d3d7a17fd5e8a51ba11f78525316e2062436
5
5
  SHA512:
6
- metadata.gz: a8f5b17a8d1a4d0defb7898b4f8ed2721b960e8b3dbf5216db4ef2dc0d9c56ad9f3d9fe71c67f05735f6838aea729d79d9680614280e142de2f3f2ff17be51c0
7
- data.tar.gz: 51624964387dcd0f1699827e497f170f31a7ac8d542398ac6fd14cb2d81719cc9c6d018b2dc2178718b6e008e320806b41c3bd77a50f208204a023fb63fae76c
6
+ metadata.gz: a0c5362513008667000c309259dfb21e61835eb150f095f18bfdd84cba21e391d3d44de2bd2d72ed7fffc34963122c029d4032e8ecc5d3459d1f5818dd935f5c
7
+ data.tar.gz: 25a59ae832bd2eb65bf92dabe3dac3989f34ad0b46d49e554ec19769b83549b71a2387237b45ee6a7c9e3472662db74c197029ecae2bda1b1ce6b292fdaf79b1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schematize (0.3.0)
4
+ json_schematize (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -74,7 +74,7 @@ schema.id #=> 999999
74
74
  ```
75
75
 
76
76
  ### Field options:
77
- ```bash
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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class JsonSchematize::Base
4
+ def self.acceptable_types
5
+ raise NoMethodError, "Expected acceptable_values to be defined in parent class"
6
+ end
7
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class JsonSchematize::Boolean
3
+ require "json_schematize/base"
4
+
5
+ class JsonSchematize::Boolean < JsonSchematize::Base
4
6
  FALSE_VALUES = ["false", "f", "0", false]
5
7
  TRUE_VALUES = ["true", "t", "1", true]
6
8
 
@@ -10,4 +12,8 @@ class JsonSchematize::Boolean
10
12
 
11
13
  raise JsonSchematize::UndefinedBoolean, "#{val} is not a valid #{self.class}"
12
14
  end
15
+
16
+ def self.acceptable_types
17
+ [TrueClass, FalseClass]
18
+ end
13
19
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class JsonSchematize::EmptyValue
4
+ def initialize(*)
5
+ end
6
+ end
@@ -5,11 +5,12 @@ 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, :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 beofre transformations
27
- valiadtions!
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:)
@@ -36,9 +39,9 @@ class JsonSchematize::Field
36
39
 
37
40
  def acceptable_value?(transformed_value:, raise_on_error:)
38
41
  if array_of_types
39
- boolean = transformed_value.all? { |val| @acceptable_types.include?(val.class) }
42
+ boolean = transformed_value.all? { |val| validate_acceptable_types(val: val) }
40
43
  else
41
- boolean = @acceptable_types.include?(transformed_value.class)
44
+ boolean = validate_acceptable_types(val: transformed_value)
42
45
  end
43
46
 
44
47
  if raise_on_error && (boolean==false)
@@ -77,6 +80,18 @@ class JsonSchematize::Field
77
80
 
78
81
  private
79
82
 
83
+ def validate_acceptable_types(val:)
84
+ (all_allowed_types + @acceptable_types).include?(val.class)
85
+ end
86
+
87
+ def all_allowed_types
88
+ @all_allowed_types ||= begin
89
+ @acceptable_types.map do |t|
90
+ t.acceptable_types if t.ancestors.include?(JsonSchematize::Base)
91
+ end.compact.flatten
92
+ end
93
+ end
94
+
80
95
  def iterate_array_of_types(value:)
81
96
  return raw_converter_call(value: value) unless array_of_types
82
97
 
@@ -90,9 +105,15 @@ class JsonSchematize::Field
90
105
  end
91
106
 
92
107
  def raw_converter_call(value:)
108
+ return convert_empty_value if value.nil? && (required == false)
109
+
93
110
  converter.call(value)
94
111
  end
95
112
 
113
+ def convert_empty_value
114
+ empty_value.class == Class ? empty_value.new : empty_value
115
+ end
116
+
96
117
  include JsonSchematize::FieldTransformations
97
118
  include JsonSchematize::FieldValidators
98
119
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module JsonSchematize::FieldValidators
4
4
 
5
- def valiadtions!
5
+ def validations!
6
6
  validate_type!(t: @type)
7
7
  validate_types!
8
8
  validate_name!
@@ -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
- def self.add_field(name:, type: nil, types: [], dig_type: nil, dig: nil, validator: EMPTY_VALIDATOR, required: true, converter: nil, array_of_types: false)
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
- validator: validator,
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 = {}, raise_on_error: true, **params)
72
- @__params = stringified_params.empty? ? params : stringified_params
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
- validate_required!
76
- validate_optional!
77
- assign_values!
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,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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonSchematize
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json_schematize/version"
4
- require "json_schematize/generator"
3
+ require "json_schematize/base"
5
4
  require "json_schematize/boolean"
5
+ require "json_schematize/generator"
6
+ require "json_schematize/version"
6
7
 
7
8
  module JsonSchematize
8
9
  class Error < StandardError; 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.3.0
4
+ version: 0.5.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-10 00:00:00.000000000 Z
11
+ date: 2022-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry-byebug
@@ -88,11 +88,14 @@ files:
88
88
  - docker-compose.yml
89
89
  - json_schematize.gemspec
90
90
  - lib/json_schematize.rb
91
+ - lib/json_schematize/base.rb
91
92
  - lib/json_schematize/boolean.rb
93
+ - lib/json_schematize/empty_value.rb
92
94
  - lib/json_schematize/field.rb
93
95
  - lib/json_schematize/field_transformations.rb
94
96
  - lib/json_schematize/field_validators.rb
95
97
  - lib/json_schematize/generator.rb
98
+ - lib/json_schematize/introspect.rb
96
99
  - lib/json_schematize/version.rb
97
100
  homepage: https://github.com/matt-taylor/json_schematize
98
101
  licenses: