Skip to content

Commit

Permalink
add Validator strict model and Required rule (#545)
Browse files Browse the repository at this point in the history
add Validator strict model and Required rule
  • Loading branch information
inhere authored Jan 17, 2020
2 parents 5d35972 + a3bdbd9 commit a23799a
Show file tree
Hide file tree
Showing 95 changed files with 687 additions and 117 deletions.
40 changes: 40 additions & 0 deletions src/http-message/src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class Request extends PsrRequest implements ServerRequestInterface
*/
private $parsedQuery;

/**
* @var array|null
*/
private $parsedPath;

/**
* @var array
*/
Expand Down Expand Up @@ -367,6 +372,15 @@ public function getParsedQuery(): array
return $this->parsedQuery;
}

/**
* @return array
*/
public function getParsedPath(): array
{
return $this->parsedPath;
}


/**
* @param string $key
* @param null $default
Expand All @@ -380,6 +394,19 @@ public function parsedQuery(string $key, $default = null)
return $parsedQuery[$key] ?? $default;
}

/**
* @param string $key
* @param null $default
*
* @return mixed|null
*/
public function parsedPath(string $key, $default = null)
{
$parsedPath = $this->getParsedPath();

return $parsedPath[$key] ?? $default;
}

/**
* @param array $query
*
Expand All @@ -393,6 +420,19 @@ public function withParsedQuery(array $query): self
return $clone;
}

/**
* @param array $path
*
* @return Request
*/
public function withParsedPath(array $path): self
{
$clone = clone $this;

$clone->parsedPath = $path;
return $clone;
}

/**
* @param string $key
* @param mixed|null $default
Expand Down
5 changes: 3 additions & 2 deletions src/http-server/src/Middleware/ValidatorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

$data = $request->getParsedBody();
$query = $request->getQueryParams();
$path = $route->getParams();

// ParsedBody is empty string
$parsedBody = $data = empty($data) ? [] : $data;
Expand All @@ -64,13 +65,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$validator = BeanFactory::getBean('validator');

/* @var Request $request */
[$parsedBody, $query] = $validator->validateRequest($parsedBody, $validates, $query);
[$parsedBody, $query, $path] = $validator->validateRequest($parsedBody, $validates, $query, $path);

if ($notParsedBody) {
$parsedBody = $data;
}

$request = $request->withParsedBody($parsedBody)->withParsedQuery($query);
$request = $request->withParsedBody($parsedBody)->withParsedQuery($query)->withParsedPath($path);

return $handler->handle($request);
}
Expand Down
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/AfterDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getDate(): string
{
return $this->date;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/AlphaDash.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/AlphaNum.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/BeforeDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getDate(): string
{
return $this->date;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/Chs.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/ChsAlpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/ChsAlphaDash.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/ChsAlphaNum.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getName(): string
{
return $this->name;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ public function getEnd(): string
{
return $this->end;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/Different.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getName(): string
{
return $this->name;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/Dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function getMessage(): string
{
return $this->message;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
10 changes: 9 additions & 1 deletion src/validator/src/Annotation/Mapping/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public function getMessage(): string
{
return $this->message;
}
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
10 changes: 9 additions & 1 deletion src/validator/src/Annotation/Mapping/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public function getValues(): array
{
return $this->values;
}
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getName(): string
{
return $this->name;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/FileMediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getMediaType(): array
{
return $this->mediaType;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/FileSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getSize(): int
{
return $this->size;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/FileSuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getSuffix(): array
{
return $this->suffix;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
8 changes: 8 additions & 0 deletions src/validator/src/Annotation/Mapping/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getName(): string
{
return $this->name;
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
10 changes: 9 additions & 1 deletion src/validator/src/Annotation/Mapping/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public function getMessage(): string
{
return $this->message;
}
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
10 changes: 9 additions & 1 deletion src/validator/src/Annotation/Mapping/IsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function getName(): string
{
return $this->name;
}
}

/**
* @param string $message
*/
public function setMessage(string $message): void
{
$this->message = $message;
}
}
Loading

0 comments on commit a23799a

Please sign in to comment.