saphyr 0.4.2.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 +4 -4
- data/CHANGELOG +6 -3
- data/lib/saphyr/engine.rb +17 -12
- data/lib/saphyr/fields/array_field.rb +1 -2
- data/lib/saphyr/fields/boolean_field.rb +1 -2
- data/lib/saphyr/fields/field_base.rb +20 -3
- data/lib/saphyr/fields/float_field.rb +1 -2
- data/lib/saphyr/fields/integer_field.rb +1 -2
- data/lib/saphyr/fields/schema_field.rb +1 -2
- data/lib/saphyr/fields/string_field.rb +1 -2
- data/lib/saphyr/version.rb +1 -1
- data/rdoc/02_Field_Types.md +26 -26
- data/rdoc/04_Define_Custom_Field_Type.md +47 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b1c26f4af7159d647c18c278b0a57abf7583a8876b401f15f396fb538850f07
|
|
4
|
+
data.tar.gz: 29f8fe0a5e93e4d08c6eee677e13a29687f939493d9591e717050986df297089
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6419db7b64a4a99c50ed40fae4ba2fd8ad098bc92aa3696b5d4a2e43264c6f2dbad98c11841c95944ebf58be4b62f2e3b328b8d17f6f235d8e65cd55019394ea
|
|
7
|
+
data.tar.gz: d4c40c6b13270e89551558112ce962cc0c0c8a0ae3ae46529c57a552ab25243b3c19f96e513dc87db509eb22a12b52e10a6438fa7c66d94125f3f602557f2f88
|
data/CHANGELOG
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
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
9
|
Version: 0.4.2.beta - 2025-05-06
|
|
5
|
-
-----------------------------------------------------------------
|
|
6
10
|
|
|
7
11
|
- Fix CVE-2024-47220 upgrade webrick to ~> 1.8.2
|
|
8
12
|
|
|
9
13
|
-----------------------------------------------------------------
|
|
10
14
|
Version: 0.1.0.beta - 2023-08-30
|
|
11
|
-
-----------------------------------------------------------------
|
|
12
15
|
|
|
13
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
|
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
|
|
@@ -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
|
data/lib/saphyr/version.rb
CHANGED
data/rdoc/02_Field_Types.md
CHANGED
|
@@ -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
|
|
20
|
-
field
|
|
21
|
-
field
|
|
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 :
|
|
46
|
-
field :
|
|
47
|
-
field :
|
|
48
|
-
field :
|
|
49
|
-
field :
|
|
50
|
-
field :
|
|
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 :
|
|
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 :
|
|
69
|
-
field :
|
|
70
|
-
field :
|
|
71
|
-
field :
|
|
72
|
-
field :
|
|
73
|
-
field :
|
|
74
|
-
|
|
75
|
-
field :price,
|
|
76
|
-
field :
|
|
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 :
|
|
92
|
-
field :
|
|
93
|
-
field :
|
|
91
|
+
field :name, :boolean
|
|
92
|
+
field :name, :boolean, eq: true
|
|
93
|
+
field :name, :boolean, eq: false
|
|
94
94
|
|
|
95
|
-
field :active,
|
|
96
|
-
field :
|
|
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
|
+
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: 2025-05-
|
|
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,6 +78,7 @@ 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
|