The schema to use.
The new middleware.
https://www.npmjs.com/package/joi
import assert from 'assert'
import createServer, { IHttpRequest, IHttpResponse, json, schema, validate } from '@egomobile/http-server'
interface IMySchema {
email: string;
name?: string;
}
const mySchema = schema.object({
email: schema.string().strict().trim().email().required(),
name: schema.string().strict().trim().min(1).optional()
})
const app = createServer()
app.post('/', [json(), validate(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 joi schema.