typed-environment-loader
Version:
typed-environment-loader is a lightweight utility for loading environment variables in a typed manner, ensuring type safety and consistency in your Node.js applications.
60 lines (59 loc) • 3.24 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberParser = void 0;
var errors_1 = require("../errors");
var base_parser_1 = require("./base-parser");
var NumberParser = (function (_super) {
__extends(NumberParser, _super);
function NumberParser() {
return _super !== null && _super.apply(this, arguments) || this;
}
NumberParser.prototype.parse = function (context) {
this._debug.info("Parsing value for key: ".concat(context.envKey, ", raw value: ").concat(context.value));
var value = this.removeQuotes(context.value);
this._debug.info("Value after removing quotes: ".concat(value));
if (value === 'NaN') {
this._debug.error("Invalid number for key: ".concat(context.envKey, ", value is NaN"));
throw new errors_1.EnvironmentValidationError(context.envKey, 'Invalid number', context.path);
}
var num = Number(value);
if (isNaN(num)) {
this._debug.error("Invalid number for key: ".concat(context.envKey, ", value: ").concat(value));
throw new errors_1.EnvironmentValidationError(context.envKey, 'Invalid number', context.path);
}
var numberSchema = context.schema;
this.validate(num, numberSchema, context);
this._debug.info("Parsed number for key: ".concat(context.envKey, ", value: ").concat(num));
return { value: num };
};
NumberParser.prototype.validate = function (value, schema, context) {
if (schema.min !== undefined && value < schema.min) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Value (".concat(value, ") is less than minimum (").concat(schema.min, ")"), context.path);
}
if (schema.max !== undefined && value > schema.max) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Value (".concat(value, ") exceeds maximum (").concat(schema.max, ")"), context.path);
}
if (schema.integer === true && !Number.isInteger(value)) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Value (".concat(value, ") must be an integer"), context.path);
}
this.runCustomValidator(schema.validator, value, context);
this._debug.trace("Validated number for key: ".concat(context.envKey, ", value: ").concat(value));
};
return NumberParser;
}(base_parser_1.BaseParser));
exports.NumberParser = NumberParser;