GithubHelp home page GithubHelp logo

vburzynski / express-openapi-validator-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cdimascio/express-openapi-validator-example

0.0 2.0 0.0 103 KB

simple openapi validation examplewith express-openapi-validator

Home Page: https://github.com/cdimascio/express-openapi-validator

JavaScript 100.00%

express-openapi-validator-example's Introduction

express-openapi-validator-example

Provides a simple example demonstrating how express-openapi-validator can be used to automatically validate api requests.

Setup the example project

# 1. clone this repo
git clone https://github.com/cdimascio/express-openapi-validator-example

# 2. install dependencies
npm install

Run it

Start the Api server

npm start

Try it

Try the out the following requests.

The express-openapi-validator automatically validates each request against an openapi 3 specification. If a request is does not match the spec, express-openapi-validator automatically returns an appropriate error response.

Validate a query parameter with a value constraint

ccurl -s http://localhost:3000/v1/pets/as |jq
{
  "errors": [
    {
      "path": ".params.id",
      "message": "should be integer",
      "errorCode": "type.openapi.validation"
    }
  ]
}

Validate a query parameter with a range constraint

curl -s http://localhost:3000/v1/pets\?limit\=1 |jq
{
  "errors": [
    {
      "path": ".query.limit",
      "message": "should be >= 5",
      "errorCode": "minimum.openapi.validation"
    },
    {
      "path": ".query.test",
      "message": "should have required property 'test'",
      "errorCode": "required.openapi.validation"
    }
  ]
}

Validate the query parameter's value type

curl -s --request POST \
  --url http://localhost:3000/v1/pets \
  --header 'content-type: application/xml' \
  --data '{
        "name": "test"
}' |jq
  "message": "unsupported media type application/xml",
  "errors": [
    {
      "path": "/v1/pets",
      "message": "unsupported media type application/xml"
    }
  ]
}

Validate a POST body to ensure required parameters are present

λ  curl -s --request POST \
  --url http://localhost:3000/v1/pets \
  --header 'content-type: application/json' \
  --data '{
}'|jq
{
  "message": "request.body should have required property 'name'",
  "errors": [
    {
      "path": ".body.name",
      "message": "should have required property 'name'",
      "errorCode": "required.openapi.validation"
    }
  ]
}

File upload example

curl -XPOST http://localhost:3000/v1/pets/10/photos -F [email protected]|jq
{
  "files_metadata": [
    {
      "originalname": "app.js",
      "encoding": "7bit",
      "mimetype": "application/octet-stream"
    }
  ]
}

Validate security

Using ApiKeyAuth

curl -XPOST http://localhost:3000/v1/pets |jq

{
  "message": "'X-API-Key' header required.",
  "errors": [
    {
      "path": "/v1/pets",
      "message": "'X-API-Key' header required."
    }
  ]
}

with the api key and security handler

curl -XPOST http://localhost:3000/v1/pets --header 'X-Api-Key: XXXXX' --header 'content-type: application/json' -d '{"name": "carmine"}' |jq
{
  "name": "sparky"
}

...and much more. Try it out!

Fetch the spec

curl http://localhost:3000/spec

express-openapi-validator-example's People

Contributors

cdimascio avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.