treaty 0.2.0 → 0.3.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: 2697d0627eb77c0671a3b71a5aacefc47acd90339214badcc68ab30ad61b96b0
4
- data.tar.gz: 32dfce4473730165c99715bbc83d2c822148fb6392bc47a098bcd7771131b748
3
+ metadata.gz: 34516da12bec1bd3df351363dacd13fc3e7e9c31806fef4a6aecf37be88754a8
4
+ data.tar.gz: 71bb387ce08aa6a3d42de04bef6e2c7e281089184e0815ad2679a9b87df30213
5
5
  SHA512:
6
- metadata.gz: b6681d643573b27f56102619b50ef8f48e332b834e580f4d0dc164f0c5e298f221da2877db78d2c88853857f682b5f183b58c86e0b2d72c540a7462670d150d7
7
- data.tar.gz: 70f69a8794ff010e115fdb80c81fc954ed1bd2881509cbcbf9d714957714c298775291e03f2268633d4fc8e293820a3a3cdd93cc2cb249b3beb51ffd64029037
6
+ metadata.gz: '0398e7fa2f958bb24331f239cedd5db66880e2e08f402b82659b63a99941176e42ff64c867bdcd5c2aae054e4b35410f132d44f37724046c14b5c6967e9c6e6a'
7
+ data.tar.gz: 25433a8f90b4e6ee4528e3fa933be6ad7a6443ad593b12c99b9cc2fb3a110723b92690a2bbb2843e7de14fc27f5e7b806008fd361c31bbd4c74bd2a906e18b7f
@@ -14,6 +14,7 @@ en:
14
14
  mismatch:
15
15
  integer: "Attribute '%{attribute}' must be an Integer, got %{actual}"
16
16
  string: "Attribute '%{attribute}' must be a String, got %{actual}"
17
+ boolean: "Attribute '%{attribute}' must be a Boolean (true or false), got %{actual}"
17
18
  object: "Attribute '%{attribute}' must be a Hash (object), got %{actual}"
18
19
  array: "Attribute '%{attribute}' must be an Array, got %{actual}"
19
20
  datetime: "Attribute '%{attribute}' must be a DateTime/Time/Date, got %{actual}"
@@ -10,6 +10,7 @@ module Treaty
10
10
  #
11
11
  # - `:integer` - Ruby Integer
12
12
  # - `:string` - Ruby String
13
+ # - `:boolean` - Ruby TrueClass or FalseClass
13
14
  # - `:object` - Ruby Hash (for nested objects)
14
15
  # - `:array` - Ruby Array (for collections)
15
16
  # - `:datetime` - Ruby DateTime, Time, or Date
@@ -19,6 +20,7 @@ module Treaty
19
20
  # Simple types:
20
21
  # integer :age
21
22
  # string :name
23
+ # boolean :published
22
24
  # datetime :created_at
23
25
  #
24
26
  # Nested structures:
@@ -41,7 +43,7 @@ module Treaty
41
43
  # TypeValidator doesn't use option_schema - it validates based on attribute_type.
42
44
  # This validator is always active for all attributes.
43
45
  class TypeValidator < Treaty::Attribute::Option::Base
44
- ALLOWED_TYPES = %i[integer string object array datetime].freeze
46
+ ALLOWED_TYPES = %i[integer string boolean object array datetime].freeze
45
47
 
46
48
  # Validates that the attribute type is one of the allowed types
47
49
  #
@@ -63,7 +65,7 @@ module Treaty
63
65
  # @param value [Object] The value to validate
64
66
  # @raise [Treaty::Exceptions::Validation] If value type doesn't match
65
67
  # @return [void]
66
- def validate_value!(value) # rubocop:disable Metrics/MethodLength
68
+ def validate_value!(value) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
67
69
  return if value.nil? # Type validation doesn't check for nil, required does.
68
70
 
69
71
  case @attribute_type
@@ -71,6 +73,8 @@ module Treaty
71
73
  validate_integer!(value)
72
74
  when :string
73
75
  validate_string!(value)
76
+ when :boolean
77
+ validate_boolean!(value)
74
78
  when :object
75
79
  validate_object!(value)
76
80
  when :array
@@ -110,6 +114,20 @@ module Treaty
110
114
  actual: value.class)
111
115
  end
112
116
 
117
+ # Validates that value is a Boolean (TrueClass or FalseClass)
118
+ #
119
+ # @param value [Object] The value to validate
120
+ # @raise [Treaty::Exceptions::Validation] If value is not a Boolean
121
+ # @return [void]
122
+ def validate_boolean!(value)
123
+ return if value.is_a?(TrueClass) || value.is_a?(FalseClass)
124
+
125
+ raise Treaty::Exceptions::Validation,
126
+ I18n.t("treaty.attributes.validators.type.mismatch.boolean",
127
+ attribute: @attribute_name,
128
+ actual: value.class)
129
+ end
130
+
113
131
  # Validates that value is a Hash (object type)
114
132
  #
115
133
  # @param value [Object] The value to validate
@@ -3,7 +3,7 @@
3
3
  module Treaty
4
4
  module VERSION
5
5
  MAJOR = 0
6
- MINOR = 2
6
+ MINOR = 3
7
7
  PATCH = 0
8
8
  PRE = nil
9
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: treaty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov