UNPKG

veffect

Version:

powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha

295 lines (294 loc) 9.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reduceWithContext = exports.proto = exports.prefixed = exports.isUnsupported = exports.isSourceUnavailable = exports.isOr = exports.isMissingDataOnly = exports.isMissingData = exports.isInvalidData = exports.isConfigError = exports.isAnd = exports.Unsupported = exports.SourceUnavailable = exports.Or = exports.MissingData = exports.InvalidData = exports.ConfigErrorTypeId = exports.And = void 0; var Either = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Either.js")); var _Function = /*#__PURE__*/require("../Function.js"); var _Predicate = /*#__PURE__*/require("../Predicate.js"); var RA = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../ReadonlyArray.js")); var OpCodes = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./opCodes/configError.js")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** @internal */ const ConfigErrorSymbolKey = "effect/ConfigError"; /** @internal */ const ConfigErrorTypeId = exports.ConfigErrorTypeId = /*#__PURE__*/Symbol.for(ConfigErrorSymbolKey); /** @internal */ const proto = exports.proto = { [ConfigErrorTypeId]: ConfigErrorTypeId }; /** @internal */ const And = (self, that) => { const error = Object.create(proto); error._tag = OpCodes.OP_AND; error.left = self; error.right = that; Object.defineProperty(error, "toString", { enumerable: false, value() { return `${this.left} and ${this.right}`; } }); return error; }; /** @internal */ exports.And = And; const Or = (self, that) => { const error = Object.create(proto); error._tag = OpCodes.OP_OR; error.left = self; error.right = that; Object.defineProperty(error, "toString", { enumerable: false, value() { return `${this.left} or ${this.right}`; } }); return error; }; /** @internal */ exports.Or = Or; const InvalidData = (path, message, options = { pathDelim: "." }) => { const error = Object.create(proto); error._tag = OpCodes.OP_INVALID_DATA; error.path = path; error.message = message; Object.defineProperty(error, "toString", { enumerable: false, value() { const path = (0, _Function.pipe)(this.path, RA.join(options.pathDelim)); return `(Invalid data at ${path}: "${this.message}")`; } }); return error; }; /** @internal */ exports.InvalidData = InvalidData; const MissingData = (path, message, options = { pathDelim: "." }) => { const error = Object.create(proto); error._tag = OpCodes.OP_MISSING_DATA; error.path = path; error.message = message; Object.defineProperty(error, "toString", { enumerable: false, value() { const path = (0, _Function.pipe)(this.path, RA.join(options.pathDelim)); return `(Missing data at ${path}: "${this.message}")`; } }); return error; }; /** @internal */ exports.MissingData = MissingData; const SourceUnavailable = (path, message, cause, options = { pathDelim: "." }) => { const error = Object.create(proto); error._tag = OpCodes.OP_SOURCE_UNAVAILABLE; error.path = path; error.message = message; error.cause = cause; Object.defineProperty(error, "toString", { enumerable: false, value() { const path = (0, _Function.pipe)(this.path, RA.join(options.pathDelim)); return `(Source unavailable at ${path}: "${this.message}")`; } }); return error; }; /** @internal */ exports.SourceUnavailable = SourceUnavailable; const Unsupported = (path, message, options = { pathDelim: "." }) => { const error = Object.create(proto); error._tag = OpCodes.OP_UNSUPPORTED; error.path = path; error.message = message; Object.defineProperty(error, "toString", { enumerable: false, value() { const path = (0, _Function.pipe)(this.path, RA.join(options.pathDelim)); return `(Unsupported operation at ${path}: "${this.message}")`; } }); return error; }; /** @internal */ exports.Unsupported = Unsupported; const isConfigError = u => (0, _Predicate.hasProperty)(u, ConfigErrorTypeId); /** @internal */ exports.isConfigError = isConfigError; const isAnd = self => self._tag === OpCodes.OP_AND; /** @internal */ exports.isAnd = isAnd; const isOr = self => self._tag === OpCodes.OP_OR; /** @internal */ exports.isOr = isOr; const isInvalidData = self => self._tag === OpCodes.OP_INVALID_DATA; /** @internal */ exports.isInvalidData = isInvalidData; const isMissingData = self => self._tag === OpCodes.OP_MISSING_DATA; /** @internal */ exports.isMissingData = isMissingData; const isSourceUnavailable = self => self._tag === OpCodes.OP_SOURCE_UNAVAILABLE; /** @internal */ exports.isSourceUnavailable = isSourceUnavailable; const isUnsupported = self => self._tag === OpCodes.OP_UNSUPPORTED; /** @internal */ exports.isUnsupported = isUnsupported; const prefixed = exports.prefixed = /*#__PURE__*/(0, _Function.dual)(2, (self, prefix) => { switch (self._tag) { case OpCodes.OP_AND: { return And(prefixed(self.left, prefix), prefixed(self.right, prefix)); } case OpCodes.OP_OR: { return Or(prefixed(self.left, prefix), prefixed(self.right, prefix)); } case OpCodes.OP_INVALID_DATA: { return InvalidData([...prefix, ...self.path], self.message); } case OpCodes.OP_MISSING_DATA: { return MissingData([...prefix, ...self.path], self.message); } case OpCodes.OP_SOURCE_UNAVAILABLE: { return SourceUnavailable([...prefix, ...self.path], self.message, self.cause); } case OpCodes.OP_UNSUPPORTED: { return Unsupported([...prefix, ...self.path], self.message); } } }); /** @internal */ const IsMissingDataOnlyReducer = { andCase: (_, left, right) => left && right, orCase: (_, left, right) => left && right, invalidDataCase: _Function.constFalse, missingDataCase: _Function.constTrue, sourceUnavailableCase: _Function.constFalse, unsupportedCase: _Function.constFalse }; /** @internal */ const reduceWithContext = exports.reduceWithContext = /*#__PURE__*/(0, _Function.dual)(3, (self, context, reducer) => { const input = [self]; const output = []; while (input.length > 0) { const error = input.pop(); switch (error._tag) { case OpCodes.OP_AND: { input.push(error.right); input.push(error.left); output.push(Either.left({ _tag: "AndCase" })); break; } case OpCodes.OP_OR: { input.push(error.right); input.push(error.left); output.push(Either.left({ _tag: "OrCase" })); break; } case OpCodes.OP_INVALID_DATA: { output.push(Either.right(reducer.invalidDataCase(context, error.path, error.message))); break; } case OpCodes.OP_MISSING_DATA: { output.push(Either.right(reducer.missingDataCase(context, error.path, error.message))); break; } case OpCodes.OP_SOURCE_UNAVAILABLE: { output.push(Either.right(reducer.sourceUnavailableCase(context, error.path, error.message, error.cause))); break; } case OpCodes.OP_UNSUPPORTED: { output.push(Either.right(reducer.unsupportedCase(context, error.path, error.message))); break; } } } const accumulator = []; while (output.length > 0) { const either = output.pop(); switch (either._tag) { case "Left": { switch (either.left._tag) { case "AndCase": { const left = accumulator.pop(); const right = accumulator.pop(); const value = reducer.andCase(context, left, right); accumulator.push(value); break; } case "OrCase": { const left = accumulator.pop(); const right = accumulator.pop(); const value = reducer.orCase(context, left, right); accumulator.push(value); break; } } break; } case "Right": { accumulator.push(either.right); break; } } } if (accumulator.length === 0) { throw new Error("BUG: ConfigError.reduceWithContext - please report an issue at https://github.com/Effect-TS/effect/issues"); } return accumulator.pop(); }); /** @internal */ const isMissingDataOnly = self => reduceWithContext(self, void 0, IsMissingDataOnlyReducer); exports.isMissingDataOnly = isMissingDataOnly; //# sourceMappingURL=configError.js.map