A Javascript library for data & type validation with Typescript support
npm install must-be-valid
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
}
}
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[]
}
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
- open an Issue on GitHub, describe the changes you want to introduce and check the feedback from community
- from branch
develop
create a feature branch with descriptive name - make changes
- check for code style inconsistencies with
npm run lint
- ensure all tests pass:
npm run test
- submit a Pull request with description
Thank you <3