Skip to content

NodeJS request body schema validator middleware.

License

Notifications You must be signed in to change notification settings

belaarany/schema-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validating request payload and body

npm version

This module can validate the request's payload and body by using a schema JSON file.

Installation

Install it by using the npm install command in the node shell:

$ npm install goabela-schema-validator


Import the module in your .js file:

var schemaValidator = require("goabela-schema-validator")

Features

Validate

Method: schemaValidator.validate().

The validate method will do the validation. It will return a promise so you can call then and catch on the method. Required arguements are the body/payload as an object and the schema object.

First you have to create a schema object. A good solution could be that creating a schema file such as schema.json.

After that in the NodeJS file (e.g. under the Express controller), you can call the validate method with the schema and the body object.

The prototype of the method:

schemaValidator.validate(body, schema)
  .then(() => {
    // The body is OK
  })
  .catch(err => {
    // The body is invalid
  })

The err variable will contain an object with all the errors that the body has. You can check the possible errors at the errors section.

For a full example please check the example section.

Schema structure

Properties

The available properties are the followings:

  • type: The type of the element. Valid values are string, number and array. Default is string.
  • required: Wether the given element is required or not. Can be true or false. Default is false.
  • children: The sub-elements.
  • enum: The list of the possible values collected into an array.
  • length: The length settings of the element. Can have a min and a max value.
  • range: When the type is number, you can specify a range by adding min and max.
  • pattern: A regex script that you want to run on the given value.

Prototype

{
  "username": {
    "type": "string",
    "required": true
  }
}

Errors

By calling the catch on the validate function, you can get the errors.

.catch(err => {
  console.log(err) // Output: { "internalIssue": "schemaNotSet" }
})

Internal errors

Error key: internalIssue.

The internal issues can be the followings:

  • schemaNotSet: The schema has not been set properly.
  • bodyNotSet: The body has not been set properly.
  • bodyNotEmpty: The schema is empty which means no body should have been received, but it is not empty.

Missing keys

Error key: missingKeys.

If there are any keys which are required but the body has no such key, then this error will be returned.

It will be an array which contains the missing keys with all its parents, for instance born.date.year.

Invalid keys

Error key: invalidKeys.

If there are any keys in the body which are not in the schema, then this error will be returned.

It will be an array which contains the invalid keys with all its parents, for instance born.date.foo.

Invalid types

Error key: invalidTypes.

This error object will be returned once there are any type errors.

For instance, in the schema if you set type of year to be number, and the body's key has different type, then this error will be returned.

Out of enum

Error key: outOfEnum.

If you set an enum on a property, the validator will force the body's value to be one of the enum's value.

Example

The schema.json file:

{
  "userId": {
    "type": "string",
    "required": true
  },
  "userRole": {
    "type": "string",
    "enum": ["owner", "writer"]
  }
}


The app.js file:

// Other part of the app.js

var schemaValidator = require("goabela-schema-validator")
var bodySchema = require("./schema.json")

app.post("/user", (req, res) => {
  schemaValidator.validate(req.body, bodySchema)
    .then(() => {
      // Do something
    })
    .catch(err => {
      res.json({
        "message": "The request body is not correct.",
        "errors": err
      })
    })
})

TO-DOs

  • Migrating the script from my app into a single node package. ✔️
  • Deploying unit-testing. ✔️
  • Creating all the features stated above.
  • Migrating the validator core loops into iterators.

Tests

Test files are located in the test directory.

Use the test npm script to run the tests:

$ npm run test

License

MIT

About

NodeJS request body schema validator middleware.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published