UNPKG

2.83 kBTypeScriptView Raw
1/**
2 * This file is part of the @egodigital/egoose distribution.
3 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
4 *
5 * @egodigital/egoose is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation, version 3.
8 *
9 * @egodigital/egoose is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17import * as express from 'express';
18import * as joi from 'joi';
19import { OptionsJson } from 'body-parser';
20/**
21 * Options for 'jsonObject()' function.
22 */
23export interface JsonObjectOptions {
24 /**
25 * Indicates if input body can be (null) or not.
26 */
27 canBeNull?: boolean;
28 /**
29 * Indicates if input body can be (undefined) or not.
30 */
31 canBeUndefined?: boolean;
32 /**
33 * A custom function, that handles a failed validation.
34 */
35 failedHandler?: JsonObjectValidationFailedHandler;
36 /**
37 * Custom options for the Express json() middleware.
38 */
39 options?: OptionsJson;
40 /**
41 * The optional schema to use.
42 */
43 schema?: joi.ObjectSchema;
44}
45/**
46 * A function that returns the response for a failed JSON validation.
47 *
48 * @param {JsonObjectValidationFailedHandlerContext} context The context.
49 *
50 * @return {express.Response|PromiseLike<express.Response>} The result with the (new) response context.
51 */
52export declare type JsonObjectValidationFailedHandler = (context: JsonObjectValidationFailedHandlerContext) => (express.Response | PromiseLike<express.Response>);
53/**
54 * Context of a 'JsonValidationFailedHandler'.
55 */
56export interface JsonObjectValidationFailedHandlerContext {
57 /**
58 * The original value of the request body.
59 */
60 body: any;
61 /**
62 * An object or value, whichs contains the validation error details.
63 */
64 details: any;
65 /**
66 * An object or value, which represents an ID, that describes the reason.
67 */
68 reason: any;
69 /**
70 * The current HTTP request context.
71 */
72 request: express.Request;
73 /**
74 * The current HTTP response context.
75 */
76 response: express.Response;
77}
78/**
79 * Creates Express middlewares for validating JSON input.
80 *
81 * @param {JsonObjectOptions|joi.ObjectSchema} [optsOrSchema] Custom options or schema.
82 *
83 * @return {express.RequestHandler[]} The created handler(s).
84 */
85export declare function jsonObject(optsOrSchema?: JsonObjectOptions | joi.ObjectSchema): express.RequestHandler[];