Skip to content

Commit

Permalink
fix unit tests error for validator
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 13, 2020
1 parent c71918a commit d966759
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/validator/src/Annotation/Parser/ValidateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function parse(int $type, $annotationObject): array
$validator = $annotationObject->getValidator();
$fields = $annotationObject->getFields();
$params = $annotationObject->getParams();
$type = $annotationObject->getType();
$dataType = $annotationObject->getType();
$unfields = $annotationObject->getUnfields();

ValidateRegister::registerValidate(
Expand All @@ -46,7 +46,7 @@ public function parse(int $type, $annotationObject): array
$unfields,
$params,
'',
$type
$dataType
);

return [];
Expand Down
9 changes: 5 additions & 4 deletions src/validator/src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,25 @@ protected function doValidate(
protected function validateDefaultValidator(array $data, array $validator, array $fields, array $unfields): array
{
$properties = $validator['properties'] ?? [];

foreach ($properties as $propName => $property) {
/* @var IsString|IsInt|IsBool|IsFloat $type */
$type = $property['type']['annotation'] ?? null;
if ($type === null) {
continue;
}

$propName = $type->getName() ?: $propName;
if ($fields && !in_array($propName, $fields, true)) {
continue;
}

if (!isset($data[$propName]) && !$property['required'] && !isset($property['type']['default'])) {
// Un-fields - exclude validate
if (in_array($propName, $unfields, true)) {
continue;
}

// Unfields
if (in_array($propName, $unfields, true)) {
$propName = $type->getName() ?: $propName;
if (!isset($data[$propName]) && !$property['required'] && !isset($property['type']['default'])) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions src/validator/test/unit/ValidatorTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ public function testDefault(): void
public function testName(): void
{
$data = [];
[$result] = (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testName'));
$this->assertEquals($result, ['swoftName' => 'swoft']);
$validates = $this->getValidates(ValidateDemo::class, 'testName');
[$result] = (new Validator())->validateRequest($data, $validates);
$this->assertEquals(['swoftName' => 'swoft'], $result);
}

/**
Expand Down

0 comments on commit d966759

Please sign in to comment.