npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validstring

v1.1.9

Published

A handy validation library for strings

Downloads

5

Readme

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

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