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

string-validators

v2.0.0

Published

Little Javascript / Typescript library for validating format of string like email, url, password...

Downloads

11

Readme

About The Project

The first goal of this project is to create complete and personalized validation schemes for strings by using native functions as much as possible. This is in order to obtain maximum security and to avoid as much as possible the use of RegEx which would be likely to be subject to ReDOS attacks.

Getting Started

Installation

Use your preferred node package manager.

> pnpm install

Or clone this repository

  • Clone project

    > git clone https://github.com/jdelauney/string-validators.git

    Go to the project directory

    > cd string-validators
  • Install dependencies with npm, pnpm or yarn:

    > pnpm install

Usage

How to make your on custom string format validation schema

  1. Create test

    • in the __tests__ folder create your spec test and test it with the following command
      > pnpm test:watch src/__tests__/yourTestFile.spec.ts
    • in the __tests__ folder create your unit test
    import { describe, expect, test } from 'vitest';
    import { validator } from 'string-validators';
    
    const validPasswords = [
      'abC$123DEf',
      'ABc1$ef#gh',
      'aB$C23dE2f',
    ];
    
    const invalidPasswords = [
      '',
      'abcdef',
      'ab$12AB',
      'Ab1$2cdef',
      'AB1$cdef',
    ];
    
    describe('Feature : Strong password validator', () => {
      describe('Given a list of valid password', () => {
        test.each(validPasswords)('When %p as argument, it should return TRUE', async password => {
          const isValid = await isValidStrongPassword(password);
          expect(isValid).toBe(true);
        });
      });
    
      describe('Given a list of invalid password', () => {
        test.each(invalidPasswords)('When %p as argument, it should return FALSE', async password => {
          const isValid = await isValidStrongPassword(password);
          expect(isValid).toBe(false);
        });
      });
    });
  2. Launch test in watch mode

> pnpm test:watch src/__tests__/yourTestFile.test.ts
  1. Write your code and refactor it until all tests are green
import {
  validator, 
  minLength, 
  containsOneOfCharsCount, 
  CHARSET_LOWER_ALPHA, 
  CHARSET_NUMBER, 
  CHARSET_UPPER_ALPHA
} from "string-validators";

const isValidStrongPassword = (password: string) => {
  return validator(password, [
    not(isEmpty),
    minLength(8),
    containsOneOfCharsMinCount('$#%+*-=[]/(){}€£!?_', 1),
    containsOneOfCharsMinCount(CHARSET_LOWER_ALPHA, 3),
    containsOneOfCharsMinCount(CHARSET_UPPER_ALPHA, 2),
    containsOneOfCharsMinCount(CHARSET_NUMBER, 2),
  ]);
}

const isValidStrongPassword2 = validator('abC$123Def', [
  not(isEmpty),
  minLength(10),
  containsOneOfCharsMinCount('$#%+*-=[]/(){}€£!?_', 2),
  containsOneOfCharsMinCount(CHARSET_LOWER_ALPHA, 3),
  containsOneOfCharsMinCount(CHARSET_UPPER_ALPHA, 2),
  containsOneOfCharsMinCount(CHARSET_NUMBER, 3),
]);
// return false

Validators overview

To help us as much as possible to create validation schemas. The 'String-Validators' library contains more than 50 validation rules that one can apply.

Here are the full list of available validators :

  • isEmpty() : check if string is empty

  • isEqual(equalStr)

  • minLength(min) : check if string has a minimum number of characters

  • minLength(max) : check if string has a maximum number of characters

  • rangeLength(min, max)

  • isLengthEqual(equal) : check if string has the exact required number of characters

  • isUpper() : check if string is in upper case only

  • isLower() : check if string is in lower case only

  • isAlpha() : check if string only contain Alpha characters

  • isAlphaNumeric() : check if string only contain Alpha numerics characters

  • isNumber() : check if string only contain Number characters

  • startsWith(startStr) : check if string starts with

  • startsWithOneOf(string[])

  • startsWithOneOfChars(chars)

  • startsWithSpecialChars()

  • startsWithNumber()

  • startsWithUpperCase()

  • startsWithLowerCase()

  • endsWith(startStr) : check if string ends with

  • endsWithOneOf(string[])

  • endsWithOneOfChars(chars)

  • endsWithSpecialChars()

  • endsWithNumber()

  • endsWithUpperCase()

  • endsWithLowerCase()

  • contains(subStr)

  • containsAt(subStr, pos)

  • containsCount(subStr, count)

  • containsMinCount(subStr, minCount)

  • containsMaxCount(subStr, maxCount)

  • containsRangeCount(subStr, minCount, maxCount)

  • containsOneOf(string[])

  • containsOneOfCount(chars, count)

  • containsOneOfMinCount(chars, minCount)

  • containsOneOfMaxCount(chars, maxCount)

  • containsOneOfRangeCount(chars, minCount, maxCount)

  • containsOneOfChars(chars)

  • containsOneOfCharsCount(chars, count)

  • containsOneOfCharsMinCount(chars, minCount)

  • containsOneOfCharsMaxCount(chars, maxCount)

  • containsOneOfCharsRangeCount(chars, minCount, maxCount)

  • containsOnlyOneOfChars(chars)

  • containSpecialChars()

  • match(regex) : check if string match with a regex

  • surroundBy(leftStr, rightStr) : check if string is surrounded by leftStr and rightStr

  • surroundByOneOf(string[], string[])

  • surroundByOneOfChars(startChars, endChars)

  • surroundByOneOfPairs(string[], string[])

  • leftOf(subStr, leftStr) : check if the first occurrence of subStr have leftStr on his left

  • leftOfOneOf(subStr, string[])

  • leftOfOneOfChars(subStr, chars)

  • rightOf(subStr, leftStr) : check if the first occurrence of subStr have rightStr on his right

  • rightOfOneOf(subStr, string[])

  • rightOfOneOfChars(subStr, chars)

  • followBy(subStr, followByStr)

  • followByOneOf(subStr, string[])

  • followByOneOfChars(subStr, chars)

  • oneOfFollowBy()

  • oneOfCharsFollowByOneOfChars()

  • not(validatorFunc) : Negate the result of a validator

  • or(validatorFuncA, validatorFuncB)

For the complete list of available validator check the validators folder. Names are enough friendly to understand their purposes.

Roadmap

  • [ ] Write a full documentation
  • [ ] add more validators

See the open issues for a full list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License.

Copyright 2022 J.DELAUNEY

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.

Connect with me:

Project Link: https://github.com/jdelauney/string-validators