assertInput
Assert input is a small library to easily verify different types of user inputs. It will work on an object of data and return a rejected promise with a suiting error messages if any data is not validated. If all data is validated, it will returned a cleaned object with any superfluous fields removed.
Examples
When data isn't validated
const assertInput = ; const data = name: 'xyz' age: 150; ;
Function throwError
will be called with
When all data is validated
const assertInput = ; const data = name: 'Foo Bar' age: 42 someOtherField: 'some other data';
Function insertIntoDatabase
will be called with
Validators
Numbers
- isNumber
- minNumber(min)
- maxNumber(max)
- minMaxNumber(min, max)
Strings
- isString
- minStringLength(min)
- maxStringLength(max)
- minMaxStringLength(min, max)
More validators coming soon
Custom validators
You can create your own validators and use them in combinations with the already existing ones.
const assertInput = ; // Create the validatorlet hexColorValidator = /^#?$/ failText: `must be a hex color.`;// Combine it with isString to first verify that the data is a stringhexColorValidator = assertInputisString hexColorValidator; ;