@juneil/entityts v2.0.0
EntityTS
Introduction
The idea is to use the power of Typescript/Decorators to have an easy and practical entity library. Based on a transformer system, this library use Joi to transform the entities to Validation Schemas.
How to install
npm install --save @juneil/entityts
Usage
import { Entity, Type, Required } from '@juneil/entityts';
class User extends Entity {
@Type(Entity.Type.Hex)
@Length(10)
@Required()
id: string;
@Type(String)
name: string;
}
const user1 = new User({ id: '1DB6A7FF08' });
user1.isValid() // true
const user2 = new User();
user2.isValid() // falseStatic methods
schema(ModeEnum | EntityOptions): Joi.Schema- Build and return the schema for a mode
Instance methods
schema(ModeEnum | EntityOptions): Joi.Schema- Build and return the schema for a mode
isValid(ModeEnum): boolean- Based on the schema, valid the instance
EntityOptions
strict?: boolean - Default: true - Used in the constructor of the entity to throw an error if validation failed
mode?: ModeEnum - Default: ModeEnum.READ - Schema mode
array?: boolean - Default: false - Build the schema as a list of the Entity
unknown?: boolean - Default: true - Allow unknown keys in the payload
Decorators
Type
@Type(String | Number | Date | Object | Boolean | Buffer | TypeEnum | *Entity)Specify the type of the property's value and will be the base of the Schema
*Entity: Allow you to have a property with the type of an Entity. Example:
class Entity2 extends Entity {
@Required()
@Type(String)
id: string
}
class Entity1 extends Entity {
@Type(String)
id: string
@Type(Entity2)
sub: Entity2
}
new Entity1().isValid() // true
new Entity1({ sub: {} }).isValid() // false
new Entity1({ sub: { id: 'abc' } }).isValid() // trueArray
@Array(String | Number | Date | Object | Boolean | Buffer | TypeEnum | *Entity)Same as @Type() but with array.
ObjectPattern
@ObjectPattern(RegExp, String | Number | Date | Object | Boolean | Buffer | TypeEnum | *Entity)Allow to define the key's pattern and the value's type of an object
Required
@Required(...ModeEnum) // By default: Entity.Mode.READSet the property as required, can be setted for several modes
Strip
@Strip(...ModeEnum) // By default: Entity.Mode.READThe property will be removed depending of the modes provided
Valid
@Valid(...any)Set the valid values for the property
Invalid
@Invalid(...any)Set the invalid values for the property
Allow
@Allow(...any)Set the allowed values for the property
Min
@Min(number)Add Min validation for the property's value.
Works with types: String | Number | Array | Buffer | TypeEnum
Max
@Max(number)Add Max validation for the property's value.
Works with types: String | Number | Array | Buffer | TypeEnum
Length
@Length(number)Add Length validation for the property's value.
Works with types: String | Array | Buffer | TypeEnum
Description
@Description(string)Regex
@Regex(pattern)Add regex validation for a string
Works with type: String
TypeEnum
Entity.Type.Any: anyEntity.Type.ObjectId: String ObjectId from mongodbEntity.Type.Hex: String in hexadecimal formatEntity.Type.Base64: String in base64 formatEntity.Type.IsoDate: String in iso date formatEntity.Type.URI: String URI formatEntity.Type.Email: String Email formatEntity.Type.Timestamp: Date timestamp format
ModeEnum
Entity.Mode.READ: Default modeEntity.Mode.CREATE: Mode to use while creating in a datasourceEntity.Mode.UPDATE: Mode to use while updating in a datasource
More
You can extend the schema with `more()' static methods
class Entity1 extends Entity {
@Type(String)
id: string
// Make id required
static more() {
return Joi.object({
id: Joi.any.required()
});
}
}Extends an other entity
You can directly extend an entity
class Entity1 extends Entity {
@Type(String)
id: string
}
class Entity2 extends EntityExtends(Entity1) {
@Type(Number)
id: number
@Type(Boolean)
flag: boolean
}6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago