Git Product home page Git Product logo

Comments (8)

weierophinney avatar weierophinney commented on May 30, 2024

zend-filter is not a validation library. It's purpose is to transform data and/or normalize data.

The proper place to do checks like this is within a validator.

If you are using this particular filter with a data set used within zend-inputfilter, what I'd do instead is use the validator Zend\Validator\Digits to ensure every value within the string is a numeric digit.


Originally posted by @weierophinney at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

Thank you @weierophinney ,
We do want data normalization. If we have a string which contains "00", we do want to get a null int. What we do not want, however, is that even strings which are not a valid way to represent 0, such as "four", end up quietly converted to 0. If filter() returns 0 for such cases, we expect the call to trigger a warning.

Note that while I requested a warning to be triggered, I would find it a proper solution too to just refuse to guess a result and - for example - throw an exception. To phrase this differently, "don't quietly take a guess".


Originally posted by @Chealer at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

@Chealer
The filter uses PHP's normal behaviour for conversion from string to numbers:

If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).

http://php.net/manual/en/language.types.string.php#language.types.string.conversion

If your use case doesn't allow this conversion, than add a validator in front.


Originally posted by @froschdesign at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

@Chealer Another route is to create a custom filter, if the current one does not implement the behavior you require.

Typically, we do not want our filters to raise exceptions. If they cannot filter the value, they should return the original value verbatim. So, in this case, I'd write a filter that checks to ensure the value is not malformed, and, if not, filters it, but otherwise returns it verbatim. Then use a validator to see if the value is valid for the domain.

Alternately, if you really want the exception and/or error, trigger it.

But we do not plan to implement such a feature in zend-filter, for the reasons stated above.


Originally posted by @weierophinney at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

@froschdesign , I read ToInt's code and understand why it happens. If your point is that PHP itself already has a problematic behavior, I do not disagree.

It's not really that a specific use case doesn't allow such conversion. We use Zend filters to normalize inputs throughout the application. The designers must not have realized this problem, but this "filtering" is often quietly altering input and complicating debugging.


Originally posted by @Chealer at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

Thank you @weierophinney ,
I understand that we can work around this, but this requires some effort, and our philosophy leads us to contribute to our upstreams.

If there is no real solution in sight, I recommend mitigating through documentation.


Originally posted by @Chealer at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

@Chealer We'd gladly accept documentation to cover this scenario. Docs are in the repository itself.


Originally posted by @weierophinney at zendframework/zend-filter#63 (comment)

from laminas-filter.

weierophinney avatar weierophinney commented on May 30, 2024

@Chealer

If your point is that PHP itself already has a problematic behavior…

No, but we should document this behavior for the filter.


Originally posted by @froschdesign at zendframework/zend-filter#63 (comment)

from laminas-filter.

Related Issues (20)

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.