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

105 lines (104 loc) 4.09 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.pattern = pattern; exports.invalid = invalid; const E = __importStar(require("../internal/effect")); const errors_1 = require("../errors"); /** * Creates a schema that validates the input based on a pattern matching function * The pattern function can return different schemas based on runtime inspection of the input * * @param patternFn A function that takes the input and returns an appropriate schema * @returns A schema that validates using dynamic schema selection based on input * * @example * const responseSchema = pattern(input => { * if (input.status === 'success') return object({ status: literal('success'), data: any() }); * if (input.status === 'error') return object({ status: literal('error'), message: string() }); * return invalid('Unknown response type'); * }); */ function pattern(patternFn) { const schema = { _tag: 'PatternSchema', toValidator: () => ({ validate: (input, options) => { // Get the appropriate schema based on the input const result = patternFn(input); // Handle case where pattern function returned an invalid result if (isInvalidResult(result)) { return E.fail(new errors_1.CustomValidationError(result.message, options === null || options === void 0 ? void 0 : options.path)); } // Validate using the returned schema const validator = result.toValidator(); return validator.validate(input, options); }, parse: (input) => { const result = E.runSync(E.either(schema.toValidator().validate(input))); if (E.isLeft(result)) { throw result.left; } return result.right; }, safeParse: (input) => { const result = E.runSync(E.either(schema.toValidator().validate(input))); if (E.isLeft(result)) { return { success: false, error: result.left }; } return { success: true, data: result.right }; }, validateAsync: async (input, options) => { return E.unwrapEither(E.runSync(E.either(schema.toValidator().validate(input, options)))); } }) }; return schema; } /** * Helper to create an invalid result with custom message */ function invalid(message) { return { _type: 'invalid', message }; } /** * Type guard for InvalidResult */ function isInvalidResult(value) { return value && value._type === 'invalid'; }