Skip to content

Latest commit

 

History

History
215 lines (143 loc) · 6.51 KB

README.rst

File metadata and controls

215 lines (143 loc) · 6.51 KB

Validators travis_status

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

Validators are either Functions or Objects with a __call__() function, which take a value and throw a ValidationError if they detect that it is invalid.

The __call__() form is a workaround for the fact that only Function objects are callable in JavaScript.

Browsers:

Node.js:

npm install validators

Error

ValidationError(messages[, {code: null, params: null}])

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.

Utilities

EMPTY_VALUES

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

  • null
  • undefined
  • ''

isEmptyValue(value)

Convenience function for checking if a value is strictly one of EMPTY_VALUES.

isCallable(obj)

Returns true if the given object is a Function or an Object with a __call__() Function.

callValidator(validator, value)

Calls a validator with a value - this utility function is provided to ensure code which needs to work with validators doesn't need to care how they're defined.

Validators

RegexValidator(options)

Creates a validator which walidates 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.

URLValidator(options)

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

EmailValidator(options)

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

validateEmail(value)

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

validateSlug(value)

Validates that input is a valid slug.

validateIPv4Address(value)

Validates that input is a valid IPv4 address.

validateIPv6Address(value)

Validates that input is a valid IPv6 address.

validateIPv46Address(value) ------------------------------

Validates that input is a valid IPv4 or IPv6 address.

ipAddressValidators(protocol[, unpackIPv4])

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.

validateCommaSeparatedIntegerList(value)

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

BaseValidator(limitValue)

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

MaxValueValidator(limitValue)

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

MinValueValidator(limitValue)

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

MaxLengthValidator(limitValue)

Validates that input is at least a given length.

MinLengthValidator(limitValue)

Validates that input is at most a given length.

Additional IPv6 Functions

ipv6.isValidIPv6Address(value)

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

ipv6.cleanIPv6Address(value[, {errorMessage: '...', unpackIPv4: false}])

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.