validator

Validator - Providing Data Analysis Across Your Project

It Began With a Mistake

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.
function createUser(array $request): void {
    // validate field by field...
}

Validator 💘 Value

Validator

Value

Just an Example

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:

<?php declare(strict_types = 1);

namespace noxkiwi\validator;

use noxkiwi\validator\Interfaces\ValidatorInterface;

class MyController
{
    public function checkEmail(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 {
    public static function findUserForMail(string $mailAddress): UserObject
    {
// PSEUDO
    }
}

Deeper Dive

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
{
    public function checkEmail(string $mailAddress): UserObject {
        try {
           $mail = new EmailValue($mail);
           return UserTable::findUserForMail($mail);
        } catch (\Exception $e) {
            $this->response->send(Http::400, ErrorMessageBuilder::build($e));
        }
    }
}

class UserTable {
    public static function findUserForMail(EmailValue $mail): UserObject
    {
//PSEUDO
    }
}

Interface Methods

validate

public function validate(mixed $value, ?array $options = null): array;

isValid

public function isValid(mixed $value): bool;

getErrors

public function getErrors(): array;

reset

public function reset(): Validator;

Let’s Connect!

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!