Readme
Untie is a simple validator for JS-objects.
Usage
const Type Validator = ; const input = user: id: 13 nickname: 500 profile: firtname: 'Sergey' lastname: 'Sova' ; static schema = id: TypenumberRequired name: TypestringRequired profile: Typeshape firstname: string lastname: string ; console;
Simple docs
validate
method returns:
null
if passed document is valid.Error
if document has errors.- object of
Error
s if document nested fields has errors.
Installation
npm install --save untie
Usage with Express
validators/user.js
const Type Validator = ; moduleexports = static schema = login: TypestringRequired email: TypestringRequired { const value = dataObjectproperty; if typeof value !== 'string' return 'Invalid property `password`, expected type `string`'; if valuelength < 8 return `Property password must be 8 symbols minimum.`; } ;
for Node < v6
var untie = ; moduleexports = untie;
routes/register.js
const UserValidator = ; module { const error = UserValidator; // return null or error object if error return ; // create user model and save reqstatus201;}
Validators
-
array
-
bool
-
func
-
number
-
object
-
string
-
symbol
-
any
-
arrayOf
-
objectOf
-
oneOf
-
OneOfType
-
shape
More examples
const array bool func number object string symbol any arrayOf objectOf oneOf OneOfType shape} = Type; const exampleValidationSchema = optionalArray: array optionalBool: bool optionalFunc: func optionalNumber: number optionalObject: object optionalString: string optionalSymbol: symbol optionalAnyValue: any // Value must one of that values optionalEnum: // Value must correspond one of type optionalUnion: // each item in array must be a number optionalNumbersArray: // Value of each key must be function optionalMethods: // Object must correspond defined struct optionalPlayer: // That method must be defined requiredFunc: funcRequired // Should be passed any value requiredAny: anyRequired { if dataObjectproperty >= 1200 return 'Custom property must be more than 1199.'; } // Required all props in required shape requiredShape: shape;