propertype
a struct constructor/validator library. propertype is a tool to check if an object has the proper type on its properties. as a constructor, it will extract properties defined in its type out of the payload it has been given.
installation
$ npm i propertype --save
possible types
any
: anythingrequired
: boolean - default:false
- error:propertype-required
boolean
: boolean or string of'true'
or'false'
required
: boolean - default:false
- error:propertype-required
number
: number or a string of a numberrequired
: boolean - default:false
- error:propertype-required
min
: minimum value - error:propertype-number-min
max
: maximum value - error:propertype-number-max
string
: a stringrequired
: boolean - default:false
- error:propertype-required
min
: minimum string length - error:propertype-string-min
max
: maximum string length - error:propertype-string-max
pattern
: a regex pattern to test against - default:.*
- error:propertype-string-pattern
object
: any object without caring about its propertiesrequired
: boolean - default:false
- error:propertype-required
oneOf
: the value should equal one of the values in its options keyrequired
: boolean - default:false
- error:propertype-required
options
: array of possible values - error:propertype-oneof-options
arrayOf
: an array of a specific typerequired
: boolean - default:false
- error:propertype-required
min
: minimum array length - error:propertype-array-min
max
: maximum array length - error:propertype-array-max
type
: the specific type that all values in the array should be - default:any
shape
: shape is like a nested propertype configrequired
: boolean - default:false
- error:propertype-required
types
: an object of property configurations, just like its top level one
usage example
const Propertype = ; // define a structconst Person = ; const payload = name: 'Dariush Alipour' gender: 'male' married: true age: 26 skills: 'js' 'golang' email: 'drsh.alipour@gmail.com' outfit: shirtColor: 'black' jeansColor: 'blue' sneakerSize: 55 ; // only check/validate typesconst errors = Person; // return: { outfit: { sneakerSize: 'propertype-number-max' } } // explain type rulesconst rules = Person; // construct onelet person;try person = ; catcherr /* err equals ValidationError { code: 400, name: 'ValidationError', message: 'There are one or more errors in pre-construct validation', info: { outfit: { sneakerSize: 'propertype-number-max', }, }, } */