Skip to content

Commit 99e432c

Browse files
Mark Salmonjasonmccreary
authored andcommitted
Adds numeric and gt:0 rules
1 parent 8843552 commit 99e432c

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/Translators/Rules.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ public static function fromColumn(Column $column)
2424
$rules = array_merge($rules, [self::overrideStringRuleForSpecialNames($column->name())]);
2525
}
2626

27+
if (in_array($column->dataType(), [
28+
'integer',
29+
'tinyInteger',
30+
'smallInteger',
31+
'mediumInteger',
32+
'bigInteger',
33+
'decimal',
34+
'double',
35+
'float',
36+
'increments',
37+
'tinyIncrements',
38+
'smallIncrements',
39+
'mediumIncrements',
40+
'bigIncrements',
41+
'unsignedBigInteger',
42+
'unsignedDecimal',
43+
'unsignedInteger',
44+
'unsignedMediumInteger',
45+
'unsignedSmallInteger',
46+
'unsignedTinyInteger'
47+
])) {
48+
$rules = array_merge($rules, ['numeric']);
49+
50+
if (Str::startsWith($column->dataType(), 'unsigned')) {
51+
$rules = array_merge($rules, ['gt:0']);
52+
}
53+
}
54+
2755

2856
if ($column->attributes()) {
2957
if (in_array($column->dataType(), ['string', 'char'])) {

tests/Feature/Translators/RulesTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ public function forColumn_overrides_string_rule_with_password_rule_for_attribute
7272
$this->assertNotContains('string', Rules::fromColumn($column));
7373
}
7474

75+
/**
76+
* @test
77+
* @dataProvider numericDataTypesProvider
78+
*/
79+
public function forColumn_returns_numeric_rule_for_numeric_types($data_type)
80+
{
81+
$column = new Column('test', $data_type);
82+
83+
$this->assertContains('numeric', Rules::fromColumn($column));
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function forColumn_returns_gt0_rule_for_unsigned_numeric_types()
90+
{
91+
$column = new Column('test', 'integer');
92+
93+
$this->assertContains('numeric', Rules::fromColumn($column));
94+
$this->assertNotContains('gt:0', Rules::fromColumn($column));
95+
96+
$column = new Column('test', 'unsignedInteger');
97+
98+
$this->assertContains('gt:0', Rules::fromColumn($column));
99+
$this->assertContains('numeric', Rules::fromColumn($column));
100+
}
101+
75102
public function stringDataTypesProvider()
76103
{
77104
return [
@@ -80,4 +107,29 @@ public function stringDataTypesProvider()
80107
['text']
81108
];
82109
}
110+
111+
public function numericDataTypesProvider()
112+
{
113+
return [
114+
['integer'],
115+
['tinyInteger'],
116+
['smallInteger'],
117+
['mediumInteger'],
118+
['bigInteger'],
119+
['decimal'],
120+
['double'],
121+
['float'],
122+
['increments'],
123+
['tinyIncrements'],
124+
['smallIncrements'],
125+
['mediumIncrements'],
126+
['bigIncrements'],
127+
['unsignedBigInteger'],
128+
['unsignedDecimal'],
129+
['unsignedInteger'],
130+
['unsignedMediumInteger'],
131+
['unsignedSmallInteger'],
132+
['unsignedTinyInteger'],
133+
];
134+
}
83135
}

0 commit comments

Comments
 (0)