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

egg-swagger-validate

v0.2.1

Published

Validate parameters via swagger for egg.

Downloads

2

Readme

WARNING: DO NOT USE IT IN YOUR PRODUCTION ENVIRONMENT BEFORE 1.0.0.

Installation

npm install -S egg-swagger-validate

# and its dependencies
npm install -S egg-validate

Usage

Import it and its dependencies via config/plugin.js:

module.exports = {
  ...

  'validate': {
    enable: true,
    package: 'egg-validate'
  },
  'swagger-validate': {
    enable: true,
    package: 'egg-swagger-validate'
  }

  ...
}

Config it via config/config.<env>.js:

module.exports = {
  ...

  swaggerValidate: {
    swaggerFile: '/absolute/path/to/swagger/file'
  }

  ...
}

Extentions to the Swagger Schema

Why?

It is NOT one-to-one mapping of Swagger Schema parameters' type to egg-validate's type. Therefore, we have to find a way to do this.

Swagger specification said:

Allows extensions to the Swagger Schema. The field name MUST begin with x-, for example, x-internal-id.

So, I extend it.

Extensions

Extensions of Parameter Object:

  • x-format corresponding to type in parameter
  • x-format-options corresponding to other fields except type in parameter
  • x-controller bind a controller to current path automatically

Do not use app/router.js in egg.js, if you start to use x-controller.

More information of available value of x-format and x-format-options can be found in parameter's document.

Extentions to egg-validate

Why?

egg-swagger-validate will check multiple types of parameters at one time, different from what egg-validate does. Therefore, we have to find a way to distinguish different types of validate error.

Extensions

Extensions of egg-validate:

  • Add in field for errors.

For example, when using egg-validate, the response is:

{
    "code": "invalid_param",
    "errors": [
        {
            "code": "missing_field",
            "field": "quantity",
            "message": "required"
        }
    ],
    "message": "Validation Failed"
}

But, when using egg-swagger-validate, the response is:

{
    "code": "invalid_param",
    "errors": [
        {
            "code": "missing_field",
            "field": "quantity",
            "in": "query",
            "message": "required"
        }
    ],
    "message": "Validation Failed"
}

You can see, in field let you know where the missing field in.

Functions of egg-swagger-validate

  1. generates validate rules from x-format and x-format-options
  2. validate parameters according to rules generated from step 1
  3. if not pass, throw error
  4. if pass, handle request by using controller specified by x-controller

Example of extended Swagger Schema

...

paths:
  /mails:
    get:
      summary: get mails
      description: Get Mails
      parameters:
        - name: email
          in: query
          description: email address
          required: true
          type: string
          x-format: email
        - name: quantity
          in: query
          description: quantity of emails
          required: true
          type: number
          x-format: number
          x-format-options:
            max: 20
            min: 1
      x-controller: mails.get

...

Last

Sit here, code this.