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 🙏

© 2026 – Pkg Stats / Ryan Hefner

goabela-schema-validator

v0.1.10

Published

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

Readme

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.

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

  • [x] Migrating the script from my app into a single node package.
  • [ ] Creating all the features stated above.
  • [ ] Migrating the validator core loops into iterators.

License

MIT