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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 672fe60de0d65a961d15b86a8461c967815818aad2bae13bef645bba235a6808
4
- data.tar.gz: 1831d51bdc595939bc3c61b66f72d42ce6c3d82af944c2a09b83710d1582eb5a
3
+ metadata.gz: 68e050b609ffe18b09ada29f3606534113be56827b161bae6e917cbeadc82a57
4
+ data.tar.gz: 76b1ba697a03d6edcccb6f5196d3411d8af423ff5e0e0b1661b81db3e02846e6
5
5
  SHA512:
6
- metadata.gz: 054603f4b2ab7fcc8b0d47ded07535bd8f550a8950515bda53753ac52d45c61b72a81162133c26e0ab2188d0415101d6612c064f2f5fe7e45fd4c50026ba756a
7
- data.tar.gz: 4c4709448714f4ff6e6deaaa5671a0c8e5de0bc54a85bb16a4647c7f2d2a9a45e69f2c01422c06c6a74f71759397d11e99a9e2357d21cf0aa0a576a3c30cdf64
6
+ metadata.gz: e7fc1162f788fd180c0cd1a080bb2dbbcaaeb7b28fa1d59b3d490a0bd3f744a3457998d65fc1ce6d2e9819ce898416d6832c658d8bd5e3d47486980d6a4a1db3
7
+ data.tar.gz: 1d9d02707117f5619902867966c4e0a1f542e0e9aedd2b183557aaf25c6fc85704d488db2c52c22534a377779a68b1bc9d09b31420ad77af54576d701358f9fe
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ **Version 0.5.1**
2
+ - String caster should validate for null byte by default
3
+ - skip_unexpected_attributes option is true by default
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_cast (0.5.0)
4
+ hash_cast (0.5.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module HashCast
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -221,7 +221,7 @@ describe HashCast::Caster do
221
221
  end.to_not raise_error
222
222
  end
223
223
 
224
- it "should raise error if unexpected attribute was given" do
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 "shouldn't unexpected attributes error if skip_unexpected_attributes flag is set to true" do
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, skip_unexpected_attributes: true)
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.0
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-10-28 00:00:00.000000000 Z
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