UNPKG

10 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ValidateObject = exports.SchemaBuilder = void 0;
4var Joi = require("@hapi/joi");
5var itErrors = require("./locale/joi_it");
6/**
7 * SchemaBuilder for the Joi validator.
8 * It exposes some facility methods for some simple task,
9 * and it also allows a complete personalization of the final result.
10 */
11var SchemaBuilder = /** @class */ (function () {
12 function SchemaBuilder() {
13 this.keys = {};
14 }
15 /**
16 * Generate the final Joi Object Schema
17 */
18 SchemaBuilder.prototype.build = function () {
19 return Joi.object().keys(this.keys);
20 };
21 /**
22 * General method, that can be used with the Joi functions
23 */
24 SchemaBuilder.prototype.general = function (key, spec) {
25 this.keys[key] = spec;
26 this.lastKey = key;
27 return this;
28 };
29 /**
30 * Add an optional string to the Schema
31 * @param key the key
32 * @param min minimum length of the string
33 * @param max maximum length of the string
34 * @return the schema builder
35 */
36 SchemaBuilder.prototype.stringOptional = function (key, min, max) {
37 var tmp = Joi.string();
38 if (min !== undefined) {
39 tmp = tmp.min(min);
40 }
41 else {
42 tmp = tmp.allow("");
43 }
44 if (max !== undefined) {
45 tmp = tmp.max(max);
46 }
47 this.keys[key] = tmp;
48 this.lastKey = key;
49 return this;
50 };
51 /**
52 * Add a required string to the Schema
53 * @param key the key
54 * @param min minimum length of the string
55 * @param max maximum length of the string
56 * @return the schema builder
57 */
58 SchemaBuilder.prototype.string = function (key, min, max) {
59 this.stringOptional(key, min, max);
60 this.keys[key] = this.keys[key].required();
61 this.lastKey = key;
62 return this;
63 };
64 /**
65 * Add a required email to the Schema
66 * @param key the key
67 * @return the schema builder
68 */
69 SchemaBuilder.prototype.email = function (key) {
70 this.keys[key] = Joi.string()
71 .email()
72 .required();
73 this.lastKey = key;
74 return this;
75 };
76 /**
77 * Add an optional email to the Schema
78 * @param key the key
79 * @return the schema builder
80 */
81 SchemaBuilder.prototype.emailOptional = function (key) {
82 this.keys[key] = Joi.string().email();
83 this.lastKey = key;
84 return this;
85 };
86 /**
87 * Add a required password to the Schema.
88 * The password will be validated using the following regex:
89 * ^[0-9a-zA-Z\|\!\"\£\$\%\&\/\(\)\=\?\^\,\;\.\:\-\_\\\~]{6,}$
90 * @param key the key
91 * @return the schema builder
92 */
93 SchemaBuilder.prototype.password = function (key) {
94 this.keys[key] = Joi.string()
95 .regex(/^[0-9a-zA-Z\|\!\"\£\$\%\&\/\(\)\=\?\^\,\;\.\:\-\_\\\~]{6,}$/)
96 .required();
97 return this;
98 };
99 /**
100 * Add an optional password to the Schema.
101 * The password will be validated using the following regex:
102 * ^[0-9a-zA-Z\|\!\"\£\$\%\&\/\(\)\=\?\^\,\;\.\:\-\_\\\~]{6,}$
103 * @param key the key
104 * @return the schema builder
105 */
106 SchemaBuilder.prototype.passwordOptional = function (key) {
107 this.keys[key] = Joi.string().regex(/^[0-9a-zA-Z\|\!\"\£\$\%\&\/\(\)\=\?\^\,\;\.\:\-\_\\\~]{6,}$/);
108 return this;
109 };
110 /**
111 * Add an optional number to the Schema
112 * @param key the key
113 * @param min minimum value of the number
114 * @param max maximum value of the number
115 * @return the schema builder
116 */
117 SchemaBuilder.prototype.numberOptional = function (key, min, max) {
118 var tmp = Joi.number();
119 if (min !== undefined) {
120 tmp = tmp.min(min);
121 }
122 if (max !== undefined) {
123 tmp = tmp.max(max);
124 }
125 this.keys[key] = tmp;
126 this.lastKey = key;
127 return this;
128 };
129 /**
130 * Add a required number to the Schema
131 * @param key the key
132 * @param min minimum value of the number
133 * @param max maximum value of the number
134 * @return the schema builder
135 */
136 SchemaBuilder.prototype.number = function (key, min, max) {
137 this.numberOptional(key, min, max);
138 this.keys[key] = this.keys[key].required();
139 this.lastKey = key;
140 return this;
141 };
142 /**
143 * Add an optional integer number to the Schema
144 * @param key the key
145 * @param min minimum value of the number
146 * @param max maximum value of the number
147 * @return the schema builder
148 */
149 SchemaBuilder.prototype.integerOptional = function (key, min, max) {
150 var tmp = Joi.number().integer();
151 if (min !== undefined) {
152 tmp = tmp.min(min);
153 }
154 if (max !== undefined) {
155 tmp = tmp.max(max);
156 }
157 this.keys[key] = tmp;
158 this.lastKey = key;
159 return this;
160 };
161 /**
162 * Add a required integer number to the Schema
163 * @param key the key
164 * @param min minimum value of the number
165 * @param max maximum value of the number
166 * @return the schema builder
167 */
168 SchemaBuilder.prototype.integer = function (key, min, max) {
169 this.integerOptional(key, min, max);
170 this.keys[key] = this.keys[key].required();
171 this.lastKey = key;
172 return this;
173 };
174 /**
175 * Add an optional date to the Schema
176 * @param key the key
177 * @return the schema builder
178 */
179 SchemaBuilder.prototype.dateOptional = function (key) {
180 this.keys[key] = Joi.date();
181 this.lastKey = key;
182 return this;
183 };
184 /**
185 * Add a required date to the Schema
186 * @param key the key
187 * @return the schema builder
188 */
189 SchemaBuilder.prototype.date = function (key) {
190 this.keys[key] = Joi.date().required();
191 this.lastKey = key;
192 return this;
193 };
194 SchemaBuilder.prototype.arrayOfStrings = function (key) {
195 this.keys[key] = Joi.array().items(Joi.string());
196 this.lastKey = key;
197 return this;
198 };
199 SchemaBuilder.prototype.arrayOfStringsOptional = function (key) {
200 this.keys[key] = Joi.array().items(Joi.string());
201 this.lastKey = key;
202 return this;
203 };
204 /**
205 * Add the label to the last added key
206 * @param label the label to use in case of error
207 * @return the schema builder
208 */
209 SchemaBuilder.prototype.withLabel = function (label) {
210 if (this.lastKey) {
211 this.keys[this.lastKey] = this.keys[this.lastKey].label(label);
212 }
213 return this;
214 };
215 return SchemaBuilder;
216}());
217exports.SchemaBuilder = SchemaBuilder;
218/**
219 * This class is used to validate an object using a given schema.
220 * It is used by Lynx to automatically validate the body of any requests, using
221 * the Body decorator.
222 */
223var ValidateObject = /** @class */ (function () {
224 /**
225 * @param obj the object to validate
226 * @param schema the schema
227 * @param locales an array of available language. You can use the `req.acceptsLanguages()`
228 */
229 function ValidateObject(obj, schema, locales) {
230 this._obj = obj;
231 this.schema = schema;
232 var options = null;
233 for (var _i = 0, locales_1 = locales; _i < locales_1.length; _i++) {
234 var locale = locales_1[_i];
235 if (locale.indexOf("en") != -1) {
236 break;
237 }
238 if (locale == "it") {
239 options = {
240 language: itErrors.errors
241 };
242 break;
243 }
244 }
245 this.validate(options);
246 }
247 ValidateObject.prototype.validate = function (options) {
248 this.valid = this.schema.validate(this._obj, options);
249 };
250 Object.defineProperty(ValidateObject.prototype, "isValid", {
251 /**
252 * Verify that the object respect the schema.
253 * @return true if the object is valid, false otherwise.
254 */
255 get: function () {
256 return !this.valid.error;
257 },
258 enumerable: false,
259 configurable: true
260 });
261 Object.defineProperty(ValidateObject.prototype, "obj", {
262 /**
263 * Unwrap the object (can be valid or not!)
264 * @return the unwrapped object
265 */
266 get: function () {
267 return this._obj;
268 },
269 enumerable: false,
270 configurable: true
271 });
272 Object.defineProperty(ValidateObject.prototype, "errors", {
273 /**
274 * Getter that returns an array of validation errors.
275 * @return an array of validation errors. It can not be null.
276 */
277 get: function () {
278 var errors = [];
279 if (this.isValid) {
280 return errors;
281 }
282 if (this.valid.error) {
283 for (var _i = 0, _a = this.valid.error.details; _i < _a.length; _i++) {
284 var err = _a[_i];
285 errors.push({
286 name: err.context && err.context.key ? err.context.key : "",
287 message: err.message
288 });
289 }
290 }
291 return errors;
292 },
293 enumerable: false,
294 configurable: true
295 });
296 Object.defineProperty(ValidateObject.prototype, "errorsMap", {
297 /**
298 * Getter that returns a map of errors. This prop contains the save information
299 * as the `errors` prop, but with a different format.
300 * @return a map or localized errors.
301 */
302 get: function () {
303 var map = {};
304 for (var _i = 0, _a = this.errors; _i < _a.length; _i++) {
305 var err = _a[_i];
306 map[err.name] = err.message;
307 }
308 return map;
309 },
310 enumerable: false,
311 configurable: true
312 });
313 return ValidateObject;
314}());
315exports.ValidateObject = ValidateObject;