UNPKG

3.49 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.addFormats = exports.formats = void 0;
7const convict_1 = require("convict");
8const datemath_parser_1 = __importDefault(require("datemath-parser"));
9const utils_1 = require("@terascope/utils");
10exports.formats = [
11 {
12 name: 'required_String',
13 validate(val) {
14 if (!val || !utils_1.isString(val)) {
15 throw new Error('This field is required and must by of type string');
16 }
17 },
18 coerce(val) {
19 return val;
20 },
21 },
22 {
23 name: 'optional_String',
24 validate(val) {
25 if (!val) {
26 return;
27 }
28 if (utils_1.isString(val)) {
29 return;
30 }
31 throw new Error('This field is optional but if specified it must be of type string');
32 },
33 coerce(val) {
34 return val;
35 },
36 },
37 {
38 name: 'optional_Date',
39 validate(val) {
40 if (!val) {
41 return;
42 }
43 if (utils_1.isString(val) || utils_1.isInteger(val)) {
44 if (utils_1.isValidDate(val)) {
45 return;
46 }
47 try {
48 datemath_parser_1.default.parse(val);
49 }
50 catch (err) {
51 throw new Error(`value: "${val}" cannot be coerced into a proper date
52 the error: ${err.stack}`);
53 }
54 }
55 else {
56 throw new Error('parameter must be a string or number IF specified');
57 }
58 },
59 coerce(val) {
60 return val;
61 },
62 },
63 {
64 name: 'elasticsearch_Name',
65 validate(val) {
66 if (val.length > 255) {
67 throw new Error(`value: ${val} should not exceed 255 characters`);
68 }
69 if (utils_1.startsWith(val, '_')
70 || utils_1.startsWith(val, '-')
71 || utils_1.startsWith(val, '+')) {
72 throw new Error(`value: ${val} should not start with _, -, or +`);
73 }
74 if (val === '.' || val === '..') {
75 throw new Error(`value: ${val} should not equal . or ..`);
76 }
77 // NOTE: the \\\\ is necessary to match a single \ in this case
78 const badChar = new RegExp('[#*?"<>|/\\\\]');
79 if (badChar.test(val)) {
80 throw new Error(`value: ${val} should not contain any invalid characters: #*?"<>|/\\`);
81 }
82 const upperRE = new RegExp('[A-Z]');
83 if (upperRE.test(val)) {
84 throw new Error(`value: ${val} should be lower case`);
85 }
86 },
87 coerce(val) {
88 return val;
89 },
90 },
91 {
92 name: 'positive_int',
93 validate(val) {
94 const int = utils_1.toInteger(val);
95 if (int === false || int < 1) {
96 throw new Error('must be valid integer greater than zero');
97 }
98 },
99 coerce(val) {
100 return utils_1.toInteger(val) || 0;
101 },
102 },
103];
104function addFormats() {
105 exports.formats.forEach(convict_1.addFormat);
106}
107exports.addFormats = addFormats;
108addFormats();
109//# sourceMappingURL=formats.js.map
\No newline at end of file