1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.json = exports.url = exports.port = exports.host = exports.email = exports.str = exports.num = exports.bool = void 0;
|
4 | var errors_1 = require("./errors");
|
5 | var makers_1 = require("./makers");
|
6 |
|
7 | var isFQDN = function (input) {
|
8 | if (!input.length)
|
9 | return false;
|
10 | var parts = input.split('.');
|
11 | for (var part = void 0, i = 0; i < parts.length; i++) {
|
12 | part = parts[i];
|
13 | if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part))
|
14 | return false;
|
15 | if (/[\uff01-\uff5e]/.test(part))
|
16 | return false;
|
17 | if (part[0] === '-' || part[part.length - 1] === '-')
|
18 | return false;
|
19 | }
|
20 | return true;
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 | var ipv4Regex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
|
26 | var ipv6Regex = /([a-f0-9]+:+)+[a-f0-9]+/;
|
27 | var isIP = function (input) {
|
28 | if (!input.length)
|
29 | return false;
|
30 | return ipv4Regex.test(input) || ipv6Regex.test(input);
|
31 | };
|
32 | var EMAIL_REGEX = /^[^@\s]+@[^@\s]+\.[^@\s]+$/;
|
33 |
|
34 |
|
35 | exports.bool = (0, makers_1.makeExactValidator)(function (input) {
|
36 | switch (input) {
|
37 | case true:
|
38 | case 'true':
|
39 | case 't':
|
40 | case '1':
|
41 | return true;
|
42 | case false:
|
43 | case 'false':
|
44 | case 'f':
|
45 | case '0':
|
46 | return false;
|
47 | default:
|
48 | throw new errors_1.EnvError("Invalid bool input: \"".concat(input, "\""));
|
49 | }
|
50 | });
|
51 | exports.num = (0, makers_1.makeValidator)(function (input) {
|
52 | var coerced = parseFloat(input);
|
53 | if (Number.isNaN(coerced))
|
54 | throw new errors_1.EnvError("Invalid number input: \"".concat(input, "\""));
|
55 | return coerced;
|
56 | });
|
57 | exports.str = (0, makers_1.makeValidator)(function (input) {
|
58 | if (typeof input === 'string')
|
59 | return input;
|
60 | throw new errors_1.EnvError("Not a string: \"".concat(input, "\""));
|
61 | });
|
62 | exports.email = (0, makers_1.makeValidator)(function (x) {
|
63 | if (EMAIL_REGEX.test(x))
|
64 | return x;
|
65 | throw new errors_1.EnvError("Invalid email address: \"".concat(x, "\""));
|
66 | });
|
67 | exports.host = (0, makers_1.makeValidator)(function (input) {
|
68 | if (!isFQDN(input) && !isIP(input)) {
|
69 | throw new errors_1.EnvError("Invalid host (domain or ip): \"".concat(input, "\""));
|
70 | }
|
71 | return input;
|
72 | });
|
73 | exports.port = (0, makers_1.makeValidator)(function (input) {
|
74 | var coerced = +input;
|
75 | if (Number.isNaN(coerced) ||
|
76 | "".concat(coerced) !== "".concat(input) ||
|
77 | coerced % 1 !== 0 ||
|
78 | coerced < 1 ||
|
79 | coerced > 65535) {
|
80 | throw new errors_1.EnvError("Invalid port input: \"".concat(input, "\""));
|
81 | }
|
82 | return coerced;
|
83 | });
|
84 | exports.url = (0, makers_1.makeValidator)(function (x) {
|
85 | try {
|
86 | new URL(x);
|
87 | return x;
|
88 | }
|
89 | catch (e) {
|
90 | throw new errors_1.EnvError("Invalid url: \"".concat(x, "\""));
|
91 | }
|
92 | });
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | exports.json = (0, makers_1.makeStructuredValidator)(function (x) {
|
105 | try {
|
106 | return JSON.parse(x);
|
107 | }
|
108 | catch (e) {
|
109 | throw new errors_1.EnvError("Invalid json: \"".concat(x, "\""));
|
110 | }
|
111 | });
|
112 |
|
\ | No newline at end of file |