Skip to content

Commit

Permalink
Added a new validator to check if country code is in the format ISO-3…
Browse files Browse the repository at this point in the history
…166-1 ALPHA-2.
  • Loading branch information
Luís Nóbrega committed Nov 29, 2019
1 parent 73689e2 commit 4541cd1
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.0.2] - 2019-11-29
### Added
- Published on Packagist. Added to README.md how to install it using composer.

## [0.0.3] - 2019-11-29
### Added
- Added a new validator to check if country code is in the format ISO-3166-1 ALPHA-2.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

This is a package who validates if a Resume is in JSON Resume format. It also validates if any desired properties are not empty.

The validations that makes are, if:
The validations that makes are:

- It complies with JSON Schema of JSON Resume.
- It has certain fields filled in. This is configurable.
- If complies with JSON Schema of JSON Resume.
- If has certain fields filled in. This is configurable.
- If the country code (basics.countryCode) is in ISO-3166-1 ALPHA-2 format. If this field is empty, is not validated.

## Installing

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lfbn/json-resume-validator",
"description": "Validate your JSON Resume",
"version": "0.0.2",
"version": "0.0.3",
"keywords": [
"JSON Resume",
"JSON Schema"
Expand All @@ -16,7 +16,8 @@
"require": {
"php": "^7.2",
"justinrainbow/json-schema": "^5.2",
"adbario/php-dot-notation": "^2.2"
"adbario/php-dot-notation": "^2.2",
"league/iso3166": "^3.0@dev"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand Down
59 changes: 57 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/Exception/Validation/InvalidCountryCodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Lfbn\JsonResumeValidator\Exception\Validation;

use Exception;

/**
* Class InvalidCountryCodeException
* @package Lfbn\JsonResumeValidator\Exception
*/
class InvalidCountryCodeException extends Exception
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;

/**
* Class FileHasMandatoryFieldsMissingException
* Class MandatoryFieldsMissingException
* @package Lfbn\JsonResumeValidator\Exception
*/
class MandatoryFieldsMissingException extends Exception
Expand Down
1 change: 0 additions & 1 deletion src/Exception/Validation/SchemaViolationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Lfbn\JsonResumeValidator\Exception\Validation;

use Exception;
use Throwable;

/**
* Class SchemaViolationException
Expand Down
5 changes: 5 additions & 0 deletions src/JsonResumeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lfbn\JsonResumeValidator\Exception\FileDoesNotExistsException;
use Lfbn\JsonResumeValidator\Exception\FileIsNotAValidJsonException;
use Lfbn\JsonResumeValidator\Exception\Validation\InvalidCountryCodeException;
use Lfbn\JsonResumeValidator\Exception\Validation\MandatoryFieldsMissingException;
use Lfbn\JsonResumeValidator\Exception\Validation\SchemaViolationException;
use Lfbn\JsonResumeValidator\Validator\ValidationJsonResumeChain;
Expand Down Expand Up @@ -32,6 +33,9 @@ private function __construct(array $jsonResume, array $config = [])
}

/**
* Allows you to get a JsonResumeValidator instance. With this you can validate your JSON Resume. Notice that
* if the file failed to be read then, it throws one of the exceptions below.
*
* @param string $jsonResumeFilePathOrUrl
* @param array $config
* @return JsonResumeValidator
Expand Down Expand Up @@ -75,6 +79,7 @@ private static function isJson(string $string): bool
* @return bool
* @throws MandatoryFieldsMissingException
* @throws SchemaViolationException
* @throws InvalidCountryCodeException
*/
public function isValid(): bool
{
Expand Down
41 changes: 41 additions & 0 deletions src/Validator/Handler/CountryCodeValidationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Lfbn\JsonResumeValidator\Validator\Handler;

use Adbar\Dot;
use Exception;
use League\ISO3166\ISO3166;
use Lfbn\JsonResumeValidator\Exception\Validation\InvalidCountryCodeException;

/**
* Class CountryCodeValidationHandler
* @package Lfbn\JsonResumeValidator\Validator\Handler
*/
class CountryCodeValidationHandler extends AbstractValidationHandler
{

/**
* @param array $jsonResume
* @return bool
* @throws InvalidCountryCodeException
*/
public function handle(array $jsonResume): bool
{
$dot = new Dot($jsonResume);
$countryCode = $dot->get('basics.location.countryCode');

if (empty($countryCode)) {
return true;
}

try {
(new ISO3166())->alpha2($countryCode);
} catch (Exception $e) {
throw new InvalidCountryCodeException(
"The country code ({$countryCode}) is not compliant."
);
}

return parent::handle($jsonResume);
}
}
8 changes: 6 additions & 2 deletions src/Validator/ValidationJsonResumeChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Lfbn\JsonResumeValidator\Validator;

use Lfbn\JsonResumeValidator\Exception\Validation\InvalidCountryCodeException;
use Lfbn\JsonResumeValidator\Exception\Validation\SchemaViolationException;
use Lfbn\JsonResumeValidator\Exception\Validation\MandatoryFieldsMissingException;
use Lfbn\JsonResumeValidator\Validator\Handler\CountryCodeValidationHandler;
use Lfbn\JsonResumeValidator\Validator\Handler\MandatoryFieldsValidationHandler;
use Lfbn\JsonResumeValidator\Validator\Handler\SchemaValidationHandler;
use Lfbn\JsonResumeValidator\Validator\Handler\ValidUrlsValidationHandler;

/**
* Class ValidationJsonResumeChain
Expand All @@ -20,16 +21,19 @@ class ValidationJsonResumeChain
* @return bool
* @throws SchemaViolationException
* @throws MandatoryFieldsMissingException
* @throws InvalidCountryCodeException
*/
public function processRequest(array $jsonResume, array $config): bool
{
$schemaValidation = new SchemaValidationHandler($config);
$mandatoryFields = new MandatoryFieldsValidationHandler($config);
$countryCode = new CountryCodeValidationHandler($config);

$schemaValidation->setNext($mandatoryFields);
$schemaValidation->setNext($mandatoryFields)->setNext($countryCode);

$schemaValidation->handle($jsonResume);
$mandatoryFields->handle($jsonResume);
$countryCode->handle($jsonResume);

return true;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/InvalidCountryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Lfbn\JsonResumeValidator\Tests\Integration;

use Lfbn\JsonResumeValidator\Exception\FileDoesNotExistsException;
use Lfbn\JsonResumeValidator\Exception\FileIsNotAValidJsonException;
use Lfbn\JsonResumeValidator\Exception\Validation\InvalidCountryCodeException;
use Lfbn\JsonResumeValidator\Exception\Validation\MandatoryFieldsMissingException;
use Lfbn\JsonResumeValidator\Exception\Validation\SchemaViolationException;
use Lfbn\JsonResumeValidator\JsonResumeValidator;

/**
* Class InvalidCountryTest
* @package Lfbn\JsonResumeValidator\Tests\Integration
*/
class InvalidCountryTest extends BaseTest
{

/**
* @throws FileDoesNotExistsException
* @throws FileIsNotAValidJsonException
* @throws MandatoryFieldsMissingException
* @throws SchemaViolationException
* @throws InvalidCountryCodeException
*/
public function testItShouldValidateIfTheCountryIsValid(): void
{
$this->expectException(InvalidCountryCodeException::class);

$resume = JsonResumeValidator::load(
$this->getDataFullPath('invalid/invalid-country.json')
);

$this->assertTrue($resume->isValid());
}
}
Loading

0 comments on commit 4541cd1

Please sign in to comment.