1 | 'use strict';
|
2 |
|
3 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
4 |
|
5 | var _tv = require('tv4');
|
6 |
|
7 | var _tv2 = _interopRequireDefault(_tv);
|
8 |
|
9 | var _assert = require('assert');
|
10 |
|
11 | var _assert2 = _interopRequireDefault(_assert);
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | var allowNull = function allowNull(data, schema) {
|
16 | return data === null && (schema.type === 'null' || schema.type.includes('null'));
|
17 | };
|
18 |
|
19 | _tv2.default.addFormat('objectid', function (data, schema) {
|
20 | if (allowNull(data, schema)) return null;
|
21 | if (typeof data === 'string' && /^[a-f\d]{24}$/i.test(data)) return null;
|
22 | return 'objectid expected';
|
23 | });
|
24 |
|
25 | _tv2.default.addFormat('date-time', function (data, schema) {
|
26 | if (allowNull(data, schema)) return null;
|
27 |
|
28 | var dateTimeRegex = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/;
|
29 | if (typeof data === 'string' && dateTimeRegex.test(data)) return null;
|
30 | return 'date-time, ISOString format, expected';
|
31 | });
|
32 |
|
33 | _tv2.default.addFormat('date', function (data, schema) {
|
34 | if (allowNull(data, schema)) return null;
|
35 | if (typeof data === 'string' && /\d{4}-[01]\d-[0-3]\d/.test(data)) return null;
|
36 | return 'date, YYYY-MM-DD format, expected';
|
37 | });
|
38 |
|
39 | _tv2.default.addFormat('time', function (data, schema) {
|
40 | if (allowNull(data, schema)) return null;
|
41 | if (typeof data === 'string' && /^[0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/.test(data)) return null;
|
42 | return 'time, HH:mm:ss or HH:mm format, expected';
|
43 | });
|
44 |
|
45 | _tv2.default.addFormat('email', function (data, schema) {
|
46 | if (allowNull(data, schema)) return null;
|
47 |
|
48 | var emailRegex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
|
49 | if (typeof data === 'string' && emailRegex.test(data)) return null;
|
50 | return 'email expected';
|
51 | });
|
52 |
|
53 | _tv2.default.addFormat('non-negative-integer', function (data, schema) {
|
54 | if (allowNull(data, schema)) return null;
|
55 | if (typeof data === 'string' && /^[0-9]+$/.test(data)) return null;
|
56 | return 'non negative integer expected';
|
57 | });
|
58 |
|
59 | module.exports = _tv2.default;
|
60 | module.exports.assertValid = function (data, schema, errorMessage) {
|
61 | (0, _assert2.default)(data !== undefined, 'data must be defined');
|
62 | (0, _assert2.default)((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) === 'object', 'schema must be an object');
|
63 | if (errorMessage != null) (0, _assert2.default)(typeof errorMessage === 'string', 'errorMessage must be a string');
|
64 | var cleanedData = JSON.parse(JSON.stringify(data));
|
65 |
|
66 | var _tv4$validateResult = _tv2.default.validateResult(cleanedData, schema, null, true),
|
67 | valid = _tv4$validateResult.valid,
|
68 | error = _tv4$validateResult.error;
|
69 |
|
70 | if (!valid) {
|
71 | var message = function () {
|
72 | var result = '';
|
73 | if (errorMessage) result += errorMessage + '; ';
|
74 | result += 'failed';
|
75 | if (schema.title) result += ' "' + schema.title + '"';
|
76 | result += ' schema validation';
|
77 | if (error.schemaPath.length) result += ' at schema path ' + error.schemaPath;
|
78 | if (error.dataPath.length) result += ' for data path ' + error.dataPath;
|
79 | result += '; ' + error.message.toLowerCase();
|
80 | return result;
|
81 | }();
|
82 | error.name = 'SchemaValidationError';
|
83 | error.message = message;
|
84 | error.data = data;
|
85 | error.schema = schema;
|
86 | throw error;
|
87 | }
|
88 | }; |
\ | No newline at end of file |