Skip to content

Commit d0d6771

Browse files
Mark Salmonjasonmccreary
authored andcommitted
Add email and password validation rules
1 parent 2126dbc commit d0d6771

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/Translators/Rules.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Blueprint\Translators;
44

55
use Blueprint\Column;
6+
use Illuminate\Support\Str;
67

78
class Rules
89
{
@@ -20,15 +21,35 @@ public static function fromColumn(Column $column)
2021

2122
// hack for tests...
2223
if (in_array($column->dataType(), ['string', 'char', 'text', 'longText'])) {
23-
$rules = array_merge($rules, ['string']);
24+
$rules = array_merge($rules, [self::overrideStringRuleForSpecialNames($column->name())]);
2425
}
2526

27+
2628
if ($column->attributes()) {
2729
if (in_array($column->dataType(), ['string', 'char'])) {
2830
$rules = array_merge($rules, ['max:' . implode($column->attributes())]);
2931
}
3032
}
3133

34+
// if () {
35+
// $rules = array_merge($rules, ['email']);
36+
// }
37+
3238
return $rules;
3339
}
34-
}
40+
41+
private static function overrideStringRuleForSpecialNames($name)
42+
{
43+
switch ($name) {
44+
case Str::startsWith($name, 'email'):
45+
return 'email';
46+
break;
47+
case $name === 'password':
48+
return 'password';
49+
break;
50+
default:
51+
return 'string';
52+
break;
53+
}
54+
}
55+
}

tests/Feature/Translators/RulesTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ public function forColumn_returns_max_rule_for_string_attributes()
4343
$this->assertContains('max:10', Rules::fromColumn($column));
4444
}
4545

46+
/**
47+
* @test
48+
* @dataProvider stringDataTypesProvider
49+
*/
50+
public function forColumn_overrides_string_rule_with_email_rule_for_attributes_named_email_or_email_address($data_type)
51+
{
52+
$column = new Column('email', $data_type);
53+
54+
$this->assertContains('email', Rules::fromColumn($column));
55+
$this->assertNotContains('string', Rules::fromColumn($column));
56+
57+
$column = new Column('email_address', $data_type);
58+
59+
$this->assertContains('email', Rules::fromColumn($column));
60+
$this->assertNotContains('string', Rules::fromColumn($column));
61+
}
62+
63+
/**
64+
* @test
65+
* @dataProvider stringDataTypesProvider
66+
*/
67+
public function forColumn_overrides_string_rule_with_password_rule_for_attributes_named_password($data_type)
68+
{
69+
$column = new Column('password', $data_type);
70+
71+
$this->assertContains('password', Rules::fromColumn($column));
72+
$this->assertNotContains('string', Rules::fromColumn($column));
73+
}
74+
4675
public function stringDataTypesProvider()
4776
{
4877
return [
@@ -51,5 +80,4 @@ public function stringDataTypesProvider()
5180
['text']
5281
];
5382
}
54-
55-
}
83+
}

0 commit comments

Comments
 (0)