ValidString
A handy validation library for strings
Installation
npm install validstring --save
Usage
var validator = validatorisValid// Returns: true
In order to not force you to change your programming style, there are several ways to achieve the same results.
// Chained validationvar validator1 = validatorisValid// Returns: true // Constucted validationvar validator2 = notEmpty: null alphaNumeric: extraChars: ' 'validator2isValid// Returns: true // Built validation// Useful when the validation needs to be defined programmaticallyvar validator3 = validator3isValid// Returns: true // Bi-dimensional build validationvar validator4 = validator4isValid// Returns: true
For simplicity reasons all the examples from now on will be shown using the
chainable style
, as shown invalidator1
Check Validity
var validator = validatorisValid// Returns: false
Assert validity
var validator1 = validator1// Returns: true var validator2 = validator2// Returns: true
Note that: The
validator2
differs fromvalidator1
in the fact that it does not use the.test(str)
method, but instead it uses the.assert(str, bool)
, which will perform the test and assert the result, returning a boolean value.
Get error messages
var validator1 = validatorisValid// Returns: false validator1// Returns ['Your name must contain upper and lower case letters from A to Z only.']
Validations
alphabetic
Is valid if the tested string has only characters from A to Z (or a to z)
Default error message: '%s must contain upper and lower case letters from A to Z only.'
Options
Name | Expected type | Description | Example |
---|---|---|---|
extraChars | string | Extends the range of characters accepted by this validation. | {extraChars: '-_$', ...} |
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
alphaNumeric
Is valid if the tested string has only characters from A to Z (or a to z) and/or from 0 to 9
Default error message: '%s must contain upper and lower case letters from A to Z and numbers only.'
Options
Name | Expected type | Description | Example |
---|---|---|---|
extraChars | string | Extends the range of characters accepted by this validation. | {extraChars: '-_$', ...} |
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
has
Is valid if the tested string contains any of the specified characters.
Default error message: '%s must contain any of the following characters: ...'
Options
Name | Expected type | Description | Example |
---|---|---|---|
chars required |
string | Specifies the characters that should be present in the tested string. | {chars: '-_$', ...} |
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
hasNot
Is valid if the tested string does not contain any of the specified characters.
Default error message: '%s cannot contain any of the following characters: ...'
Options
Name | Expected type | Description | Example |
---|---|---|---|
chars required |
string | Specifies the characters that should not be present in the tested string. | {chars: '-_$', ...} |
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
notEmpty
Is valid if the tested string has 1 or more characters
Default error message: '%s must not be empty.'
Options
Name | Expected type | Description | Example |
---|---|---|---|
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
numeric
Is valid if the tested string has only characters from 0 to 9
Default error message: '%s must contain numbers only.'
Options
Name | Expected type | Description | Example |
---|---|---|---|
extraChars | string | Extends the range of characters accepted by this validation. | {extraChars: '-_$', ...} |
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
regExPattern
Is valid if the tested string based on a given regular expression pattern.
Default error message: '%s is not valid.'
Options
Name | Expected type | Description | Example |
---|---|---|---|
pattern required |
RegExp | Sets the pattern on which the string will be evaluated | {pattern: /[0-9]{4}\s[A-Z]{3}/i, ...} |
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
safePassword
Is valid if the tested string is at least 8 characters long and contains both letters and numbers.
Default error message: '%s must be at least 8 characters long and should contain both letters and numbers.'
Options
Name | Expected type | Description | Example |
---|---|---|---|
errorMessage | string | Sets a custom error message for this validation. Note that %s will be replaced by the name of the field or any text you define later on. |
{errorMessage: '%s must have numbers', ...} |
Tests
npm test
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.