-
Notifications
You must be signed in to change notification settings - Fork 1
Analysis
tamtakoe edited this page Jul 15, 2016
·
28 revisions
- Allowes empty values: undefined, null, '' (also string with spaces only), [], {}; you can change this behaviour
- There is
presence
validator to disallow empty values -
numericality
validator by default converts strings to number - Custom error messages
- Multiple error formats
- You can not set guaranteed order of validations
//grouped (default)
{
"username": ["Username can't be blank"],
"password": ["Password must be at least 6 characters"]
}
//flat
["Username 'nicklas' is not allowed", "Password must be at least 6 characters"]
//detailed
{
"attribute": "username",
"value": "nicklas",
"validator": "exclusion",
"globalOptions": {
"format": "detailed"
},
"attributes": {
"username": "nicklas",
"password": "bad"
},
"options": {
"within": ["nicklas"],
"message": "'%{value}' is not allowed"
},
"error": "Username 'nicklas' is not allowed"
},
{...}
}
https://github.com/DockYard/ember-validations
- Allowes empty if
allowBlank
for each validators is enabled. Blank values are false - Presence validates the property has a value that is not null, undefined, or ''
http://guides.rubyonrails.org/active_record_validations.html
- Allowes empty if
allow_blank
,allow_nil
are enabled. Blank values are false, '', ' ' (whitespace string), nil, [], and {}. http://api.rubyonrails.org/v4.2/classes/Object.html#method-i-blank-3F - Conditional Validation
validates :card_number, presence: true, if:/unless: :paid_with_card?
- Custom messages
- You can set order of validations http://guides.rubyonrails.org/v3.2.9/active_record_validations_callbacks.html #6.2
person.errors.messages
# => {:name=>["can't be blank", "is too short (minimum is 3 characters)"]}
person.errors[:name]
# => ["can't be blank", "is too short (minimum is 3 characters)"]
http://framework.zend.com/manual/current/en/index.html#zend-validator
- Zend\Validator\NotEmpty validator (you can change behaviour http://framework.zend.com/manual/current/en/modules/zend.validator.not-empty.html#zend-validator-notempty-types)
- Converts string to number
- Translating messages $validator->setTranslator($translate);
-
$input->setAllowEmpty(true)
to skip validators if value is empty. You can't allow empty values for validators. http://stackoverflow.com/a/14915485, https://github.com/zendframework/zend-inputfilter/blob/master/src/Input.php#L387 - You can specify validators order using array http://framework.zend.com/manual/current/en/modules/zend.validator.validator-chains.html
- Has InputFilter which can filter and/or validate sets of data (it's like transformer) http://framework.zend.com/manual/current/en/modules/zend.input-filter.intro.html
- You can set order of validations http://framework.zend.com/manual/current/en/modules/zend.validator.validator-chains.html
// $validator->isValid($value);
true/false
// $validator->getMessages()
["stringLengthTooShort" => "The string 'word' is too short; it must be at least 8 characters"]
http://symfony.com/doc/current/book/validation.html
- About empty values
false === $value || (empty($value) && '0' != $value) // here [] is blank
https://github.com/symfony/validator/blob/master/Constraints/NotBlankValidator.php
'' !== $value && null !== $value // here [] is not blank...
https://github.com/symfony/validator/blob/master/Constraints/BlankValidator.php
null === $value || '' === $value
https://github.com/symfony/validator/blob/master/Constraints/LengthValidator.php
null === $value || '' === $value
https://github.com/symfony/validator/blob/master/Constraints/DateValidator.php
null === $value
https://github.com/symfony/validator/blob/master/Constraints/AbstractComparisonValidator.php
- You can set order of validations (not by default) https://github.com/symfony/symfony/issues/3112
// http://api.symfony.com/3.1/Symfony/Component/Validator/ConstraintViolation.html
// $errors = $validator->validate($author);
// (string) $errors; //for debugging
'The violation message'
// $errors
[{
message: 'The violation message',
messageTemplate: 'The raw violation message',
parameters: 'The parameters to substitute in the raw violation message',
root: 'The value originally passed to the validator',
propertyPath: 'The property path from the root value to the invalid value',
invalidValue: 'The invalid value that caused this violation',
plural: 'The number for determining the plural form when translating the message',
code: 'The error code of the violation',
constraint: 'The constraint whose validation caused the violation',
cause: 'The cause of the violation'
}, {...}]
http://docs.jboss.org/hibernate/beanvalidation/spec/1.1/api/
- null element is valid for many validators
- You can set order of validations by using validation groups and group sequences.
//returns
{
value: 'value',
message: 'message',
groups: {},
payload: {}
}
//returns [] in .list validators
[{...}, ...]
Docs: https://developer.github.com/v3/#client-errors
HTTP/1.1 422 Unprocessable Entity
Content-Length: 149
{
"message": "Validation Failed",
"errors": [
{
"resource": "Issue",
"field": "title",
"code": "missing_field"
}
]
}
https://developers.google.com/drive/v3/web/handle-errors
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}