项目作者: general-galactic

项目描述 :
A set of typescript decorators that can be used to generate schemas, validations, and metadata from typescript classes / entities.
高级语言: TypeScript
项目地址: git://github.com/general-galactic/entity-decorators.git
创建时间: 2021-01-25T23:25:07Z
项目社区:https://github.com/general-galactic/entity-decorators

开源协议:Apache License 2.0

下载


Entity Decorators

This project contains a set of typescript decorators that allow you to mark up your typescript entity classes. You
can then use transformers to convert the decorators to things like json schemas and Joi Schemas. You can also write your
own decorators and transformers to do whatever you need. Here’s what it looks like:

  1. export default class Entity {
  2. @Required
  3. @MinimumLength(6)
  4. firstName: string
  5. @Optional
  6. @MinimumValue(1)
  7. articleCount: number
  8. @Required
  9. @Enumeration( UserTypes.member, UserTypes.owner )
  10. userType: UserTypes
  11. @Required
  12. address: Address
  13. @Required
  14. @ArrayItems( ContactMethod )
  15. @MinimumLength(1)
  16. contactMethods: ContactMethod[]
  17. }

You can then use this class to generate a schema using a tranformer like this:

  1. const transformer = new JoiSchemaTransformer()
  2. const entityJoiSchema = transformer.tranformFromEntityClass( Entity )
  3. entityJoiSchema.validate( entity )

If you use Hapi or Fastify you can use transformers to generate schemas for your routes. These schemas can even
be used to generate OpenAPI documentation for your APIs:

HAPI ( gives you runtime validation and Swagger or OpenAPI documentation )

  1. const transformer = new JoiSchemaTransformer()
  2. server.route({
  3. method: 'POST',
  4. path: '/post',
  5. options: {
  6. validate: {
  7. payload: transformer.tranformFromEntityClass( RoutePostBody )
  8. }
  9. },
  10. handler( request, h ) { ... }
  11. })

FASTIFY -> COMING SOON

  1. const transformer = new JsonSchemaTransformer()
  2. fastify.post('/post', {
  3. handler () {},
  4. schema: {
  5. body: transformer.tranformFromEntityClass( RoutePostBody )
  6. }
  7. })

Installing

Install from npm: npm i @general-galactic/entity-decorators.

Decorators

General

  • Required - must be populated. Will NOT allow null or undefined values.
  • Optional - will allow populated, null, or undefined values.
  • Description - provide notes to the swagger generator - shows up on swagger UI.
  • Enumeration - provide a finite set of allowed values.
  • Scope - define scopes which are groups of properties. e.g. public, private, etc.

Strings

  • MinimumLength - Minimum string length.
  • MaximumLength - Maximum string length.
  • Url - A string that should match a url format.
  • Email - A string that should match an email format.
  • ISO8601Date - A string that should match the ISO8601 date format.

Arrays

  • ArrayItems - used to define the type of an array of objects. This is required to descend into and extract decorators from the sub objects.
  • MinimumLength - Minimum array length.
  • MaximumLength - Maximum array length.

Numbers

  • MinimumValue - Minimum number value.
  • MaximumValue - Maximum number value.
  • NegativeValue - Negative number values only.
  • PositiveValue - Positive number values only.

Dates

  • ISO8601Date - A date field that should match the ISO8601 date format.

Contributing

We’d love help fixing bugs and adding new capabilities. Send us a PR :)