UNPKG

5.16 kBTypeScriptView Raw
1import * as Joi from "@hapi/joi";
2/**
3 * SchemaBuilder for the Joi validator.
4 * It exposes some facility methods for some simple task,
5 * and it also allows a complete personalization of the final result.
6 */
7export declare class SchemaBuilder {
8 keys: any;
9 lastKey: string;
10 constructor();
11 /**
12 * Generate the final Joi Object Schema
13 */
14 build(): Joi.ObjectSchema;
15 /**
16 * General method, that can be used with the Joi functions
17 */
18 general(key: string, spec: any): SchemaBuilder;
19 /**
20 * Add an optional string to the Schema
21 * @param key the key
22 * @param min minimum length of the string
23 * @param max maximum length of the string
24 * @return the schema builder
25 */
26 stringOptional(key: string, min?: number, max?: number): SchemaBuilder;
27 /**
28 * Add a required string to the Schema
29 * @param key the key
30 * @param min minimum length of the string
31 * @param max maximum length of the string
32 * @return the schema builder
33 */
34 string(key: string, min?: number, max?: number): SchemaBuilder;
35 /**
36 * Add a required email to the Schema
37 * @param key the key
38 * @return the schema builder
39 */
40 email(key: string): SchemaBuilder;
41 /**
42 * Add an optional email to the Schema
43 * @param key the key
44 * @return the schema builder
45 */
46 emailOptional(key: string): SchemaBuilder;
47 /**
48 * Add a required password to the Schema.
49 * The password will be validated using the following regex:
50 * ^[0-9a-zA-Z\|\!\"\£\$\%\&\/\(\)\=\?\^\,\;\.\:\-\_\\\~]{6,}$
51 * @param key the key
52 * @return the schema builder
53 */
54 password(key: string): SchemaBuilder;
55 /**
56 * Add an optional password to the Schema.
57 * The password will be validated using the following regex:
58 * ^[0-9a-zA-Z\|\!\"\£\$\%\&\/\(\)\=\?\^\,\;\.\:\-\_\\\~]{6,}$
59 * @param key the key
60 * @return the schema builder
61 */
62 passwordOptional(key: string): SchemaBuilder;
63 /**
64 * Add an optional number to the Schema
65 * @param key the key
66 * @param min minimum value of the number
67 * @param max maximum value of the number
68 * @return the schema builder
69 */
70 numberOptional(key: string, min?: number, max?: number): SchemaBuilder;
71 /**
72 * Add a required number to the Schema
73 * @param key the key
74 * @param min minimum value of the number
75 * @param max maximum value of the number
76 * @return the schema builder
77 */
78 number(key: string, min?: number, max?: number): SchemaBuilder;
79 /**
80 * Add an optional integer number to the Schema
81 * @param key the key
82 * @param min minimum value of the number
83 * @param max maximum value of the number
84 * @return the schema builder
85 */
86 integerOptional(key: string, min?: number, max?: number): SchemaBuilder;
87 /**
88 * Add a required integer number to the Schema
89 * @param key the key
90 * @param min minimum value of the number
91 * @param max maximum value of the number
92 * @return the schema builder
93 */
94 integer(key: string, min?: number, max?: number): SchemaBuilder;
95 /**
96 * Add an optional date to the Schema
97 * @param key the key
98 * @return the schema builder
99 */
100 dateOptional(key: string): SchemaBuilder;
101 /**
102 * Add a required date to the Schema
103 * @param key the key
104 * @return the schema builder
105 */
106 date(key: string): SchemaBuilder;
107 arrayOfStrings(key: string): SchemaBuilder;
108 arrayOfStringsOptional(key: string): SchemaBuilder;
109 /**
110 * Add the label to the last added key
111 * @param label the label to use in case of error
112 * @return the schema builder
113 */
114 withLabel(label: string): SchemaBuilder;
115}
116/**
117 * Contains the name of the error and a localized error message.
118 */
119export interface ValidationError {
120 name: string;
121 message: string;
122}
123/**
124 * This class is used to validate an object using a given schema.
125 * It is used by Lynx to automatically validate the body of any requests, using
126 * the Body decorator.
127 */
128export declare class ValidateObject<T> {
129 private _obj;
130 private schema;
131 private valid;
132 /**
133 * @param obj the object to validate
134 * @param schema the schema
135 * @param locales an array of available language. You can use the `req.acceptsLanguages()`
136 */
137 constructor(obj: any, schema: Joi.Schema, locales: string[]);
138 private validate;
139 /**
140 * Verify that the object respect the schema.
141 * @return true if the object is valid, false otherwise.
142 */
143 get isValid(): boolean;
144 /**
145 * Unwrap the object (can be valid or not!)
146 * @return the unwrapped object
147 */
148 get obj(): T;
149 /**
150 * Getter that returns an array of validation errors.
151 * @return an array of validation errors. It can not be null.
152 */
153 get errors(): ValidationError[];
154 /**
155 * Getter that returns a map of errors. This prop contains the save information
156 * as the `errors` prop, but with a different format.
157 * @return a map or localized errors.
158 */
159 get errorsMap(): any;
160}