Skip to content

Commit

Permalink
Implements validation rule for countries
Browse files Browse the repository at this point in the history
  • Loading branch information
artstorm committed Jul 17, 2015
1 parent 982e410 commit ead8102
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/app/Http/Validators/UserValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class UserValidator extends Validator
'last_name' => 'string',
'phone' => 'string',
'mobile' => 'string',
'country' => 'country',
'timezone_identifier' => 'timezone'
];

Expand Down
19 changes: 18 additions & 1 deletion api/app/Services/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function __construct(App $app)

$this->registerValidateFloat();
$this->registerValidateUuid();
$this->registerValidateCountry();
}

/**
Expand Down Expand Up @@ -100,6 +101,7 @@ public function messages()
return [
'float' => 'The :attribute must be a float.',
'uuid' => 'The :attribute must be an UUID string.',
'country' => 'The :attribute must be a valid country code.',
];
}

Expand Down Expand Up @@ -195,7 +197,6 @@ public function validateMany()
}

if (!empty(array_filter($errors))) {

// Use merge instead of passing the errors to the MessageBag
// constructor, to preserve nulls
$errorBag = new MessageBag();
Expand Down Expand Up @@ -354,4 +355,20 @@ protected function registerValidateUuid()
return preg_match('/^\{?[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}\}?$/', $value);
});
}

/**
* Register custom validation rule for countries.
*
* @return void
*/
protected function registerValidateCountry()
{
$this->validator->extend('country', function ($attribute, $value, $parameters) {
$countries = $this->app->make('App\Services\Datasets\countries')
->all()
->toArray();

return in_array($value, array_fetch($countries, 'country_code'));
});
}
}
17 changes: 17 additions & 0 deletions api/tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ public function testFailingUuidValidation()
$this->assertStringEndsWith('must be an UUID string.', $this->validator->errors()->get('uuid')[0]);
}

public function testPassingCountryValidation()
{
$data = ['country' => 'SE'];

$this->assertTrue($this->validator->with($data)->passes());
}

public function testFailingCountryValidation()
{
$data = ['country' => 'SWE'];

$this->assertFalse($this->validator->with($data)->passes());

$this->assertStringEndsWith('valid country code.', $this->validator->errors()->get('country')[0]);
}

public function testTestEntityValidator()
{
$validator = $this->app->make('App\Http\Validators\TestEntityValidator');
Expand Down Expand Up @@ -142,6 +158,7 @@ public function rules()
'float' => 'float',
'uuid' => 'uuid',
'multi_word_column_title' => 'string',
'country' => 'country',
];
}
}

0 comments on commit ead8102

Please sign in to comment.