The schema to use.
The new middleware.
https://www.npmjs.com/package/ajv
import assert from 'assert'
import createServer, { IHttpRequest, IHttpResponse, json, JSONSchema7, validateAjv } from '@egomobile/http-server'
interface IMySchema {
email: string;
name?: string;
}
// by default, following settings will be auto-set, if not defined:
//
// - `mySchema.additionalProperties` === `false`
const mySchema: JSONSchema7 = {
type: "object",
required: ["email"],
properties: {
email: {
type: "string",
pattern: "^(([^<>()[\\].,;:\\s@\"]+(\\.[^<>()[\\].,;:\\s@\"]+)*)|(\".+\"))@(([^<>()[\\].,;:\\s@\"]+\\.)+[^<>()[\\].,;:\\s@\"]{2,})$"
},
name: {
type: "string",
minLength: 1,
pattern: "^(\\S+)(.*)(\\S*)$"
}
}
};
const app = createServer()
app.post('/', [json(), validateAjv(mySchema)], (request, response) => {
const body: IMySchema = request.body
assert.strictEqual(typeof body, 'object')
assert.strictEqual(typeof body.email, 'string')
if (typeof body.name !== 'undefined') {
assert.strictEqual(typeof body.name, 'string')
assert.strictEqual(body.name.length > 0, true)
}
})
// ...
Generated using TypeDoc
Creates a middleware, that validates the data of the 'body' property inside the 'request' object with the help of a Ajv/JSON schema.