This module can validate the request's payload and body by using a schema JSON file.
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")
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.
The available properties are the followings:
type
: The type of the element. Valid values arestring
,number
andarray
. Default isstring
.required
: Wether the given element is required or not. Can betrue
orfalse
. Default isfalse
.children
: The sub-elements.enum
: The list of the possible values collected into an array.: The length settings of the element. Can have alength
min
and amax
value.: When therange
type
isnumber
, you can specify a range by addingmin
andmax
.: A regex script that you want to run on the given value.pattern
{
"username": {
"type": "string",
"required": true
}
}
By calling the catch
on the validate
function, you can get the errors.
.catch(err => {
console.log(err) // Output: { "internalIssue": "schemaNotSet" }
})
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.
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
.
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
.
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.
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.
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
})
})
})
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.
Test files are located in the test
directory.
Use the test
npm
script to run the tests:
$ npm run test