UNPKG

3.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.json = exports.url = exports.port = exports.host = exports.email = exports.str = exports.num = exports.bool = void 0;
4var errors_1 = require("./errors");
5var makers_1 = require("./makers");
6// Simplified adaptation of https://github.com/validatorjs/validator.js/blob/master/src/lib/isFQDN.js
7var 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; // disallow full-width chars
17 if (part[0] === '-' || part[part.length - 1] === '-')
18 return false;
19 }
20 return true;
21};
22// "best effort" regex-based IP address check
23// If you want a more exhaustive check, create your own custom validator, perhaps wrapping this
24// implementation (the source of the ipv4 regex below): https://github.com/validatorjs/validator.js/blob/master/src/lib/isIP.js
25var 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])$/;
26var ipv6Regex = /([a-f0-9]+:+)+[a-f0-9]+/;
27var isIP = function (input) {
28 if (!input.length)
29 return false;
30 return ipv4Regex.test(input) || ipv6Regex.test(input);
31};
32var EMAIL_REGEX = /^[^@\s]+@[^@\s]+\.[^@\s]+$/; // intentionally non-exhaustive
33// We use exact validator here because narrowing down to either 'true' or 'false'
34// makes no sense.
35exports.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});
51exports.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});
57exports.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});
62exports.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});
67exports.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});
73exports.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});
84exports.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 * Unless passing a default property, it's recommended that you provide an explicit type parameter
95 * for json validation if you're using TypeScript. Otherwise the output will be typed as `any`.
96 * For example:
97 *
98 * ```ts
99 * cleanEnv({
100 * MY_VAR: json<{ foo: number }>(),
101 * })
102 * ```
103 */
104exports.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//# sourceMappingURL=validators.js.map
\No newline at end of file