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.
npm install --save @juneil/entityts
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() // false
schema(ModeEnum | EntityOptions): Joi.Schema
- Build and return the schema for a mode
schema(ModeEnum | EntityOptions): Joi.Schema
- Build and return the schema for a mode
isValid(ModeEnum): boolean
- Based on the schema, valid the instance
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
@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() // true
@Array(String | Number | Date | Object | Boolean | Buffer | TypeEnum | *Entity)
Same as @Type() but with array.
@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(...ModeEnum) // By default: Entity.Mode.READ
Set the property as required, can be setted for several modes
@Strip(...ModeEnum) // By default: Entity.Mode.READ
The property will be removed depending of the modes provided
@Valid(...any)
Set the valid values for the property
@Invalid(...any)
Set the invalid values for the property
@Allow(...any)
Set the allowed values for the property
@Min(number)
Add Min validation for the property's value.
Works with types: String | Number | Array | Buffer | TypeEnum
@Max(number)
Add Max validation for the property's value.
Works with types: String | Number | Array | Buffer | TypeEnum
@Length(number)
Add Length validation for the property's value.
Works with types: String | Array | Buffer | TypeEnum
@Description(string)
@Regex(pattern)
Add regex validation for a string
Works with type: String
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
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
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()
});
}
}
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
}