First NPM package, Express AutoRoute with jsonSchema validation
Tags: opensource, discuss, showdev, express
Published: 2019-06-11 13:53:00

---
title: First NPM package, Express AutoRoute with jsonSchema validation
published: true
description: My very first public NPM package, which allows autorouting for express, with plugins for json schema validation on both the request and response.
tags: opensource, discuss, showdev, express
---

This is my first package. I'm currently using it for one of my other personal project and decided to make its own package, for others to use. 

I'm a little nervous as I haven't really shared my worked publicly but I've also written about [facing your fears](https://dev.to/dechamp/the-results-of-putting-your-fears-to-the-side-42gg) before. 

This package is meant to make route setup clean and configurable, while adding the ability to add middleware for not just all routes, but individual routes as well. 

Setup is simple as just a few lines.

```js

const {default: autoRoute, RouteRequestJsonSchemaValidator, RouteResponseJsonSchemaValidator} = require('express-auto-route');
//express server
const server = ...;
// see exmple below.
const routes = ...;
 
autoRoute({
  server,
  routes,
  middlewares: [
    RouteRequestJsonSchemaValidator,
    RouteResponseJsonSchemaValidator,
    // your custom middleware
  ],
});
```

Then just add your settings via a config file.

```js
const routes = [
    {
        id: "resourceGet",
        method: "GET",
        modulePath: "/routes/resource/get.js",
        path: "/resources",
        responseJsonSchema: "/path/to/schemas/resources.schema.json",
    },
    {
        id: "resourceGetUuid",
        method: "GET",
        modulePath: "/routes/resource/get.js",
        path: "/resources/:uuid",
        requestJsonSchema: {
            query: "/path/to/schemas/getByUuid.schema.json"
        },
        responseJsonSchema: "/path/to/schemas/resource.schema.json"
    }
]
```

Then just add our schemas, as you can see below that we have the full ability to combine schemas for powerful clean solutions.

/path/to/schemas/resources.schema.json
```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "resources",
  "type": "array",
  "items": {
    "$ref": "schema/resource.schema.json"
  }
}

```

/path/to/schemas/resources.schema.json
```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/resource",
  "definitions": {
    "resource": {
      "title": "resource",
      "type": "object",
      "required": [
        "name",
        "path",
        "description"
      ],
      "additionalProperties": false,
      "properties": {
        "uuid": {
          "$ref": "schema/uuid.schema.json"
        },
        "name": {
          "type": "string",
          "title": "Name",
          "minLength": 2,
          "maxLength": 60
        },
        "path": {
          "type": "string",
          "title": "Path",
          "minLength": 1,
          "pattern": "^\\/[\\w\\-.~%!$&'()*+,;=:@\\/]*$"
        },
        "description": {
          "type": "string",
          "title": "Description",
          "minLength": 1,
          "maxLength": 254
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "updatedAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    }
  }
}

```

/path/to/schemas/getByUuid.schema.json
```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "getByUuid",
  "type": "object",
  "required": [
    "uuid"
  ],
  "properties": {
    "uuid": {
      "$ref": "schema/uuid.schema.json"
    }
  }
}

```

/path/to/schemas/uuid.schema.json
```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "UUID",
  "$ref": "#/definitions/uuid",
  "definitions": {
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
    }
  }
}
```

The schema's paths are pulled via the config, via the middleware. So you can use the schemas or if you rather, drop the middleware and no need for schemas.

As I mentioned above, you have the option for individual route middleware as well! 



```js
const routes = [
    {
        id: "resourceGet",
        method: "GET",
        modulePath: "/routes/resource/get.js",
        path: "/resources",
        middleware: "/path/to/your/customMiddleware.js",
    }
]
```

Hope you enjoy it! Please let me know your thoughts!

https://www.npmjs.com/package/@dechamp/express-auto-route