hash_cast 0.5.0 → 0.5.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 +4 -4
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +1 -1
- data/lib/hash_cast/attributes_caster.rb +1 -1
- data/lib/hash_cast/casters/string_caster.rb +13 -0
- data/lib/hash_cast/config.rb +7 -1
- data/lib/hash_cast/version.rb +1 -1
- data/spec/hash_cast/caster_spec.rb +29 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68e050b609ffe18b09ada29f3606534113be56827b161bae6e917cbeadc82a57
|
4
|
+
data.tar.gz: 76b1ba697a03d6edcccb6f5196d3411d8af423ff5e0e0b1661b81db3e02846e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7fc1162f788fd180c0cd1a080bb2dbbcaaeb7b28fa1d59b3d490a0bd3f744a3457998d65fc1ce6d2e9819ce898416d6832c658d8bd5e3d47486980d6a4a1db3
|
7
|
+
data.tar.gz: 1d9d02707117f5619902867966c4e0a1f542e0e9aedd2b183557aaf25c6fc85704d488db2c52c22534a377779a68b1bc9d09b31420ad77af54576d701358f9fe
|
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
@@ -23,7 +23,7 @@ class HashCast::AttributesCaster
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
if !options[:skip_unexpected_attributes]
|
26
|
+
if options.has_key?(:skip_unexpected_attributes) && !options[:skip_unexpected_attributes]
|
27
27
|
check_unexpected_attributes_not_given!(hash_keys, casted_hash.keys)
|
28
28
|
end
|
29
29
|
|
@@ -1,6 +1,19 @@
|
|
1
1
|
class HashCast::Casters::StringCaster
|
2
|
+
NULL_BYTE_CHARACTER = "\u0000".freeze
|
2
3
|
|
3
4
|
def self.cast(value, attr_name, options = {})
|
5
|
+
casted_value = cast_string(value, attr_name, options)
|
6
|
+
|
7
|
+
if HashCast.config.validate_string_null_byte && casted_value.match?(NULL_BYTE_CHARACTER)
|
8
|
+
raise HashCast::Errors::CastingError, 'contains invalid characters'
|
9
|
+
end
|
10
|
+
|
11
|
+
casted_value
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def self.cast_string(value, attr_name, options = {})
|
4
17
|
if value.is_a?(String)
|
5
18
|
value
|
6
19
|
elsif value.is_a?(Symbol)
|
data/lib/hash_cast/config.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class HashCast::Config
|
2
|
-
attr_accessor :input_keys, :output_keys
|
2
|
+
attr_accessor :input_keys, :output_keys, :validate_string_null_byte
|
3
3
|
|
4
4
|
def input_keys
|
5
5
|
@input_keys || :symbol
|
@@ -8,4 +8,10 @@ class HashCast::Config
|
|
8
8
|
def output_keys
|
9
9
|
@output_keys || :symbol
|
10
10
|
end
|
11
|
+
|
12
|
+
def validate_string_null_byte
|
13
|
+
return true if @validate_string_null_byte.nil?
|
14
|
+
|
15
|
+
@validate_string_null_byte
|
16
|
+
end
|
11
17
|
end
|
data/lib/hash_cast/version.rb
CHANGED
@@ -221,7 +221,7 @@ describe HashCast::Caster do
|
|
221
221
|
end.to_not raise_error
|
222
222
|
end
|
223
223
|
|
224
|
-
it "
|
224
|
+
it "raises error if skip_unexpected_attributes=false and unexpected attribute was given" do
|
225
225
|
input_hash = {
|
226
226
|
contact: {
|
227
227
|
wrong_attribute: 'foo',
|
@@ -248,11 +248,11 @@ describe HashCast::Caster do
|
|
248
248
|
}
|
249
249
|
|
250
250
|
expect do
|
251
|
-
ContactCaster.cast(input_hash)
|
251
|
+
ContactCaster.cast(input_hash, skip_unexpected_attributes: false)
|
252
252
|
end.to raise_error(HashCast::Errors::UnexpectedAttributeError, "contact[wrong_attribute] is not valid attribute name")
|
253
253
|
end
|
254
254
|
|
255
|
-
it "
|
255
|
+
it "doesn't raise unexpected attributes error by default" do
|
256
256
|
input_hash = {
|
257
257
|
contact: {
|
258
258
|
wrong_attribute: 'foo',
|
@@ -279,7 +279,7 @@ describe HashCast::Caster do
|
|
279
279
|
}
|
280
280
|
|
281
281
|
expect do
|
282
|
-
ContactCaster.cast(input_hash
|
282
|
+
ContactCaster.cast(input_hash)
|
283
283
|
end.not_to raise_error(HashCast::Errors::UnexpectedAttributeError)
|
284
284
|
|
285
285
|
end
|
@@ -381,4 +381,29 @@ describe HashCast::Caster do
|
|
381
381
|
end.to raise_error(HashCast::Errors::CastingError, "city should be a string")
|
382
382
|
end
|
383
383
|
end
|
384
|
+
|
385
|
+
context "string caster" do
|
386
|
+
before(:all) do
|
387
|
+
class HomeCaster
|
388
|
+
include HashCast::Caster
|
389
|
+
|
390
|
+
attributes do
|
391
|
+
string :city
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
after{ HashCast.config.validate_string_null_byte = nil }
|
397
|
+
|
398
|
+
it "should allow null byte if validate_string_null_byte config is set to false" do
|
399
|
+
HashCast.config.validate_string_null_byte = false
|
400
|
+
HomeCaster.cast(city: "\u0000")
|
401
|
+
end
|
402
|
+
|
403
|
+
it "should not allow null byte if validate_string_null_byte config by default" do
|
404
|
+
expect do
|
405
|
+
HomeCaster.cast(city: "\u0000")
|
406
|
+
end.to raise_error(HashCast::Errors::CastingError, "city contains invalid characters")
|
407
|
+
end
|
408
|
+
end
|
384
409
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_cast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albert Gazizov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- ".rspec"
|
51
51
|
- ".ruby-gemset"
|
52
52
|
- ".ruby-version"
|
53
|
+
- CHANGELOG.md
|
53
54
|
- Gemfile
|
54
55
|
- Gemfile.lock
|
55
56
|
- LICENSE.txt
|