• Creates a middleware, that validates the data of the 'body' property inside the 'request' object with the help of a Ajv/JSON schema.

    Parameters

    Returns UniqueHttpMiddleware

    The new middleware.

    See

    https://www.npmjs.com/package/ajv

    Example

    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)
    }
    })

    // ...
  • Parameters

    Returns UniqueHttpMiddleware

  • Parameters

    Returns UniqueHttpMiddleware

Generated using TypeDoc