Giter Site home page Giter Site logo

filterus's People

Contributors

bocharsky-bw avatar frostbane avatar hostyle avatar ircmaxell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

filterus's Issues

Can I achieve optional keys by using a pool of maps?

I have a validation requirement where an array can look like either of these:

[ 'name' => 'string',
  'from_address' => 'email',
  'from_name' => 'string',
  'subject' => 'string',
  'body_plain' => 'string',
  'to_address' => 'email', ] // Differs just in this key

[ 'name' => 'string',
  'from_address' => 'email',
  'from_name' => 'string',
  'subject' => 'string',
  'body_plain' => 'string',
  'to_address_field' => 'string', ] // Differs just in this key

My strategy is that, since there is no way to specify keys as optional, I could run a map for each one, and accept if either returns true. On that basis, I thought I could use a pool as well.

use Filterus\Filter as F;

$baseValidator = [
    'name' => 'string',
    'from_address' => 'email',
    'from_name' => 'string',
    'subject' => 'string',
    'body_plain' => 'string',
];

// Use a pool of maps, should work?
$eitherValidators = F::pool([
    F::map($baseValidator + ['to_address_field' => 'string', ]),
    F::map($baseValidator + ['to_address' => 'email', ]),
]);

However, this blows up in the Filterus internals:

explode() expects parameter 2 to be string, array given

.../vendor/ircmaxell/filterus/lib/Filterus/Filter.php:98
.../vendor/ircmaxell/filterus/lib/Filterus/Filter.php:52
.../vendor/ircmaxell/filterus/lib/Filterus/Filters/Map.php:21
.../vendor/ircmaxell/filterus/lib/Filterus/Filters/Map.php:45

I'll write a custom validator for now, but would be interested to see how people would solve this.

Possible bug? Invalid outcome of validation

Having an array like

array(19) {
  'sku' =>
  string(2) "SW"
  '_type' =>
  string(6) "simple"
  '_attribute_set' =>
  string(18) "xxxxx"
  '_product_websites' =>
  string(10) "xxxxx"
  'name' =>
  NULL
  'description' =>
  string(8) "TESTTEST"
  'short_description' =>
  string(8) "TESTTEST"
  'price' =>
  int(0)
  'url_key' =>
  NULL
  'cost' =>
  NULL
  'status' =>
  int(1)
  'visibility' =>
  int(4)
  'manufacturer' =>
  string(9) "xxxx"
  'tax_class_id' =>
  int(2)
  'base_price_unit' =>
  string(3) "PCS"
  'base_price_base_unit' =>
  string(3) "PCS"
  'country_of_manufacture' =>
  string(11) "xxxxx"
  'qty' =>
  int(0)
  'is_in_stock' =>
  int(1)
}

And with this filter:

$filter = Filter::map(array(
                'name'              => 'string,min:1', //anything above 1 should do
                'sku'               => 'string,min:3', //prefix is hardcoded, so anything above strlen 2 will do
                '_attribute_set'    => 'string,min:1', //TODO Should be hardcoded to whatever is in $conf
                '_product_websites' => 'string,min:1',
                '_type'             => 'string,min:1', //TODO Either configurable or simple
            ));

I would suspect that $filter->validate($product) would throw back false since "name" is clearly a) not a string and b) is not having a stringlengh of atleast 1.

But I do get true as a result, which is false.

Email validation doesn't work on an empty string

There isn't an implementation for validate for emails, so although an empty string will be correctly filtered to null, it will validate as true, which is not desirable.

Repro code:

$filter = \Filterus\Filter::factory('email');

$result = $filter->filter('');
var_dump($result); // Returns NULL, correct

$ok = $filter->validate('');
echo $ok ? 'Passed' : 'Failed'; // Incorrectly returns Passed

A solution for now is to use a chain, like so:

$filter = \Filterus\Filter::chain('email', 'string,min:1');

$result = $filter->filter('');
var_dump($result); // Returns NULL, correct

$ok = $filter->validate('');
echo $ok ? 'Passed' : 'Failed'; // Correctly returns Failed

String inclusion of scalar values?

$arr = [
  'a' => 1,
  'b' => 2,
  'c' => 3,
  4   => 4,
];

\Filterus\Filter::factory('array, keys:string')->filter($arr);

Doesn't remove 4.

I noticed you've got is_scalar() testing, then a cast to string, which takes place on 4 so that it's '4'. Was just curious why you wouldn't filter int out for alpha when array, keys:int will exclude 'a', 'b' and 'c'.

Shouldn't the string test just be is_string()? Maybe calls for the addition of array, keys:scalar?

What's the best way to allow strings to be null?

Hi, thanks for providing this library. I have the following solution to allow "string or null", and will provide it below. However, if there is a better way to do this, please let me know.

First a null validator:

/**
 * Class to allow nulls
 */

class AllowNull extends \Filterus\Filter
{
    public function filter($var)
    {
        return $var;
    }

    public function validate($var)
    {
        return is_null($var);
    }
}

Then register like so:

use Filterus\Filter as F;

F::registerFilter('null', AllowNull::class);

Finally use in a pool:

$validator = F::pool('string', 'null');

Want some help triaging simple PRs?

Hi @ircmaxell

There's some PRs that have languished for a while in this repo. If you are willing to make me a committer, I'd be happy to go through the trivial docs ones, check their validity, and merge them down if they are OK.

reserved "Object" name

Hi,

I tried running the unit tests under php7.2.12 and it fails in

Filterus\Filters\Object

PHP Fatal error:  Cannot use 'Object' as class name as it is reserved

I am planning to refactor it and submit a pull request, but before I make any changes do you have any ideas on what class name should it be changed to?

packagist.org

hi there, can't see this project on packagist.org, can you put it on. thnx~

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.