Download the PHP package mizmoz/validate without Composer
On this page you can find all versions of the php package mizmoz/validate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mizmoz/validate
More information about mizmoz/validate
Files in mizmoz/validate
Package validate
Short Description Validation inspired by React Prop Types
License MIT
Homepage https://www.mizmoz.com/
Informations about the package validate
Mizmoz / validate
Validation for PHP 7 that tries to suck less.
We've used a lot of different validation libraries over time and I've yet to be truly happy with any of them.
The main aim for this is to create a validator that can handle complex items, resolve them and also create nice descriptions of themselves. The dream is to create a validator that can handle REST API data, send useful error messages to the user and also a nice description of the endpoint. This will be the face of the Mizmoz API.
Table of Contents
- Getting started
- Composer installation
- Keeping the resources up to date
- Basic validation
- Testing
- Validators
IsArray
IsArrayOf
IsArrayOfShape
IsArrayOfType
IsBoolean
IsDate
IsEmail
IsEmailDisposable
IsFilter
IsInteger
IsNumeric
IsObject
IsOneOf
IsOneOfType
IsReCaptcha
IsRequired
IsSame
IsShape
IsString
Getting started
Composer installation
It's probably worth pointing out the API is really new and very likely to change.
Keeping the resources up to date
If you‘re using the IsEmailDisposable validator you‘ll want to make sure you‘re using an up to date list of disposable email host names.
Best practice is you create a cron job that executes the update below.
See the resources folder for an example cron file that should be placed in /etc/cron.d.
Basic validation
Resolving a validator to a new value
More complex and useful examples
Validate ACL
Whilst there are no validators for ACL (they might come later). There are exceptions that can be returned or thrown in your application. This way you can use a standard set of Exceptions to handle ACL failures. For example we use validation on our API and will catch the AclException to display an error message.
Testing
Any validator or resolver can be mocked using the ValidatorFactory::mock()
method.
The mocking is pretty simple and just allows for the return Result object to be set by calling methods on the mock object.
Getting the parameters passed to the Mocked object
Validators
IsArray
Check the value is an array
IsArrayOf
Check the array contains only the provided values. Useful for checking enums that are allowed multiple values. For only 1 value set IsOneOf
IsArrayOfShape
Same as IsShape except the $value must be an array.
IsArrayOfType
Check the array contains only items of the particular type
Checking against a single type
Checking against multiple types
IsBoolean
Check the value is boolean-ish, that is 1, '1', true, 'true' & 0, '0', false, 'false'.
IsDate
Check the value is a valid date in the format provided.
$options
string format
- the date format the value is expected to be inbool setValueToDateTime
- set the value from the result to the value when the date is valid.bool strict
- using strict will force empty strings to fail
IsEmailDisposable
Check if the value is a disposable email address like guerillamail.com etc
IsEmail
Check if the value is a valid email address
Don‘t allow disposable email addreses
IsFilter
The filter is a pretty cool helper for parsing strings for filtering.
Basic hash tags with example usage
We use the filter to map to column names for things like statuses etc. Only @tag
& #tag
are
supported anything else will be returned in the filter key as plain text
Special :isInteger
tagging
The filter value has any tags removed
Default tags when no tags are present using the *
Defaults are for the defined group so you can have other tags without defaults
IsInteger
Check the value is an integer
bool $strict
- set to strict to only allow integers and not number strings or floats.
IsNumeric
Check the value is a number
IsObject
IsOneOf
IsOneOfType
IsReCaptcha
Validate a reCAPTCHA response
IsRequired
IsSame
IsShape
Check the value is a particular shape, sometimes is easier to explain with an example...
Shapes can be nested to validate shapes of shapes.
Using the Validate::set()
provides a helper to return nice descriptions of the shape.
IsString
Check the value is a string
Road map
On the todo list
- Formalise the API
- Optional descriptions in OpenAPI format: https://github.com/OAI/OpenAPI-Specification
- Create validators as ReactJS components. Parse the description from Chain to form components.
- Add docs for all remaining validators... there are quite a few more than listed here so be sure to have a look in the src/Validator directory.
- Add more validators!
Tasks
- Create description for isOneOfType
General
Allow positive or negative matching. Possibly like this:
Validators
IsPassword
Check a string matches the requirements for the password.
- Minimum length
- Uppercase characters
- Lowercase characters
- Special characters
- Numbers
Resolvers
ToHash
Create a hash of the given data with various techniques. MD5, SHA1, password_hash etc.
Generate API releases
At Mizmoz we already use the validator chain to generate our API schema in JSON.
The next step would be to create new versions or releases by comparing the new and old API endpoints. This would allow us to create discrete versions for each change and also help with documentation by highlighting each of the changes. We could even go further and highlight breaking changes to the API when defaults etc are changed.
All versions of validate with dependencies
ext-intl Version >=1.1
ext-json Version >=1.2
symfony/console Version ^7.0
guzzlehttp/guzzle Version ^7.9