1 | 'use strict';
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const convict_1 = require("convict");
|
7 |
|
8 | const datemath_parser_1 = __importDefault(require("datemath-parser"));
|
9 | const utils_1 = require("@terascope/utils");
|
10 | exports.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 |
|
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 | function addFormats() {
|
93 | exports.formats.forEach(convict_1.addFormat);
|
94 | }
|
95 | exports.addFormats = addFormats;
|
96 | addFormats();
|
97 |
|
\ | No newline at end of file |