You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?phpdeclare(strict_types=1);
namespaceWillow\Controllers;
usePsr\Http\Message\ResponseInterface;
usePsr\Http\Message\ServerRequestInterfaceasRequest;
usePsr\Http\Server\RequestHandlerInterfaceasRequestHandler;
useRespect\Validation\ValidatorasV;
useWillow\Middleware\ResponseBody;
abstractclass WriteValidatorBase
{
publicfunction__invoke(Request$request, RequestHandler$handler): ResponseInterface
{
$responseBody = $this->processValidation($request->getAttribute('response_body'));
// If there are any missing or required data points then we short circuit and return invalid request.if ($responseBody->hasMissingRequiredOrInvalid()) {
$responseBody = $responseBody
->setStatus(ResponseBody::HTTP_BAD_REQUEST)
->setMessage('Missing or invalid request');
return$responseBody();
}
return$handler->handle($request);
}
/** * You should override this function to perform the validations * @param ResponseBody $responseBody * @return ResponseBody */protectedfunctionprocessValidation(ResponseBody$responseBody): ResponseBody {
return$responseBody;
}
/** * Default processValidation() for generic validations * @param ResponseBody $responseBody * @param array $fields * @return ResponseBody */protectedfunctiondefaultValidation(ResponseBody$responseBody, array$fields): ResponseBody {
$parsedRequest = $responseBody->getParsedRequest();
// Iterate all the model fieldsforeach($fieldsas$field => $dataType) {
$protectedField = $dataType[0] === '*';
// Is the model field NOT in the request?if (!V::key($field)->validate($parsedRequest)) {
// Any dataType proceeded with an * are protected fields and can not be changed (e.g. password_hash)if ($protectedField) {
continue;
}
// If the request is missing this field so register it as optional$responseBody->registerParam('optional', $field, $dataType);
} else {
// If Datatype is proceeded with an * it means the field is protected and can not be changed (e.g. password_hash)if ($protectedField) {
$responseBody->registerParam('invalid', $field, null);
}
// Don't allow emoji characters -- this prevents SQL Errorsif ($dataType === 'string') {
$fieldValue = $parsedRequest[$field];
if (V::notEmpty()->validate($fieldValue) && !V::notEmoji()->validate($fieldValue)) {
$responseBody->registerParam('invalid', $field, 'alpha-numeric. Value given: ' . $fieldValue);
}
}
}
}
return$responseBody;
}
}
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: