New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@general-galactic/entity-decorators

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@general-galactic/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](https://joi.dev). You can also write your

  • 0.1.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

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:

export default class Entity {

    @Required()
    @MinimumLength(6)
    firstName: string

    @Optional()
    @MinimumValue(1)
    articleCount: number

    @Required()
    @Enumeration( UserTypes.member, UserTypes.owner )
    userType: UserTypes

    @Required()
    @SubObject( Address )
    address: Address

    @Required()
    @ArrayItems( ContactMethod )
    @MinimumLength(1)
    contactMethods: ContactMethod[]

}

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

const transformer = new JoiSchemaTransformer()
const entityJoiSchema = transformer.tranformFromEntityClass( Entity )
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 )

const transformer = new JoiSchemaTransformer()

server.route({
    method: 'POST',
    path: '/post',
    options: {
        validate: {
            payload: transformer.tranformFromEntityClass( RoutePostBody )
        }
    },
    handler( request, h ) { ... }
})

FASTIFY -> COMING SOON

const transformer = new JsonSchemaTransformer()

fastify.post('/post', {
  handler () {},
  schema: {
    body: transformer.tranformFromEntityClass( RoutePostBody )
  }
})

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.
  • SubObject - used to define to descend into and extract decorators from a sub objects.
  • 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.
  • Phone - A string that should match a phone 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 :)

FAQs

Package last updated on 29 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc