In 2016, during the hectic initial phase of our startup, we made a critical mistake that impacted several months of invoice provisions for our sales managers. We mistakenly applied the netToGross tax calculation for sales tax multiple times, even on already gross values, leading to significant errors. This issue went unnoticed for several months, causing considerable trouble. As a result, we developed two separate libraries to ensure valid values across all our platforms.
The first commitment was to ensure valid parameters across the entire projects by creating the Value library. Then, as a logical conclusion, the next step was to implement a standard on how data has to be validated.
// The scenario is real, but this example is much simplified, of course.functioncreateUser(array$request): void {
// validate field by field...
}- Checks input for integrity.
- Returns one or more detailed pieces of information on why an input is problematic.
- Can only be constructed with valid input.
- Once constructed, the input is immutable, ensuring integrity during runtime.
When validating data for integrity, you won't just need the fact that the input is problematic, you need a very detailed information on why the value actually is problematic. So we introduced the validator structure to almost every method that handles user input. We can't just validate numbers. There already is a collection of Validators. Example Validators:
IbanValidator
EmailValidator
DomainValidator
ConnectionValidator
And many more...
Here’s how you can use these Value objects: If you are going to use the Validator stack for user inputs, you can easily do things like these:
<?phpdeclare(strict_types = 1);
namespacenoxkiwi\validator;
usenoxkiwi\validator\Interfaces\ValidatorInterface;
class MyController
{
publicfunctioncheckEmail(string$mailAddress): UserObject {
$errors = EmailValidator::getInstance()->validate($mailAddress);
if ($errors) {
$this->response->send(Http::400, ErrorMessageBuilder::build($errors);
return;
}
return UserTable::findUserForMail($mailAddress);
}
}
class UserTable {
publicstaticfunctionfindUserForMail(string$mailAddress): UserObject
{
// PSEUDO
}
}On the other hand, you can also rely on our Value objects that integrate the Validator stack.
This way, you can ensure that the method findUserForMail will be called with a valid input which needs no further validation.
class MyController
{
publicfunctioncheckEmail(string$mailAddress): UserObject {
try {
$mail = newEmailValue($mail);
return UserTable::findUserForMail($mail);
} catch (\Exception$e) {
$this->response->send(Http::400, ErrorMessageBuilder::build($e));
}
}
}
class UserTable {
publicstaticfunctionfindUserForMail(EmailValue$mail): UserObject
{
//PSEUDO
}
}publicfunction validate(mixed$value, ?array$options = null): array;- Description: Validates the given value and returns an array of errors. If the value is valid, the array will be empty. Optional validation options can be provided.
- Parameters:
mixed $value: The value to be validated.array|null $options: (Optional) An array of options for validation.
- Returns:
array- An array of errors.
publicfunction isValid(mixed$value): bool;- Description: Sends the value to the validate function and returns true if the array of errors is empty.
- Parameters:
mixed $value: The value to be validated.
- Returns:
bool- True if the value is valid, false otherwise.
publicfunction getErrors(): array;- Description: Returns all the errors that exist in the instance.
- Parameters: None.
- Returns:
array- An array of errors.
publicfunction reset(): Validator;- Description: Removes any error from the Errorstack instance.
- Parameters: None.
- Returns:
\noxkiwi\validator\Validator- The Validator instance.
If you're excited about the possibility of working together or simply want to discuss innovative ideas, I'd love to hear from you. Don't hesitate to reach out via email.
Let's create something amazing together!