| 1 | package Poz::Types::number; | ||
| 2 | use 5.032; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent 'Poz::Types::scalar'; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ($class, $opts) = @_; | ||
| 9 | $opts = $opts || {}; | ||
| 10 | $opts->{required_error} //= "required"; | ||
| 11 | $opts->{invalid_type_error} //= "Not a number"; | ||
| 12 | my $self = $class->SUPER::new($opts); | ||
| 13 | return $self; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub rule { | ||
| 17 | my ($self, $value) = @_; | ||
| 18 | return $self->{required_error} unless defined $value; | ||
| 19 | return $self->{invalid_type_error} unless $value =~ /^-?\d+\.?\d*$/; | ||
| 20 | return; | ||
| 21 | }; | ||
| 22 | |||
| 23 | sub coerce { | ||
| 24 | my ($self, $value) = @_; | ||
| 25 | return $value +0; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub gt { | ||
| 29 | my ($self, $min, $opts) = @_; | ||
| 30 | $opts = $opts || {}; | ||
| 31 | $opts->{message} //= "Too small"; | ||
| 32 | push @{$self->{rules}}, sub { | ||
| 33 | my ($self, $value) = @_; | ||
| 34 | return $opts->{message} if $value <= $min; | ||
| 35 | return; | ||
| 36 | }; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub gte { | ||
| 41 | my ($self, $min, $opts) = @_; | ||
| 42 | $opts = $opts || {}; | ||
| 43 | $opts->{message} //= "Too small"; | ||
| 44 | push @{$self->{rules}}, sub { | ||
| 45 | my ($self, $value) = @_; | ||
| 46 | return $opts->{message} if $value < $min; | ||
| 47 | return; | ||
| 48 | }; | ||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub min { | ||
| 53 | my ($self, $min, $opts) = @_; | ||
| 54 | return $self->gte($min, $opts); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub lt { | ||
| 58 | my ($self, $max, $opts) = @_; | ||
| 59 | $opts = $opts || {}; | ||
| 60 | $opts->{message} //= "Too large"; | ||
| 61 | push @{$self->{rules}}, sub { | ||
| 62 | my ($self, $value) = @_; | ||
| 63 | return $opts->{message} if $value >= $max; | ||
| 64 | return; | ||
| 65 | }; | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub lte { | ||
| 70 | my ($self, $max, $opts) = @_; | ||
| 71 | $opts = $opts || {}; | ||
| 72 | $opts->{message} //= "Too large"; | ||
| 73 | push @{$self->{rules}}, sub { | ||
| 74 | my ($self, $value) = @_; | ||
| 75 | return $opts->{message} if $value > $max; | ||
| 76 | return; | ||
| 77 | }; | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub max { | ||
| 82 | my ($self, $max, $opts) = @_; | ||
| 83 | return $self->lte($max, $opts); | ||
| 84 | } | ||
| 85 | |||
| 86 | # value must be an integer | ||
| 87 | sub int { | ||
| 88 | my ($self, $opts) = @_; | ||
| 89 | $opts = $opts || {}; | ||
| 90 | $opts->{message} //= "Not an integer"; | ||
| 91 | push @{$self->{rules}}, sub { | ||
| 92 | my ($self, $value) = @_; | ||
| 93 | return $opts->{message} if $value !~ /^-?\d+$/; | ||
| 94 | return; | ||
| 95 | }; | ||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub positive { | ||
| 100 | my ($self, $opts) = @_; | ||
| 101 | $opts = $opts || {}; | ||
| 102 | $opts->{message} //= "Not a positive number"; | ||
| 103 | push @{$self->{rules}}, sub { | ||
| 104 | my ($self, $value) = @_; | ||
| 105 | return $opts->{message} if $value <= 0; | ||
| 106 | return; | ||
| 107 | }; | ||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub negative { | ||
| 112 | my ($self, $opts) = @_; | ||
| 113 | $opts = $opts || {}; | ||
| 114 | $opts->{message} //= "Not a negative number"; | ||
| 115 | push @{$self->{rules}}, sub { | ||
| 116 | my ($self, $value) = @_; | ||
| 117 | return $opts->{message} if $value >= 0; | ||
| 118 | return; | ||
| 119 | }; | ||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub nonpositive { | ||
| 124 | my ($self, $opts) = @_; | ||
| 125 | $opts = $opts || {}; | ||
| 126 | $opts->{message} //= "Not a non-positive number"; | ||
| 127 | push @{$self->{rules}}, sub { | ||
| 128 | my ($self, $value) = @_; | ||
| 129 | return $opts->{message} if $value > 0; | ||
| 130 | return; | ||
| 131 | }; | ||
| 132 | return $self; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub nonnegative { | ||
| 136 | my ($self, $opts) = @_; | ||
| 137 | $opts = $opts || {}; | ||
| 138 | $opts->{message} //= "Not a non-negative number"; | ||
| 139 | push @{$self->{rules}}, sub { | ||
| 140 | my ($self, $value) = @_; | ||
| 141 | return $opts->{message} if $value < 0; | ||
| 142 | return; | ||
| 143 | }; | ||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | # Evenly divisible by 5. | ||
| 148 | sub multipleOf { | ||
| 149 | my ($self, $divisor, $opts) = @_; | ||
| 150 | $opts = $opts || {}; | ||
| 151 | $opts->{message} //= "Not a multiple of $divisor"; | ||
| 152 | push @{$self->{rules}}, sub { | ||
| 153 | my ($self, $value) = @_; | ||
| 154 | return $opts->{message} if $value % $divisor != 0; | ||
| 155 | return; | ||
| 156 | }; | ||
| 157 | return $self; | ||
| 158 | } | ||
| 159 | |||
| 160 | # synonym for multipleOf | ||
| 161 | sub step { | ||
| 162 | my ($self, $divisor, $opts) = @_; | ||
| 163 | return $self->multipleOf($divisor, $opts); | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | =head1 NAME | ||
| 169 | |||
| 170 | Poz::Types::number - A module for number type validation and coercion | ||
| 171 | |||
| 172 | =head1 SYNOPSIS | ||
| 173 | |||
| 174 | use Poz qw/z/; | ||
| 175 | |||
| 176 | my $number = z->number; | ||
| 177 | |||
| 178 | # Validate a number | ||
| 179 | $number->rule(42); # No error | ||
| 180 | |||
| 181 | # Coerce a value to a number | ||
| 182 | my $coerced_value = $number->coerce("42.5"); | ||
| 183 | |||
| 184 | # Add validation rules | ||
| 185 | $number->gt(10)->lt(100); | ||
| 186 | |||
| 187 | # Validate with custom rules | ||
| 188 | $number->rule(50); # No error | ||
| 189 | |||
| 190 | =head1 DESCRIPTION | ||
| 191 | |||
| 192 | Poz::Types::number is a module for validating and coercing number types. It provides various methods to enforce constraints on numbers, such as greater than, less than, integer, positive, negative, and multiples of a given number. | ||
| 193 | |||
| 194 | =head1 METHODS | ||
| 195 | |||
| 196 | =head2 rule | ||
| 197 | |||
| 198 | $number->rule($value); | ||
| 199 | |||
| 200 | Validates the given value against the defined rules. Throws an error if the value is invalid. | ||
| 201 | |||
| 202 | =head2 coerce | ||
| 203 | |||
| 204 | my $coerced_value = $number->coerce($value); | ||
| 205 | |||
| 206 | Coerces the given value to a number. | ||
| 207 | |||
| 208 | =head2 gt | ||
| 209 | |||
| 210 | $number->gt($min, \%opts); | ||
| 211 | |||
| 212 | Adds a rule to ensure the value is greater than the specified minimum. | ||
| 213 | |||
| 214 | =head2 gte | ||
| 215 | |||
| 216 | $number->gte($min, \%opts); | ||
| 217 | |||
| 218 | Adds a rule to ensure the value is greater than or equal to the specified minimum. | ||
| 219 | |||
| 220 | =head2 lt | ||
| 221 | |||
| 222 | $number->lt($max, \%opts); | ||
| 223 | |||
| 224 | Adds a rule to ensure the value is less than the specified maximum. | ||
| 225 | |||
| 226 | =head2 lte | ||
| 227 | |||
| 228 | $number->lte($max, \%opts); | ||
| 229 | |||
| 230 | Adds a rule to ensure the value is less than or equal to the specified maximum. | ||
| 231 | |||
| 232 | =head2 int | ||
| 233 | |||
| 234 | $number->int(\%opts); | ||
| 235 | |||
| 236 | Adds a rule to ensure the value is an integer. | ||
| 237 | |||
| 238 | =head2 positive | ||
| 239 | |||
| 240 | $number->positive(\%opts); | ||
| 241 | |||
| 242 | Adds a rule to ensure the value is a positive number. | ||
| 243 | |||
| 244 | =head2 negative | ||
| 245 | |||
| 246 | $number->negative(\%opts); | ||
| 247 | |||
| 248 | Adds a rule to ensure the value is a negative number. | ||
| 249 | |||
| 250 | =head2 nonpositive | ||
| 251 | |||
| 252 | $number->nonpositive(\%opts); | ||
| 253 | |||
| 254 | Adds a rule to ensure the value is a non-positive number. | ||
| 255 | |||
| 256 | =head2 nonnegative | ||
| 257 | |||
| 258 | $number->nonnegative(\%opts); | ||
| 259 | |||
| 260 | Adds a rule to ensure the value is a non-negative number. | ||
| 261 | |||
| 262 | =head2 multipleOf | ||
| 263 | |||
| 264 | $number->multipleOf($divisor, \%opts); | ||
| 265 | |||
| 266 | Adds a rule to ensure the value is a multiple of the specified divisor. | ||
| 267 | |||
| 268 | =head2 step | ||
| 269 | |||
| 270 | $number->step($divisor, \%opts); | ||
| 271 | |||
| 272 | Synonym for `multipleOf`. | ||
| 273 | |||
| 274 | =head1 LICENSE | ||
| 275 | |||
| 276 | Copyright (C) ytnobody. | ||
| 277 | |||
| 278 | This library is free software; you can redistribute it and/or modify | ||
| 279 | it under the same terms as Perl itself. | ||
| 280 | |||
| 281 | =head1 AUTHOR | ||
| 282 | |||
| 283 | ytnobody E<lt>ytnobody@gmail.comE<gt> | ||
| 284 | |||
| 285 | =cut |