Giter Club home page Giter Club logo

redbox-validator's Introduction

Redbox-valiation

Redbox-validation is a simple to use validation class. The implementation is based on Laravel's (but no a rewrite of) validation package but has no external when used in your project.

Packagist Unit Tests PhpCS PhpCS

Installation

$ composer require redbox/validation

Usage

The package is flexible to use with an easy syntax.

Basic Usage

use Redbox\Validation\Validator;

$validator = new Validator([
    'foo' => 'The quick brown fox jumps over the lazy dog'
]);

$data = $validator->validate([
    'food' => 'string'
]);

/**
 * Validator::passes() is also
 * available.
 */
if ($validator->fails()) {
    /**
     * Handle errors use $validator->errors()
     * to get the errors.
     */
}

Using closures

You can use your own closures to add custom rules

use Redbox\Validation\ValidationContext;
use Redbox\Validation\Validator;

$validator = new Validator([
    'foo' => 'The quick brown fox jumps over the lazy dog'
]);

$data = $validator->validate([
    /**
     * This will validate field 'foo' equals 'baz' in this case it will not so
     * the validation will fail and an error will be set for field 'foo'.
     */
    'foo' => function (ValidationContext $context): bool {

        return ($context->keyExists() &&  $context->getValue() === 'baz')
            or $context->addError("Field {$context->getKey()} does not equal 'baz'.");
    }
]);

/**
 * Validator::passes() is also
 * available.
 */
if ($validator->fails()) {
    /**
     * Handle errors use $validator->errors()
     * to get the errors.
     */
    print_r($validator->errors());
}

Available Rules

The following validation rules are available with this package.

Type Alias Description
string N/A The field under validation must be of type string.
boolean bool The field under validation must be of type boolean.
integer int The field under validation must be of type integer.

License

The MIT License (MIT)

Copyright (c) 2023 Johnny Mast

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

redbox-validator's People

Contributors

github-actions[bot] avatar johnnymast avatar

Watchers

 avatar

redbox-validator's Issues

Feature: Type parameters

Is your feature request related to a problem? Please describe.
I would like to see an option for types to receive parameters from the definition. It may look something like this:

$validator->validate([
    'foo' => 'length:min:6:max:12'
]);

Describe the solution you'd like
This needs to be implemented in the type context class.

add type 'float' as a wrapper around is_float

Feature description

For the next upcoming version, I would like to see the type 'float' implemented. The 'is_float' type will be a wrapper around the yy core function in PHP.

You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

Where will this feature be implemented

Before I start explaining how to implement your new feature I want to quickly explain that what you will be implementing is a new type of validation. Now don't worry we have a workflow for new types that make it into Redbox-Validation. Types exist in a so-called type definition class, the functions in this class represent a validation type.

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

You can copy and paste this example and change it for yourself. You will have to change the attribute value to what you will be implemented as a type, it might go unsaid but make sure the function name is unique. If the category of your new type does not match any existing definition class in src/ValidationTypes folder you might add a new class. If you do make sure you add your class to the defineTypes function in src/Validator.php.

https://github.com/johnnymast/redbox-validation/blob/a8667ddb9a9cd4a61d2f9dc8cd866f90077ec14d/src/Validator.php#L70-L77

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the original project and I will take a look at your request.

[IN Progress] add type 'ipv6' as a wrapper around var_filter

For version 0.20 I would like to see the type 'ipv6' implemented. The 'ipv6' type will be a wrapper around the filter_varcore function in PHP. You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

How will you go about implementing this new feature? Fork the project here on Github to your own account and create a new branch on your fork. Implement the new feature in the file.

https://github.com/johnnymast/redbox-validation/blob/v0.20/src/ValidationTypes/InternetTypeDefinitions.php

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the v0.20 branch here on the original project. I will have a look at your implementation and pull in your work if it all works as intended.

[IN Progress] add type 'ipv4' as a wrapper around var_filter

For version 0.20 I would like to see the type 'ipv4' implemented. The 'ipv4' type will be a wrapper around the filter_varcore function in PHP. You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

How will you go about implementing this new feature? Fork the project here on Github to your own account and create a new branch on your fork. Implement the new feature in the file.

https://github.com/johnnymast/redbox-validation/blob/v0.20/src/ValidationTypes/InternetTypeDefinitions.php

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the v0.20 branch here on the original project. I will have a look at your implementation and pull in your work if it all works as intended.

add type 'object' as a wrapper around is_object

For version 0.20 I would like to see the type 'object' implemented. The 'object' type will be a wrapper around the is_object core function in PHP. You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

How will you go about implementing this new feature? Fork the project here on Github to your own account and create a new branch on your fork. Implement the new feature in the file.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the v0.20 branch here on the original project. I will have a look at your implementation and pull in your work if it all works as intended.

add type 'serializeable' as a type

Feature description

For the next upcoming version, I would like to see the type 'serializable' implemented. This type will serialize and unserialize the string. If the strings are the same as the input it is valid JSON.

NOTE: Be aware this could be a potential security risk of winch could lead to code execution if not handled right.

You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

Where will this feature be implemented

Before I start explaining how to implement your new feature I want to quickly explain that what you will be implementing is a new type of validation. Now don't worry we have a workflow for new types that make it into Redbox-Validation. Types exist in a so-called type definition class, the functions in this class represent a validation type.

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

You can copy and paste this example and change it for yourself. You will have to change the attribute value to what you will be implemented as a type, it might go unsaid but make sure the function name is unique. If the category of your new type does not match any existing definition class in src/ValidationTypes folder you might add a new class. If you do make sure you add your class to the defineTypes function in src/Validator.php.

https://github.com/johnnymast/redbox-validation/blob/a8667ddb9a9cd4a61d2f9dc8cd866f90077ec14d/src/Validator.php#L70-L77

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the original project and I will take a look at your request.

add type 'json' as a type

Feature description

For the next upcoming version, I would like to see the type 'json' implemented. This would be testing if encoding and decoding the JSON string with the result in the same string. If this is the case its valid JSON.

You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

Where will this feature be implemented

Before I start explaining how to implement your new feature I want to quickly explain that what you will be implementing is a new type of validation. Now don't worry we have a workflow for new types that make it into Redbox-Validation. Types exist in a so-called type definition class, the functions in this class represent a validation type.

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

You can copy and paste this example and change it for yourself. You will have to change the attribute value to what you will be implemented as a type, it might go unsaid but make sure the function name is unique. If the category of your new type does not match any existing definition class in src/ValidationTypes folder you might add a new class. If you do make sure you add your class to the defineTypes function in src/Validator.php.

https://github.com/johnnymast/redbox-validation/blob/a8667ddb9a9cd4a61d2f9dc8cd866f90077ec14d/src/Validator.php#L70-L77

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the original project and I will take a look at your request.

[DATE]: [FEATURE NAME]


name: Good first issue
about: Request a new feature for Redbox Validator
title: "[DATE]: [FEATURE NAME]"
labels: enhancement, feature request
assignees: johnnymast

What will this feature do?

Describe the feature in detail.

Where will this feature be implemented

Describe where the feature will be implemented.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the v0.20 branch here on the original project. I will have a look at your implementation and pull in your work if it all works as intended.

add type 'numeric' as a wrapper around is_numeric.

For version 0.20 I would like to see the type 'numeric' implemented. The 'numeric' type will be a wrapper around the is_nummeric core function in PHP. You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

How will you go about implementing this new feature? Fork the project here on Github to your own account and create a new branch on your fork. Implement the new feature in the file.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the v0.20 branch here on the original project. I will have a look at your implementation and pull in your work if it all works as intended.

Failed to validate exitance of validation type.

Describe the bug

I used this code knowing well it would not work because we don't support arguments yet but the validator validations on the rules did not catch the unknown type of 'required'.

Here is a screenshot of the causing code:

image

The exception triggered is here.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/Validator.php#L192-L198

To Reproduce
Add a nonexisting validation type in validate type and watch the horror.

Expected behavior
A clear and concise description of what you expected to happen.

Software

  • PHP 8.1
  • Slim 4
  • Nginx

Feature: Custom error messages for types

Is your feature request related to a problem? Please describe.
No this will be a new feature for Redbox-Validation

Describe the solution you'd like
I would like to see an option for developers to add a custom error message for types.

add type 'array' as a wrapper around is_array.

For version 0.20 I would like to see the type 'array' implemented. The 'array' type will be a wrapper around the is_array core function in PHP. You can take as short or as long as you like but all things considered, you won't have to take long (like 15 minutes most likely if you understand how the project works).

How will you go about implementing this new feature? Fork the project here on Github to your own account and create a new branch on your fork. Implement the new feature in the file.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php

Here is the sample implementation of the 'string' type in the code.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/ValidationTypes/TypeDefinitions.php#L30-L35

What to do after your implementation

After you have done this you will have to write a test for it in the feature test directory winch can be found here.

When you are done

So you are done with your implementation and the unit tests? Send me a pull request from your fork to the v0.20 branch here on the original project. I will have a look at your implementation and pull in your work if it all works as intended.

Some how the validator did not throw an ValidationDefinitionException before running the validation

I used this code knowing well it would not work because we don't support arguments yet but the validator validations on the rules did not catch the unknown type of 'required'.

Here is a screenshot of the causing code:

image

The exception triggered is here.

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/Validator.php#L192-L198

What should have happened?

Before executing this (non-existing) type of validation it should have thrown a validation exception at either one of these two places.

Here:

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/Validator.php#L173

or here:

https://github.com/johnnymast/redbox-validation/blob/65f941782eccd967121d865d5fc74dd2bd2b497c/src/Validator.php#L119

What needs to be done?

Check if we validate the given rules correctly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.