validators

Validators for browsers and Node.js

========================== Validators |travis_status| ==========================

.. |travis_status| image:: https://secure.travis-ci.org/insin/validators.png :target: http://travis-ci.org/insin/validators

Validators which can be shared between browsers and Node.js, based on Django's core validators.

Validators are Functions which take a value and throw a ValidationError if they detect that it is invalid.

Browsers:

  • validators.js_ / validators.min.js_

Node.js::

npm install validators

.. _Node.js: http://nodejs.org/ .. _Django: https://www.djangoproject.com/ .. _validators.js: https://raw.github.com/insin/validators/master/validators.js .. _validators.min.js: https://raw.github.com/insin/validators/master/validators.min.js

Error

Validation error constructor.

For customisation purposes, in addition to a validation message, a ValidationError may specify a code to itentify the category of error and any params which were inserted into the validation message, when applicable.

For messages, you can pass:

  • a single message as a String
  • a list of messages
  • an object containing field-specific messages, with properties corresponding to field names.

Utilities

Defines input values for which a field is considered to be empty. These are:

  • null
  • undefined
  • ''

Validators

Creates a validator which validates that input matches a regular expression.

Options which can be passed are:

  • regex -- the regular expression pattern to search for the provided value, or a pre-compiled RegExp. By default, matches any string (including an empty string)
  • message -- the error message used by ValidationError if validation fails. Defaults to "Enter a valid value".
  • code -- the error code used by ValidationError if validation fails. Defaults to "invalid".
  • inverseMatch -- the match mode for regex. Defaults to false.

Creates a validator which validates that input looks like a valid URL.

Options which can be passed are:

  • schemes -- allowed URL schemes. Defaults to ['http', 'https', 'ftp', 'ftps'].

Creates a validator which validates that input looks like a valid e-mail address.

Options which can be passed are:

  • message -- error message to be used in any generated ValidationError.
  • code -- error code to be used in any generated ValidationError.
  • whitelist -- a whitelist of domains which are allowed to be the only thing to the right of the @ in a valid email address -- defaults to ['localhost'].

Validates that input looks like a valid e-mail address -- this is a preconfigured instance of an EmailValidator,

Validates that input is a valid slug.

Validates that input is a valid IPv4 address.

Validates that input is a valid IPv6 address.

Validates that input is a valid IPv4 or IPv6 address.

Returns the appropriate validator for validating IPv4 or IPv6 addresses, passing 'ipv4', 'ipv6', or 'both' as protocol.

If a truthy unpackIPv4 argument is given and protocol is not 'both', an Error will be thrown.

Validates that input is a comma-separated list of integers.

Base constructor for validators which compare input against a given value.

Validates that input is less than or equal to a given value.

Validates that input is greater than or equal to a given value.

Validates that input is at least a given length.

Validates that input is at most a given length.

Additional IPv6 Functions

Returns true if input is a valid IPv6 address, false otherwise.

Cleans an IPv6 address string -- replaces the longest continious zero-sequence with '::' and removes leading zeroes and makes sure all hextets are lowercase.

If an invalid address is passed, a ValidationError is thrown.

MIT License

Copyright (c) 2014, Jonathan Buchanan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.