json_schematize 0.4.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: 92b634c916946aab36db45a5c037f31fc03452d77c3aca4a1f9ca14109cc74aa
4
- data.tar.gz: d3a3b4d1085ce8a415bc03e871ada8e1c6e250df096b566fd9dc1a9aac902204
3
+ metadata.gz: 8d0fba5a93c9300806e91b5d13da3d16974a72d7598d3b180bd4f9ab13b711c1
4
+ data.tar.gz: 7361e6e1b2994abd945ab62f90f6d3d7a17fd5e8a51ba11f78525316e2062436
5
5
  SHA512:
6
- metadata.gz: e0d9a3d86984c6ce6339546b12461cea1374f507d1356f2cc2ebf675f90bb954bad2559a43f9e6a014219120eaf20537bf187dfeb486ba449036de030e5a02b0
7
- data.tar.gz: c6c8546e21abccee5ec36746aa2e12dd5d7cd1dde148a5d1c82ac72abd1252b03dfb815c947b04e0914e9639014a063533d9b736dbde475a32a97b01a02f8ed8
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.4.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,6 +84,7 @@ 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
87
88
  ```
88
89
 
89
90
  ### Schema defaults
@@ -96,7 +97,7 @@ class SchemaWithDefaults < JsonSchematize::Generator
96
97
 
97
98
  add_field name: :internals, type: InternalBody, array_of_types: true
98
99
  add_field name: :id, type: Integer
99
- add_field name: :status, type: Symbol
100
+ add_field name: :status, type: Symbol, required: false, empty_value: "empty"
100
101
  end
101
102
  ```
102
103
 
@@ -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, :dig_type, :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
28
  # validations must be done before transformations
27
- valiadtions!
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
@@ -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,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json_schematize/empty_value"
3
4
  require "json_schematize/field"
4
5
  require "json_schematize/introspect"
5
6
 
@@ -9,7 +10,7 @@ class JsonSchematize::Generator
9
10
 
10
11
  include JsonSchematize::Introspect
11
12
 
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
+ 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
15
  converter: converter || schema_defaults[:converter],
15
16
  dig: dig || schema_defaults[:dig],
@@ -18,6 +19,7 @@ class JsonSchematize::Generator
18
19
  required: (required.nil? ? schema_defaults.fetch(:required, true) : required),
19
20
  type: type || schema_defaults[:type],
20
21
  types: types || schema_defaults.fetch(:types, []),
22
+ empty_value: empty_value || schema_defaults.fetch(:empty_value, JsonSchematize::EmptyValue),
21
23
  validator: validator || schema_defaults.fetch(:validator, EMPTY_VALIDATOR),
22
24
  array_of_types: (array_of_types.nil? ? schema_defaults.fetch(:array_of_types, false) : array_of_types),
23
25
  }
@@ -96,7 +98,6 @@ class JsonSchematize::Generator
96
98
  end
97
99
  end
98
100
 
99
-
100
101
  private
101
102
 
102
103
  def assign_values!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonSchematize
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  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.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-12 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
@@ -90,6 +90,7 @@ 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