hapi-joi-decorators
Thisrepository is elisvathi/hapi-joi-decorators joi upgrade to match last hapi joi version Allows to create validation schemas by using typescript decorators using joi as a backend
Status: Work in progress
Table of Contents
- hapi-joi-decorators
Installation
npm install --save hapi-joi-decorators
General Decorators
Required Decorator
Invalidates the object if the field doesn't exist
Usage
// Joi schema schema.required;
or...
; ; // Joi schema console.log'example schema', schema;
Optional Decorator
Turns off the required flag for the field
Usage
// Joi schema schema.optional;
Nullable Decorator
Allows the field to have null as a value
Usage
// Joi schema schema.allownull
or...
ValidOptions Decorator
Allows only the values provided in the arguments
Usage
// Joi schema schema.valid'name', 'age';
Number Decorators
Max Decorator
Specifies the max value for the field
Usage
// Joi schema schema.max42;
or...
// Joi schema schema.max42.invalid42
Min Decorator
Specifies the min value for the field
Usage
// Joi schema schema.min42;
or...
// Joi schema schema.min42
Positive Decorator
Allows positive values only to the number field
Usage
// Joi schema schema.positive;
Negative Decorator
Allows negative values only to the number field
Usage
// Joi schema schema.negative;
String Decorators
MinLength Decorator
Sets a lower limit to the string length
Usage
// Joi schema schema.min42;
MaxLength Decorator
Sets an upper limit to the string length
Usage
// Joi schema schema.max42;
NotEmpty Decorator
Sets the string lower limit to 1 character
Usage
// Joi schema schema.min1;
Email Decorator
String field should be a valid email
Usage
// Joi schema schema.email;
DateString Decorator
Validates the string as a date using the specified format
Usage
// Joi schema schema.date'YY-MM-DD';
Array Decorators
ItemType Decorator
Is required to specify the array item type if the array items should be validated
Usage
// Joi schema schema.array.itemsJoi.object.keys;
MinLength Decorator
Sets a lower limit to the array length
Usage
// Joi schema schema.array.min42;
MaxLength Decorator
Sets an upper limit to the array length
Usage
// Joi schema schema.array.max42;
NotEmpty Decorator
Sets the array lower limit to 1 item
Usage
// Joi schema schema.array.min1;
Date Decorators
DateString format decorator
Specifies the format that the field should have as an input Uses joi-date-extensions Extension as a backend
Usage
// Joi schema Joi.date.format'YYYY-MM-DD'.required;
Custom Validation Schema
CustomSchema Decorator
You can create a custom Joi schema for the field or append properties to existing one
Usage
// Using a lambda function // Creating a schema from scratch, overrides any pre-built schema from other decorators
Nested Validations
Validates the child fields if their type is another class
Usage
// Joi schema schema.object.keys;
Inheritance
Inherited classes inherit the validation metadata from the base class
Usage
// Joi schema schema.object.keys;
You can also override validation properties from the base class
// Joi schema // Base Class schema.object.keys; // Derived Class schema.object.keys;
Getting the validation schema
; ;
Validating the object
; value.email = "john@example.com" Validatevalue.catch;
Example
fetchData.thenValidateuser;