Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method defaultValidation() to WriteValidator.php #29

Open
RyanNerd opened this issue Jun 17, 2021 · 0 comments
Open

Add method defaultValidation() to WriteValidator.php #29

RyanNerd opened this issue Jun 17, 2021 · 0 comments

Comments

@RyanNerd
Copy link
Owner

    protected const ALLOWED_CHARACTERS = [' ', "'", '#', '(', ')', '-', '*', '/', '_', '\\', '.']; // Expand this default?

    /**
     * Default processValidation() for generic validations 
     * @param ResponseBody $responseBody
     * @param array $parsedRequest
     */
    protected function defaultValidation(ResponseBody $responseBody, array $parsedRequest): void {
        // Iterate all the model fields
        foreach(Medicine::FIELDS as $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 or other "strange" characters -- this prevents SQL Errors
                if ($dataType === 'string') {
                    if (!V::alnum(implode(self::ALLOWED_CHARACTERS))->validate($parsedRequest[$field])) {
                        $responseBody->registerParam('invalid', $field, 'alpha-numeric. Value given: ' . $parsedRequest[$field]);
                    }
                }
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant