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.
51 lines (50 loc) • 2.64 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.StringParser = void 0;
var errors_1 = require("../errors");
var base_parser_1 = require("./base-parser");
var StringParser = (function (_super) {
__extends(StringParser, _super);
function StringParser() {
return _super !== null && _super.apply(this, arguments) || this;
}
StringParser.prototype.parse = function (context) {
var value = this.removeQuotes(context.value);
var stringSchema = context.schema;
this.validate(value, stringSchema, context);
return { value: value };
};
StringParser.prototype.validate = function (value, schema, context) {
if (schema.minLength !== undefined && value.length < schema.minLength) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Value length (".concat(value.length, ") is less than minimum length (").concat(schema.minLength, ")"), context.path);
}
if (schema.maxLength !== undefined && value.length > schema.maxLength) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Value length (".concat(value.length, ") exceeds maximum length (").concat(schema.maxLength, ")"), context.path);
}
if (schema.pattern) {
var pattern = schema.pattern instanceof RegExp ? schema.pattern : new RegExp(schema.pattern);
if (!pattern.test(value)) {
throw new errors_1.EnvironmentValidationError(context.envKey, "Value does not match pattern: ".concat(pattern), context.path);
}
}
this.runCustomValidator(schema.validator, value, context);
this._debug.trace("Validated string for key: ".concat(context.envKey, ", value: ").concat(value));
};
return StringParser;
}(base_parser_1.BaseParser));
exports.StringParser = StringParser;