3.0.0 • Published 2 years ago

must-be-valid v3.0.0

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

must-be-valid

A Javascript library for data & type validation with Typescript support

Install

npm install must-be-valid

Use

import { mustBeArray, mustBeNumber, mustBeObject, mustBeString } from 'must-be-valid'

function makeUser(userDto: unknown) {
  const userInfo = mustBeObject(userDto) // throws if not valid

  return {
    username: mustBeString(userInfo.username), // throws if not valid
    password: mustBeString(userInfo.password), // throws if not valid
    age: mustBeNumber(userInfo.age), // throws if `age` is not a number
    friendIds: mustBeArray(userInfo.friendIds).map((f) => mustBeString(f)), // throws if not valid
  }
}

Awesome type inferencing

Thanks to extensive Typescript support by the library, including the use of generics, Typescript infers the following return type of makeUser function:

function makeUser(userDto: unknown): {
  username: string
  password: string
  age: number
  friendIds: string[]
}

Chaining

const password = mustBe.string(userInfo.password).min(7).max(50).value // throws if not valid
const age = mustBe.number(userInfo.age).min(13).value // throws if not valid

Documentation

Contribute

  1. open an Issue on GitHub, describe the changes you want to introduce and check the feedback from community
  2. from branch develop create a feature branch with descriptive name
  3. make changes
  4. check for code style inconsistencies with npm run lint
  5. ensure all tests pass: npm run test
  6. submit a Pull request with description

Thank you <3

Contact author

filip.biterski@gmail.com

3.0.0

2 years ago

2.0.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago