serviceberry-json-schema
v1.0.1
Published
A JSON schema validator plugin for Serviceberry
Downloads
111
Maintainers
Readme
serviceberry-json-schema
JSON Schema validator plugin for Serviceberry. For more information visit json-schema.org.
Install
npm install serviceberry-json-schema
Usage
This plugin exports a function that creates handlers. To use this
plugin, call this function with a schema object and an optional options
object. The second argument can also be a validator instance.
Validators are objects with a compileAsync
method that returns
a validate
function (such as a Ajv
instance) If an options object is passed, a new Ajv instance is created.
const jsonSchema = require("serviceberry-json-schema");
trunk.use(jsonSchema({
type: "object",
properties: {
firstName: {
type: "string"
},
lastName: {
type: "string"
}
},
required: [
"firstName",
"lastName"
]
}));
async jsonSchema(schema[, param][, options])
schema object
param string
A dot delimited identifier specifying the request parameter to be validated. In the form of "type.name" or "type" where type is one of path, query, header, body, or all or param is the name of an own property of the request object.
Defaults to all which is equivalent to
request.getParams()
. Other examples include: "body" (request.getBody()
), "body.widget" (request.getBodyParam("widget")
), "header.Content-Length" (request.getHeader("Content-Length")
)...options object
async jsonSchema(schema[, param][, validator])
schema object
param string
See above
validator object
Ajv instance or any object that has a
compileAsync
method that takes theschema
and returns avalidate
function.