npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@general-galactic/entity-decorators

v0.1.1

Published

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

Downloads

4

Readme

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 :)