1.1.9 • Published 7 years ago

validstring v1.1.9

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

ValidString

MIT license npm npm dependencies travis-ci Code Climate Test Coverage Issue Count

A handy validation library for strings

Installation

npm install validstring --save

Usage

import ValidString from 'validstring'

var validator = new ValidString()
validator.alphaNumeric({extraChars: ' '}).notEmpty().test('Hello world').isValid
// Returns: true

In order to not force you to change your programming style, there are several ways to achieve the same results.

import ValidString from 'validstring'

// Chained validation
var validator1 = new ValidString()
validator.alphaNumeric({extraChars: ' '}).notEmpty().test('Hello world').isValid
// Returns: true

// Constucted validation
var validator2 = new ValidString({
  notEmpty: null,
  alphaNumeric: {extraChars: ' '}
})
validator2.test('Hello world').isValid
// Returns: true

// Built validation
// Useful when the validation needs to be defined programmatically
var validator3 = new ValidString()
validator3.append('notEmpty').append('alphaNumeric', {extraChars: ' '}).test('Hello world').isValid
// Returns: true

// Bi-dimensional build validation
var validator4 = new ValidString()
validator4.appendMap({
  notEmpty: null,
  alphaNumeric: {extraChars: ' '}
}).test('Hello world').isValid
// Returns: true

For simplicity reasons all the examples from now on will be shown using the chainable style, as shown in validator1

Check Validity

var validator = new ValidString()
validator.numeric().test('John doe').isValid
// Returns: false

Assert validity

var validator1 = new ValidString()
validator1.numeric().test('John doe').assert(false)
// Returns: true

var validator2 = new ValidString()
validator2.numeric().assert('John doe', false)
// Returns: true

Note that: The validator2 differs from validator1 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 = new ValidString()
validator.alphabetic().test('John 123').isValid
// Returns: false

validator1.getErrorMessages('Your name')
// 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
NameExpected typeDescriptionExample
extraCharsstringExtends the range of characters accepted by this validation.{extraChars: '-_$', ...}
errorMessagestringSets 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
NameExpected typeDescriptionExample
extraCharsstringExtends the range of characters accepted by this validation.{extraChars: '-_$', ...}
errorMessagestringSets 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
NameExpected typeDescriptionExample
chars requiredstringSpecifies the characters that should be present in the tested string.{chars: '-_$', ...}
errorMessagestringSets 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
NameExpected typeDescriptionExample
chars requiredstringSpecifies the characters that should not be present in the tested string.{chars: '-_$', ...}
errorMessagestringSets 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
NameExpected typeDescriptionExample
errorMessagestringSets 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
NameExpected typeDescriptionExample
extraCharsstringExtends the range of characters accepted by this validation.{extraChars: '-_$', ...}
errorMessagestringSets 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
NameExpected typeDescriptionExample
pattern requiredRegExpSets the pattern on which the string will be evaluated{pattern: /[0-9]{4}\s[A-Z]{3}/i, ...}
errorMessagestringSets 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
NameExpected typeDescriptionExample
errorMessagestringSets 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.

1.1.9

7 years ago

1.1.8

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago