saphyr 0.6.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d491ec052216eb7db303fa4fb65b6a558ecbb2c39a24663a43ae85eb616da14
4
- data.tar.gz: a07b8137c7e853c0e9625a18760139a220a1be4fdf5271365f81cd2459e288a5
3
+ metadata.gz: 699e716904911896a384f05b33ea53af443ca41e5981330126a4f0c08be75511
4
+ data.tar.gz: ff47bb4681ce194f3a92d10c4ce407f1a63904608a87de105e9c686bf8c4a64a
5
5
  SHA512:
6
- metadata.gz: a816ea8a58c18ae02e599f077f40ce54081e84e8a12ea47e9e3734ce410e238c2562105bc4fd55cf9facfba94477d5856cf708877514eb369f5ffdd85413d7f1
7
- data.tar.gz: bf4a796653f788a49f80bb2b1344d87a059e20a98c8fbd9ed8cd87d13062c228c153c62a8097204f252e69157611cd6d59b6b36b601fbd566b8a9bd0a8c24ff5
6
+ metadata.gz: a05d896442f9fdede8dd1452ebe61ff03a15ba1c5f58b859fdd9f0fbc34c9a0e153da6fdbeb91fa10a035d66c6b9b026c7c909b8cc1d97bb3e7d90562f9f1a73
7
+ data.tar.gz: 45d6db6210eee29afc46c0ff90eb695b99b833fe3d326bc391dc8a436d4c28b032ddacb1dd9264287883ceb34a37cbd5b9ba6492b0f31638f61acea16c1d40e6
data/CHANGELOG CHANGED
@@ -1,3 +1,18 @@
1
+ -----------------------------------------------------------------
2
+ Version: 0.6.1 - 2025-06-29
3
+
4
+ - Refactor email_field to use assert_string_regexp
5
+ - Make get() variadic to fetch deep fields
6
+ - Add get_safe()
7
+
8
+ -----------------------------------------------------------------
9
+ Version: 0.6.0 - 2025-06-21
10
+
11
+ - Add standard field types
12
+ Email, URI, URL, Base64, ipv4, ipv6, ISO Country, ISO Language
13
+ - Add assert_not_empty
14
+ - Refactor expected_types()
15
+
1
16
  -----------------------------------------------------------------
2
17
  Version: 0.5.0 - 2025-05-21
3
18
 
@@ -14,14 +14,7 @@ module Saphyr
14
14
 
15
15
  def do_validate(ctx, name, value, errors)
16
16
  return unless assert_not_empty value, errors
17
- unless value =~ ::URI::MailTo::EMAIL_REGEXP
18
- errors << {
19
- type: err('invalid'),
20
- data: {
21
- _val: value
22
- }
23
- }
24
- end
17
+ assert_string_regexp ::URI::MailTo::EMAIL_REGEXP, value, errors
25
18
  end
26
19
  end
27
20
  end
@@ -119,11 +119,80 @@ module Saphyr
119
119
  end
120
120
 
121
121
  # Get a field from the data to validate.
122
- # @param field [String | Symbol] The field name
122
+ #
123
+ # Use variadic arguments to access deep fields.
124
+ #
125
+ # Examples:
126
+ #
127
+ # data = {
128
+ # "id" => 3465,
129
+ # "info" => {
130
+ # "suffix" => ["gif", "jpg", "png"]
131
+ # "size" => 34056
132
+ # }
133
+ # }
134
+ #
135
+ # get("id") # => 3435
136
+ # get("info", "suffix", 1) # => "jpg"
137
+ # get("info", "size") # => 34056
138
+ # get("name") # => raise an exception
139
+ #
123
140
  # @return The field value
124
- def get(field)
125
- data = @ctx.data_to_validate
126
- data[field.to_s]
141
+ # @raise [Saphyr::Error] If field does not exists.
142
+ def get(*args)
143
+ status, value = get_safe(*args)
144
+ raise Saphyr::Error.new 'Requested field does not exists' if status == :err
145
+ value
146
+ end
147
+
148
+ # Get a field from the data to validate.
149
+ # (Same as +get()+ but never raise an exception).
150
+ #
151
+ # Use variadic arguments to access deep fields.
152
+ #
153
+ # Examples:
154
+ #
155
+ # data = {
156
+ # "id" => 3465,
157
+ # "info" => {
158
+ # "suffix" => ["gif", "jpg", "png"]
159
+ # "size" => 34056
160
+ # }
161
+ # }
162
+ #
163
+ # get("id") # => [:ok, 3435]
164
+ # get("info", "suffix", 1) # => [:ok, "jpg"]
165
+ # get("info", "size") # => [:ok, 34056]
166
+ # get("name") # => [:err, :not_exists]
167
+ # get("info", "suffix", 5) # => [:err, :not_index]
168
+ # get("info", 3) # => [:err, :not_array]
169
+ #
170
+ # @return An array where first element is the status and second element is the result.
171
+ def get_safe(*args)
172
+ do_get_safe @data, args.reverse
173
+ end
174
+
175
+ private
176
+
177
+ def do_get_safe(data, args)
178
+ return [:ok, data] if args.size == 0
179
+ key = args.pop
180
+ if data.is_a? Hash
181
+ key = key.to_s if key.is_a? Symbol
182
+ if data.key? key
183
+ return do_get_safe data[key], args
184
+ else
185
+ return [:err, :not_exists]
186
+ end
187
+ elsif data.is_a? Array
188
+ if key.is_a? Integer
189
+ return do_get_safe data[key], args
190
+ else
191
+ return [:err, :not_index]
192
+ end
193
+ else
194
+ return [:err, :not_array]
195
+ end
127
196
  end
128
197
  end
129
198
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Saphyr
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
5
5
  end
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.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - odelbos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-20 00:00:00.000000000 Z
11
+ date: 2025-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,8 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
- description: The purpose of Saphyr is to provide a nice and simple DSL to easily and
42
- quickly design a validation schema for JSON document.
41
+ description: Simple DSL to design validation schemas for JSON document (or Hash /
42
+ Array structure)
43
43
  email:
44
44
  - od@phibox.com
45
45
  executables: []
@@ -111,5 +111,6 @@ requirements: []
111
111
  rubygems_version: 3.5.6
112
112
  signing_key:
113
113
  specification_version: 4
114
- summary: The saphyr gem is used to validate JSON document.
114
+ summary: Simple DSL to design validation schemas for JSON document (or Hash / Array
115
+ structure)
115
116
  test_files: []