Skip to content

Analysis

tamtakoe edited this page Jul 15, 2016 · 28 revisions

Validate.js

http://validatejs.org/

  • 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"
  },
  {...}
}

Ember Validations

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 ''

Ruby on Rails

http://guides.rubyonrails.org/active_record_validations.html

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)"]

Zend Framework 2

http://framework.zend.com/manual/current/en/index.html#zend-validator

// $validator->isValid($value);
true/false

// $validator->getMessages()
["stringLengthTooShort" => "The string 'word' is too short; it must be at least 8 characters"]

Simfony 2

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
// 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'
}, {...}]

Bean validation (Java)

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
[{...}, ...]

GitHub API

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"
    }
  ]
}

Google drive API

https://developers.google.com/drive/v3/web/handle-errors

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "badRequest",
        "message": "Bad Request"
      }
    ],
    "code": 400,
    "message": "Bad Request"
  }
}
Clone this wiki locally