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