1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ValidateObject = exports.SchemaBuilder = void 0;
|
4 | var Joi = require("@hapi/joi");
|
5 | var itErrors = require("./locale/joi_it");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var SchemaBuilder = (function () {
|
12 | function SchemaBuilder() {
|
13 | this.keys = {};
|
14 | }
|
15 | |
16 |
|
17 |
|
18 | SchemaBuilder.prototype.build = function () {
|
19 | return Joi.object().keys(this.keys);
|
20 | };
|
21 | |
22 |
|
23 |
|
24 | SchemaBuilder.prototype.general = function (key, spec) {
|
25 | this.keys[key] = spec;
|
26 | this.lastKey = key;
|
27 | return this;
|
28 | };
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
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 |
|
66 |
|
67 |
|
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 |
|
78 |
|
79 |
|
80 |
|
81 | SchemaBuilder.prototype.emailOptional = function (key) {
|
82 | this.keys[key] = Joi.string().email();
|
83 | this.lastKey = key;
|
84 | return this;
|
85 | };
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
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 |
|
101 |
|
102 |
|
103 |
|
104 |
|
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 |
|
112 |
|
113 |
|
114 |
|
115 |
|
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 |
|
131 |
|
132 |
|
133 |
|
134 |
|
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 |
|
144 |
|
145 |
|
146 |
|
147 |
|
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 |
|
163 |
|
164 |
|
165 |
|
166 |
|
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 |
|
176 |
|
177 |
|
178 |
|
179 | SchemaBuilder.prototype.dateOptional = function (key) {
|
180 | this.keys[key] = Joi.date();
|
181 | this.lastKey = key;
|
182 | return this;
|
183 | };
|
184 | |
185 |
|
186 |
|
187 |
|
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 |
|
206 |
|
207 |
|
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 | }());
|
217 | exports.SchemaBuilder = SchemaBuilder;
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | var ValidateObject = (function () {
|
224 | |
225 |
|
226 |
|
227 |
|
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 |
|
253 |
|
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 |
|
264 |
|
265 |
|
266 | get: function () {
|
267 | return this._obj;
|
268 | },
|
269 | enumerable: false,
|
270 | configurable: true
|
271 | });
|
272 | Object.defineProperty(ValidateObject.prototype, "errors", {
|
273 | |
274 |
|
275 |
|
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 |
|
299 |
|
300 |
|
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 | }());
|
315 | exports.ValidateObject = ValidateObject;
|