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

serverless-lucky

v0.0.5

Published

Generate a JSON Schema from Yup Validators

Downloads

6

Readme

Lucky is a serverless plugin that generates JSON Schema's from Yup Validators Schemas.

You can transform a Yup Validator into a JSON Schema:

Note: Available only in Yup Validators using require imports and module.exports.

Validator

const { object, string, number } = require('yup')
const userValidation = object().shape({ email: string(), age: number() })

module.exports = userValidation

JSON Schema

{
  "type": "object",
  "properties": {
    "email": { "type": "string", "example": ""},
    "age": { "type": "number", "example": 10 }
  },
  "example": {}
}

How it works

Lucky generates many JSON Schema's from a specific path with Yup Validators, you can set the path for validators and get the output JSON files, Lucky also updates your documentation, adding the new generated models, only if its needed.

It's strongly recommended that you use a documentation plugin for your serverless app and enable it to use Lucky, an awesome plugin is: serverless-openapi-documenter

Installation

Install via npm in the root of your serverless app:

npm install serverless-lucky --save-dev

You can also install via yarn:

yarn add serverless-lucky -D

Add the plugin to the plugins section in your serverless serverless.yml:

plugins:
  - serverless-lucky

Configuration

Lucky configurations must be defined under custom.lucky in the serverless.yml file before they can be used in the functions' configs:

custom:
  lucky:
    validatorsBasePath: src/validations # Specify your validator's location.
    outputPath: docs/models # Specify where you want to generate the output files.
    useExamples: false # Default, set to true if JSON models should have an example property.
    inlineDocs: false # Default, set to true if your documentation don't use an external Yaml.

Note: Paths are relative to the root app folder.

Then, in your function, specify the httpApi.lucky

get-hello-world:
    handler: helloWorld/get.handler
    events:
      - httpApi:
          method: get
          path: /hello
          lucky:
            schema: hello/getValidator.js # Yup Validator File
            contentType: application/json # Default
            folders:
              - hello
              - hello/new

Note: Folder's array only uses unique values.

Generate JSON Schema's

To run, execute the command: sls lucky, the following message will be showed, if successful:

Lucky on get-hello-world: schema created in .../docs/models/hello/get.json

Another message will be followed about the same file in another folder, according to array's folder:

Lucky on get-hello-world: schema created in .../docs/models/hello/new/get.json

Yaml documentation

Lucky updates your documentation Yaml file before the creation of new JSON schemas, adding a reference to new the schema with default name, schema location, description and contentType.

Before:

documentation:
  version: '1'
    title: 'My API'
    description: 'This is my API'
    termsOfService: https://google.com
    externalDocumentation:
      url: https://google.com
      description: A link to google
    servers:
      url: https://example.com:{port}/
      description: The server
      variables:
        port:
          enum:
            - 4000
            - 3000
          default: 3000
          description: The port the server operates on
  models: {}

After:

documentation:
  version: '1'
    title: 'My API'
    description: 'This is my API'
    termsOfService: https://google.com
    externalDocumentation:
      url: https://google.com
      description: A link to google
    servers:
      url: https://example.com:{port}/
      description: The server
      variables:
        port:
          enum:
            - 4000
            - 3000
          default: 3000
          description: The port the server operates on
  models: 
    - name: GetHelloWorld
      description: ""
      contentType: application/json
      schema: ${file(docs/models/hello/get.json)}

Using folders property

One interesting concept in Lucky is that you also can use folders to create groups and organize the output schema files, specifying the same folder by different functions.

functions:
  get-hello-world:
    handler: helloWorld/get.handler
    events:
      - httpApi:
          method: get # Output file name
          path: /hello
          lucky:
            schema: hello/getValidator.js # Yup Validator File
            folders: 
            - hello # Same folder or 'group' as create-hello-world
   create-hello-world:
    handler: helloWorld/post.handler
    events:
      - httpApi:
          method: post # Output files names are the method of a function
          path: /hello
          lucky:
            schema: hello/createValidator.js # Yup Validator File
            folders: 
            - hello # Same folder or 'group' as get-hello-world

Note: It will create 2 files, post.json and get.json, located in docs/models/hello folder, according to outputPath.

License

MIT