1.1.0 • Published 4 years ago

json-validate-proxy v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

json-validate-proxy

JSON validate proxy.

A simple HTTP proxy that takes POST requests, validates JSON body against given JSON-schema and passes the request if it is valid. Invalid requests will be rejected with code 400 and detailed error message.

Installation

npm

npm install --global json-validate-proxy

Docker

docker pull korotaevio/json-validate-proxy

Usage

json-validate-proxy --help
json-validate-proxy --config config.yml

Docker

docker run \
  --detach \
  --restart unless-stopped \
  --name json-validate-proxy \
  --publish 3000:3000 \
  --volume $(pwd)/config.yml:/opt/json-validate-proxy/config.yml \
  korotaevio/json-validate-proxy

When using the docker image you are supposed to mount a config file at /opt/json-validate-proxy/config.yml. Log format is defaults to json. You can override this behaviour by specifying custom command:

docker run \
  --detach \
  --restart unless-stopped \
  --name json-validate-proxy \
  --publish 3000:3000 \
  --volume $(pwd)/config.yml:/opt/json-validate-proxy/config.yml \
  korotaevio/json-validate-proxy \
  json-validate-proxy --log-format json --config /opt/json-validate-proxy/config.yml

Configuration

Application is configured via environment variables, command line arguments and a config file.

Environment Variables

JSON_VALIDATE_PROXY_HTTP_LISTEN_HOST – listen interface
JSON_VALIDATE_PROXY_HTTP_LISTEN_PORT – listen port

Command line arguments

See built-in help via --help flag.

Config file

Config file consists of schema list and routing table.

JSON schema refers to a public standard, validation implemented with Ajv. So, any valid JSON schema is supported.

All routes are exposed at the /proxy prefix. So, when you specify route: /foo, this route will be available at /proxy/foo path.

Request body will be parsed and JSON only if there is corresponding Content-Type header. Acceptable values by default is application/json, application/json-patch+json, application/vnd.api+json, application/csp-report. You can extend this set with the extendTypes option.

The proxy.url specifies exact URL where the request will be sent. Proxied request URL will not be modified in any way with original request URL parts.

schemas: # array of JSON schema objects
  - $id: test # schema ID is required
    type: object
    properties:
      foo:
        type: string
        const: bar
    required: ["foo"]
routes:
  - route: /foo # location for this rule (will be at /proxy/foo)
    extendTypes: ["*/*"] # extend the default set of JSON-related Content-Type headers
    proxy:
      url: http://localhost:8080/bar # URL where to proxy to
      timeout: 1000 # upstream timeout in ms (optional, 5000 by default)
    schema: test # schema ID to use