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

241 lines (240 loc) 11.1 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.map = map; /** * Map schema implementation */ const E = __importStar(require("../internal/effect")); const validator_1 = require("../validator"); const errors_1 = require("../errors"); /** * Create a map schema */ function map(keySchema, valueSchema) { const validations = []; let errorMessage = undefined; const schema = { _tag: 'MapSchema', keySchema, valueSchema, minSize: (min, message) => { validations.push((input, options) => input.size >= min ? E.succeed(input) : E.fail(new errors_1.MapValidationError(message || `Map must contain at least ${min} entries`, undefined, options === null || options === void 0 ? void 0 : options.path))); return schema; }, maxSize: (max, message) => { validations.push((input, options) => input.size <= max ? E.succeed(input) : E.fail(new errors_1.MapValidationError(message || `Map must contain at most ${max} entries`, undefined, options === null || options === void 0 ? void 0 : options.path))); return schema; }, size: (size, message) => { validations.push((input, options) => input.size === size ? E.succeed(input) : E.fail(new errors_1.MapValidationError(message || `Map must contain exactly ${size} entries`, undefined, options === null || options === void 0 ? void 0 : options.path))); return schema; }, nonEmpty: (message) => { validations.push((input, options) => input.size > 0 ? E.succeed(input) : E.fail(new errors_1.MapValidationError(message || `Map must not be empty`, undefined, options === null || options === void 0 ? void 0 : options.path))); return schema; }, hasKey: (key, message) => { validations.push((input, options) => { // Simplified key comparison - in practice would need better equals implementation let hasKey = false; input.forEach((_, k) => { if (JSON.stringify(k) === JSON.stringify(key)) { hasKey = true; } }); return hasKey ? E.succeed(input) : E.fail(new errors_1.MapValidationError(message || `Map must contain the specified key`, undefined, options === null || options === void 0 ? void 0 : options.path)); }); return schema; }, hasValue: (value, message) => { validations.push((input, options) => { // Simplified value comparison - in practice would need better equals implementation let hasValue = false; input.forEach((v) => { if (JSON.stringify(v) === JSON.stringify(value)) { hasValue = true; } }); return hasValue ? E.succeed(input) : E.fail(new errors_1.MapValidationError(message || `Map must contain the specified value`, undefined, options === null || options === void 0 ? void 0 : options.path)); }); return schema; }, entries: (entries, message) => { validations.push((input, options) => { // Check if all entries in the provided array exist in the input map for (const [entryKey, entryValue] of entries) { let found = false; input.forEach((v, k) => { if (JSON.stringify(k) === JSON.stringify(entryKey) && JSON.stringify(v) === JSON.stringify(entryValue)) { found = true; } }); if (!found) { return E.fail(new errors_1.MapValidationError(message || `Map must contain all specified entries`, undefined, options === null || options === void 0 ? void 0 : options.path)); } } return E.succeed(input); }); return schema; }, // 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: 'MapSchema', 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) => { // Type validation if (!(input instanceof Map)) { return E.fail(new errors_1.TypeValidationError(errorMessage || 'Value must be a Map', 'Map', typeof input, options === null || options === void 0 ? void 0 : options.path)); } // Validate all keys and values in the map const validatedEntries = []; const keyValidator = keySchema.toValidator(); const valueValidator = valueSchema.toValidator(); input.forEach((value, key) => { const keyPath = (options === null || options === void 0 ? void 0 : options.path) ? [...options.path, '[key]'] : ['[key]']; const valuePath = (options === null || options === void 0 ? void 0 : options.path) ? [...options.path, '*'] : ['*']; // Validate key and value separately, then combine results const keyResult = keyValidator.validate(key, { ...options, path: keyPath }); const valueResult = valueValidator.validate(value, { ...options, path: valuePath }); const entryResult = E.pipe(E.all([keyResult, valueResult]), E.map(([validKey, validValue]) => [validKey, validValue])); validatedEntries.push(entryResult); }); // Handle empty map case if (validatedEntries.length === 0) { // Apply additional validations return validations.reduce((acc, validation) => E.flatMap(acc, val => validation(val, options)), E.succeed(input)); } // Combine validation results return E.pipe(E.all(validatedEntries), E.map(validEntries => new Map(validEntries)), E.flatMap(validMap => validations.reduce((acc, validation) => E.flatMap(acc, val => validation(val, options)), E.succeed(validMap)))); }) }; return schema; }