saphyr 0.4.1.beta → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 308c4d5e58f64c5a12e32475c518f2c40b7416341bb9379f4ab765101e9f6774
4
- data.tar.gz: 46406b229c5edd551d1f0f7d235806b188854b20ee321a217e7f5f12eb0ae04d
3
+ metadata.gz: 6b1c26f4af7159d647c18c278b0a57abf7583a8876b401f15f396fb538850f07
4
+ data.tar.gz: 29f8fe0a5e93e4d08c6eee677e13a29687f939493d9591e717050986df297089
5
5
  SHA512:
6
- metadata.gz: 3f44784114785401e51caa66879a4c3a285832bae4d5631809f554328d1b8a0e07aea528904f09c141741407e0bb27ee832787ddd0ca84c1d3125440e13babfc
7
- data.tar.gz: 51e5ae7a07675cefb0976e4f54f9fc6673fb7e15da6f0beb850bb9d8a38d6130abdba9474ad60bf104532cfe9918801a39ca6e26a77001c6677a0c3ad8efb923
6
+ metadata.gz: 6419db7b64a4a99c50ed40fae4ba2fd8ad098bc92aa3696b5d4a2e43264c6f2dbad98c11841c95944ebf58be4b62f2e3b328b8d17f6f235d8e65cd55019394ea
7
+ data.tar.gz: d4c40c6b13270e89551558112ce962cc0c0c8a0ae3ae46529c57a552ab25243b3c19f96e513dc87db509eb22a12b52e10a6438fa7c66d94125f3f602557f2f88
data/CHANGELOG CHANGED
@@ -1,7 +1,16 @@
1
- No changelog update during all the beta development phase.
1
+ -----------------------------------------------------------------
2
+ Version: 0.5.0 - 2025-05-21
3
+
4
+ - Refactor the way we validate field type
5
+ Adding EXPECTED_TYPES to field_base to automate the check
6
+ - Add :default option to field
2
7
 
3
8
  -----------------------------------------------------------------
4
- Version: 0.1.0.beta - 2023-08-30
9
+ Version: 0.4.2.beta - 2025-05-06
10
+
11
+ - Fix CVE-2024-47220 upgrade webrick to ~> 1.8.2
12
+
5
13
  -----------------------------------------------------------------
14
+ Version: 0.1.0.beta - 2023-08-30
6
15
 
7
16
  - Initial release
data/lib/saphyr/engine.rb CHANGED
@@ -171,20 +171,25 @@ module Saphyr
171
171
  end
172
172
 
173
173
  unless data.key? name
174
- next unless @ctx.schema.strict?
175
- if field.required?
176
- @ctx.errors << {
177
- path: field_path,
178
- errors: [
179
- {
180
- type: 'strict_mode:missing_in_data',
181
- data: {
182
- field: name,
174
+ if field.default?
175
+ data[name] = field.opts[:default]
176
+ else
177
+ next unless @ctx.schema.strict?
178
+ if field.required?
179
+ @ctx.errors << {
180
+ path: field_path,
181
+ errors: [
182
+ {
183
+ type: 'strict_mode:missing_in_data',
184
+ data: {
185
+ field: name,
186
+ }
183
187
  }
184
- }
185
- ],
186
- }
188
+ ],
189
+ }
190
+ end
187
191
  end
192
+
188
193
  next
189
194
  end
190
195
 
@@ -3,7 +3,7 @@ module Saphyr
3
3
 
4
4
  class ArrayField < FieldBase
5
5
  PREFIX = 'array'
6
-
6
+ EXPECTED_TYPES = Array
7
7
  AUTHORIZED_OPTIONS = [:len, :min, :max, :of_type, :of_schema, :opts]
8
8
 
9
9
  REQUIRED_ONE_OF_OPTIONS = [:of_type, :of_schema]
@@ -34,7 +34,6 @@ module Saphyr
34
34
  private
35
35
 
36
36
  def do_validate(ctx, name, value, errors)
37
- return unless assert_class Array, value, errors
38
37
  assert_size_len @opts[:len], value, errors
39
38
  assert_size_min @opts[:min], value, errors
40
39
  assert_size_max @opts[:max], value, errors
@@ -6,13 +6,12 @@ module Saphyr
6
6
  # Allowed options are: +:eq+.
7
7
  class BooleanField < FieldBase
8
8
  PREFIX = 'boolean'
9
-
9
+ EXPECTED_TYPES = [TrueClass, FalseClass]
10
10
  AUTHORIZED_OPTIONS = [:eq]
11
11
 
12
12
  private
13
13
 
14
14
  def do_validate(ctx, name, value, errors)
15
- return unless assert_class [TrueClass, FalseClass], value, errors
16
15
  assert_eq @opts[:eq], value, errors
17
16
  end
18
17
  end
@@ -17,13 +17,18 @@ module Saphyr
17
17
 
18
18
  # Every field type has the +:required+ and +:nullable+ options.
19
19
  # @api private
20
- DEFAULT_OPT_VALUES = {required: true, nullable: false}.freeze
20
+ DEFAULT_OPT_VALUES = {required: true, nullable: false, default: :_none_}.freeze
21
21
  # @api private
22
22
  DEFAULT_OPTS = DEFAULT_OPT_VALUES.keys.freeze
23
23
 
24
24
  # NOTE:
25
25
  # Override following constants to manage options
26
26
 
27
+ # List of expected classes for the field. It can be an array of class names, or
28
+ # a single class name. EX: [TrueClass, FalseClass] or Integer.
29
+ # @note Overriding this class constant is mandatory.
30
+ EXPECTED_TYPES = nil
31
+
27
32
  # List of authorized options.
28
33
  # @note Override this class constant if you want to use this feature.
29
34
  AUTHORIZED_OPTIONS = []
@@ -55,8 +60,10 @@ module Saphyr
55
60
  # @note Override this class constant if you want to use this feature.
56
61
  NOT_SUP_OR_EQUALS_OPTIONS = []
57
62
 
58
-
59
63
  def initialize(opts={})
64
+ if self.class::EXPECTED_TYPES.nil?
65
+ raise Saphyr::Error.new "The 'EXPECTED_TYPES' constant must be defined"
66
+ end
60
67
  if opts.key? :required
61
68
  unless assert_boolean opts[:required]
62
69
  raise Saphyr::Error.new "Option ':required' must be a Boolean"
@@ -67,10 +74,15 @@ module Saphyr
67
74
  raise Saphyr::Error.new "Option ':nullable' must be a Boolean"
68
75
  end
69
76
  end
77
+ if opts.key? :default
78
+ unless assert_class self.class::EXPECTED_TYPES, opts[:default], []
79
+ raise Saphyr::Error.new "Option ':default' bad type. Expecting: '#{self.class::EXPECTED_TYPES.to_s}', got: '#{opts[:default].class.name}'"
80
+ end
81
+ end
70
82
 
71
83
  unless authorized_options.size == 0
72
84
  opts.keys.each do |opt|
73
- next if opt == :required or opt == :nullable
85
+ next if opt == :required or opt == :nullable or opt == :default
74
86
  unless authorized_options.include? opt
75
87
  raise Saphyr::Error.new "Unauthorized option: #{opt}"
76
88
  end
@@ -212,6 +224,10 @@ module Saphyr
212
224
  @opts[:nullable]
213
225
  end
214
226
 
227
+ def default?
228
+ @opts[:default] != :_none_
229
+ end
230
+
215
231
  # -----
216
232
 
217
233
  # Check if the field value is valid.
@@ -221,6 +237,7 @@ module Saphyr
221
237
  def validate(ctx, name, value)
222
238
  # NOTE: Nullable is handle by the engine.
223
239
  errors = []
240
+ return errors unless assert_class self.class::EXPECTED_TYPES, value, errors
224
241
  do_validate ctx, name, value, errors
225
242
  errors
226
243
  end
@@ -6,7 +6,7 @@ module Saphyr
6
6
  # Allowed options are: +:eq, :gt, :gte, :lt, :lte, :in+.
7
7
  class FloatField < FieldBase
8
8
  PREFIX = 'float'
9
-
9
+ EXPECTED_TYPES = Float
10
10
  AUTHORIZED_OPTIONS = [:eq, :gt, :gte, :lt, :lte, :in]
11
11
 
12
12
  EXCLUSIVE_OPTIONS = [
@@ -36,7 +36,6 @@ module Saphyr
36
36
  private
37
37
 
38
38
  def do_validate(ctx, name, value, errors)
39
- return unless assert_class Float, value, errors
40
39
  assert_eq @opts[:eq], value, errors
41
40
  assert_numeric_gt @opts[:gt], value, errors
42
41
  assert_numeric_gte @opts[:gte], value, errors
@@ -6,7 +6,7 @@ module Saphyr
6
6
  # Allowed options are: +:eq, :gt, :gte, :lt, :lte, :in+.
7
7
  class IntegerField < FieldBase
8
8
  PREFIX = 'integer'
9
-
9
+ EXPECTED_TYPES = Integer
10
10
  AUTHORIZED_OPTIONS = [:eq, :gt, :gte, :lt, :lte, :in]
11
11
 
12
12
  EXCLUSIVE_OPTIONS = [
@@ -36,7 +36,6 @@ module Saphyr
36
36
  private
37
37
 
38
38
  def do_validate(ctx, name, value, errors)
39
- return unless assert_class Integer, value, errors
40
39
  assert_eq @opts[:eq], value, errors
41
40
  assert_numeric_gt @opts[:gt], value, errors
42
41
  assert_numeric_gte @opts[:gte], value, errors
@@ -3,9 +3,8 @@ module Saphyr
3
3
 
4
4
  class SchemaField < FieldBase
5
5
  PREFIX = 'schema'
6
-
6
+ EXPECTED_TYPES = Hash
7
7
  AUTHORIZED_OPTIONS = [:name]
8
-
9
8
  REQUIRED_OPTIONS = [:name]
10
9
 
11
10
  private
@@ -6,7 +6,7 @@ module Saphyr
6
6
  # Allowed options are: +:eq, :len, :min, :max, :in, :regexp+.
7
7
  class StringField < FieldBase
8
8
  PREFIX = 'string'
9
-
9
+ EXPECTED_TYPES = String
10
10
  AUTHORIZED_OPTIONS = [:eq, :len, :min, :max, :in, :regexp]
11
11
 
12
12
  EXCLUSIVE_OPTIONS = [
@@ -28,7 +28,6 @@ module Saphyr
28
28
  private
29
29
 
30
30
  def do_validate(ctx, name, value, errors)
31
- return unless assert_class String, value, errors
32
31
  assert_eq @opts[:eq], value, errors
33
32
  assert_size_len @opts[:len], value, errors
34
33
  assert_size_min @opts[:min], value, errors
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Saphyr
4
- VERSION = "0.4.1.beta"
4
+ VERSION = '0.5.0'
5
5
  end
@@ -4,7 +4,7 @@ By default the `Saphyr` library is including many field types.
4
4
 
5
5
  ## Common options
6
6
 
7
- All field type have the common `:required`, `:nullable`.
7
+ All field type have the common `:required`, `:nullable` and `:nullable` options.
8
8
 
9
9
  ## String
10
10
 
@@ -15,10 +15,10 @@ Here is an example with all possible options for `:string` type:
15
15
  ```ruby
16
16
  class MyValidator < Saphyr::Validator
17
17
  field :name, :string
18
- field :name, :string, eq: 'v1.1'
19
- field :name, :string, min: 5, max: 50
20
- field :name, :string, max: 50
21
- field :name, :string, len: 15
18
+ field :name, :string, eq: 'v1.1' # Field name can be a
19
+ field "name", :string, min: 5, max: 50 # Symbol or a String
20
+ field "name", :string, max: 50
21
+ field "name", :string, len: 15
22
22
  field :name, :string, len: 15, regexp: /^[a-f0-9]+$/
23
23
  field :name, :string, regexp: /^[A-Z0-9]{15}$/
24
24
  field :name, :string, in: ['jpg', 'png', 'gif']
@@ -42,15 +42,15 @@ Here is an example with all possible options for `:integer` type:
42
42
 
43
43
  ```ruby
44
44
  class MyValidator < Saphyr::Validator
45
- field :count, :integer
46
- field :count, :integer, eq: 'v1.1'
47
- field :count, :integer, gt: 0
48
- field :count, :integer, lt: 50
49
- field :count, :integer, gte: 5, lte: 50
50
- field :count, :integer, in: ['jpg', 'png', 'gif']
45
+ field :name, :integer
46
+ field :name, :integer, eq: 'v1.1'
47
+ field :name, :integer, gt: 0
48
+ field :name, :integer, lt: 50
49
+ field :name, :integer, gte: 5, lte: 50
50
+ field :name, :integer, in: ['jpg', 'png', 'gif']
51
51
 
52
52
  field :count, :integer, required: false, gte: 10
53
- field :count, :integer, nullable: true, lte: 1024
53
+ field :round, :integer, nullable: true, lte: 1024
54
54
  end
55
55
  ```
56
56
 
@@ -65,15 +65,15 @@ Here is an example with all possible options for `:float` type:
65
65
 
66
66
  ```ruby
67
67
  class MyValidator < Saphyr::Validator
68
- field :price, :float
69
- field :price, :float, eq: 15.1
70
- field :price, :float, gt: 0
71
- field :price, :float, lt: 50
72
- field :price, :float, gte: 5, lte: 50
73
- field :price, :float, in: ['jpg', 'png', 'gif']
74
-
75
- field :price, :float, required: false, gte: 10
76
- field :price, :float, nullable: true, lte: 1024
68
+ field :name, :float
69
+ field :name, :float, eq: 15.1
70
+ field :name, :float, gt: 0
71
+ field :name, :float, lt: 50
72
+ field :name, :float, gte: 5, lte: 50
73
+ field :name, :float, in: ['jpg', 'png', 'gif']
74
+
75
+ field :price, :float, required: false, gte: 10
76
+ field :discount, :float, nullable: true, lte: 1024
77
77
  end
78
78
  ```
79
79
 
@@ -88,12 +88,12 @@ Here is an example with all possible options for `:boolean` type:
88
88
 
89
89
  ```ruby
90
90
  class MyValidator < Saphyr::Validator
91
- field :active, :boolean
92
- field :active, :boolean, eq: true
93
- field :active, :boolean, eq: false
91
+ field :name, :boolean
92
+ field :name, :boolean, eq: true
93
+ field :name, :boolean, eq: false
94
94
 
95
- field :active, :boolean, required: false
96
- field :active, :boolean, nullable: true
95
+ field :active, :boolean, required: false
96
+ field :processed, :boolean, nullable: true
97
97
  end
98
98
  ```
99
99
 
@@ -0,0 +1,47 @@
1
+ # How To Define a Field Type
2
+
3
+ It's possible to define your own custom type.
4
+
5
+ You can take inspiration from `lib/saphyr/fields/*_field.rb`.
6
+
7
+ Here is an example:
8
+
9
+ ## Define custom B62 field
10
+
11
+ ```ruby
12
+ class B62Field < Saphyr::Fields::FieldBase
13
+ PREFIX = 'b62'
14
+ EXPECTED_TYPES = String
15
+ AUTHORIZED_OPTIONS = [:len, :min, :max]
16
+
17
+ EXCLUSIVE_OPTIONS = [
18
+ [ :len, [:min, :max] ], # It mean if you use 'len' then you
19
+ ] # you can't use 'min' or 'max' and vice versa
20
+
21
+ private
22
+
23
+ def do_validate(ctx, name, value, errors)
24
+ assert_size_len @opts[:len], value, errors
25
+ assert_size_min @opts[:min], value, errors
26
+ assert_size_max @opts[:max], value, errors
27
+ assert_string_regexp /^[a-zA-Z0-9]+$/, value, errors, ERR_BAD_FORMAT
28
+ end
29
+ end
30
+ ```
31
+
32
+ ## Register the custom type
33
+
34
+ ```ruby
35
+ Saphyr.register do
36
+ field_type :b62, B62Field
37
+ end
38
+ ```
39
+
40
+ ## Use the custom type in a validator
41
+
42
+ ```ruby
43
+ class ItemValidator < Saphyr::Validator
44
+ field :id, :integer, gte: 1
45
+ field :uuid, :b62, min: 10, max: 100
46
+ end
47
+ ```
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saphyr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1.beta
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - odelbos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-19 00:00:00.000000000 Z
11
+ date: 2025-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -50,6 +50,7 @@ extra_rdoc_files:
50
50
  - rdoc/01_Define_Schema.md
51
51
  - rdoc/02_Field_Types.md
52
52
  - rdoc/03_Strict_Mode.md
53
+ - rdoc/04_Define_Custom_Field_Type.md
53
54
  files:
54
55
  - CHANGELOG
55
56
  - LICENSE
@@ -77,12 +78,12 @@ files:
77
78
  - rdoc/01_Define_Schema.md
78
79
  - rdoc/02_Field_Types.md
79
80
  - rdoc/03_Strict_Mode.md
81
+ - rdoc/04_Define_Custom_Field_Type.md
80
82
  homepage: https://github.com/odelbos/saphyr
81
83
  licenses:
82
84
  - MIT
83
85
  metadata:
84
86
  homepage_uri: https://github.com/odelbos/saphyr
85
- source_code_uri: https://github.com/odelbos/saphyr
86
87
  changelog_uri: https://github.com/odelbos/saphyr/blob/main/CHANGELOG
87
88
  post_install_message:
88
89
  rdoc_options: []