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

180 lines (179 loc) 7.41 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.intersection = intersection; /** * Intersection schema implementation */ const E = __importStar(require("../internal/effect")); const validator_1 = require("../validator"); const errors_1 = require("../errors"); /** * Creates an intersection schema that combines multiple schemas. * The resulting schema validates that the input satisfies all the provided schemas. */ function intersection(schemas) { let errorMessage = undefined; const validations = []; const schema = { _tag: 'IntersectionSchema', schemas, // Refinement implementation refine: (refinement, message) => { validations.push((input, options) => refinement(input) ? E.succeed(input) : E.fail({ _tag: 'RefinementValidationError', message: typeof message === 'function' ? message(input) : message || 'Failed refinement', path: options === null || options === void 0 ? void 0 : options.path })); return schema; }, // Predicate implementation (alias for refine) predicate: (predicate, message) => { return schema.refine(predicate, message); }, // Transform implementation transform: (transformer) => { return { _tag: 'TransformedSchema', toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { return E.pipe(schema.toValidator().validate(input, options), E.map(value => transformer(value))); }) }; }, // Default value implementation default: (defaultValue) => { // Create a default validator function const defaultValidator = (0, validator_1.createEffectValidator)((input, options) => { if (input === undefined) { const value = typeof defaultValue === 'function' ? defaultValue() : defaultValue; return E.succeed(value); } return schema.toValidator().validate(input, options); }); // Return a new schema with all properties preserved return { ...schema, _tag: 'IntersectionSchema', toValidator: () => defaultValidator }; }, // Nullable implementation nullable: () => { return { _tag: 'NullableSchema', toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { if (input === null) { return E.succeed(null); } return schema.toValidator().validate(input, options); }) }; }, // Optional implementation optional: () => { return { _tag: 'OptionalSchema', toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { if (input === undefined) { return E.succeed(undefined); } return schema.toValidator().validate(input, options); }) }; }, // Nullish implementation nullish: () => { return { _tag: 'NullishSchema', toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { if (input === null || input === undefined) { return E.succeed(input); } return schema.toValidator().validate(input, options); }) }; }, // Custom error message error: (message) => { errorMessage = message; return schema; }, toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { // Collect validated results and errors separately const validatedResults = []; const errors = []; // Validate each schema using either for (const schemaItem of schemas) { const eitherResult = E.either(schemaItem.toValidator().validate(input, options)); // We then run this synchronously to collect results const result = E.runSync(eitherResult); if (E.isRight(result)) { validatedResults.push(result.right); } else { errors.push(result.left); } } // If we have errors, return them if (errors.length > 0) { return E.fail(new errors_1.IntersectionValidationError(errorMessage || 'Intersection validation failed', errors, options === null || options === void 0 ? void 0 : options.path)); } // Merge the valid results together let mergedResult; // Merge the objects - for primitive values, the last one wins if (typeof input !== 'object' || input === null) { mergedResult = validatedResults[validatedResults.length - 1]; } else { // For objects, create a merged result mergedResult = validatedResults.reduce((merged, current) => { if (typeof current !== 'object' || current === null) { return merged; } return { ...merged, ...current }; }, {}); } // Apply additional validations from refine() calls return validations.reduce((acc, validation) => E.flatMap(acc, val => validation(val, options)), E.succeed(mergedResult)); }) }; return schema; }