string-validators
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

String-Validators

ForTheBadge built-with-love
Version


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

GitHub release (latest SemVer including pre-releases) GitHub last commit Stargazers Contributors

Forks Issues requests MIT License


Signaler un Bug · Proposer une Feature


Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Validators overview
  5. Roadmap
  6. Contributing
  7. License
  8. Connect with me

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.

(back to top)

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

(back to top)

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

(back to top)

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.

(back to top)

Roadmap

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

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

(back to top)

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

(back to top)

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.

(back to top)

Connect with me:

jeromedelauney jérôme-delauney-802994bb

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

(back to top)

Package Sidebar

Install

npm i string-validators

Weekly Downloads

4

Version

2.0.0

License

MIT

Unpacked Size

182 kB

Total Files

159

Last publish

Collaborators

  • jdelauney