1.6.0 • Published 7 years ago
propertype v1.6.0
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 --savepossible 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-requiredmin: minimum value - error:propertype-number-minmax: maximum value - error:propertype-number-max
string: a stringrequired: boolean - default:false- error:propertype-requiredmin: minimum string length - error:propertype-string-minmax: maximum string length - error:propertype-string-maxpattern: 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-requiredoptions: array of possible values - error:propertype-oneof-options
arrayOf: an array of a specific typerequired: boolean - default:false- error:propertype-requiredmin: minimum array length - error:propertype-array-minmax: maximum array length - error:propertype-array-maxtype: 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-requiredtypes: an object of property configurations, just like its top level one
usage example
const Propertype = require('propertype');
// define a struct
const Person = Propertype({
name: Propertype.string({ required: true, min: 3, max: 255 }),
gender: Propertype.oneOf({ required: true, options: [ 'male', 'female' ] }),
married: Propertype.boolean,
age: Propertype.number, // you can skip parens if the default type config is good for you (required: false)
skills: Propertype.arrayOf({ required: true, type: Propertype.string }),
email: Propertype.email.required, // that's right! you can do this just like prop-types allows you
outfit: Propertype.shape({ types: {
shirtColor: Propertype.string,
jeansColor: Propertype.string,
sneakerSize: Propertype.number({ min: 30, max: 50 }),
}}),
extras: Propertype.any,
});
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 types
const errors = Person.validate(payload); // return: { outfit: { sneakerSize: 'propertype-number-max' } }
// explain type rules
const rules = Person.explain();
// construct one
let person;
try {
person = Person(payload);
} catch(err) {
/*
err equals ValidationError {
code: 400,
name: 'ValidationError',
message: 'There are one or more errors in pre-construct validation',
info: {
outfit: {
sneakerSize: 'propertype-number-max',
},
},
}
*/
}