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

243 lines (242 loc) 10.8 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.set = set; /** * Set schema implementation */ const E = __importStar(require("../internal/effect")); const validator_1 = require("../validator"); const errors_1 = require("../errors"); /** * Create a set schema */ function set(elementSchema) { const validations = []; let errorMessage = undefined; const schema = { _tag: 'SetSchema', elementSchema, minSize: (min, message) => { validations.push((input, options) => input.size >= min ? E.succeed(input) : E.fail(new errors_1.SetValidationError(message || `Set must contain at least ${min} elements`, 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.SetValidationError(message || `Set must contain at most ${max} elements`, 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.SetValidationError(message || `Set must contain exactly ${size} elements`, 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.SetValidationError(message || `Set must not be empty`, undefined, options === null || options === void 0 ? void 0 : options.path))); return schema; }, has: (value, message) => { validations.push((input, options) => { // We need a way to check if the set contains the value // This is a simplistic implementation - in practice we'd need a more robust approach let found = false; input.forEach(item => { if (JSON.stringify(item) === JSON.stringify(value)) { found = true; } }); return found ? E.succeed(input) : E.fail(new errors_1.SetValidationError(message || `Set must contain the specified value`, undefined, options === null || options === void 0 ? void 0 : options.path)); }); return schema; }, subset: (superset, message) => { validations.push((input, options) => { // Check if input is a subset of superset let isSubset = true; input.forEach(value => { let found = false; superset.forEach(item => { if (JSON.stringify(item) === JSON.stringify(value)) { found = true; } }); if (!found) { isSubset = false; } }); return isSubset ? E.succeed(input) : E.fail(new errors_1.SetValidationError(message || `Set must be a subset of the specified set`, undefined, options === null || options === void 0 ? void 0 : options.path)); }); return schema; }, superset: (subset, message) => { validations.push((input, options) => { // Check if input is a superset of subset let isSuperset = true; subset.forEach(value => { let found = false; input.forEach(item => { if (JSON.stringify(item) === JSON.stringify(value)) { found = true; } }); if (!found) { isSuperset = false; } }); return isSuperset ? E.succeed(input) : E.fail(new errors_1.SetValidationError(message || `Set must be a superset of the specified set`, undefined, options === null || options === void 0 ? void 0 : options.path)); }); 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: 'SetSchema', 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 Set)) { return E.fail(new errors_1.TypeValidationError(errorMessage || 'Value must be a Set', 'Set', typeof input, options === null || options === void 0 ? void 0 : options.path)); } // Validate all elements in the set const validatedElements = []; const elemValidator = elementSchema.toValidator(); input.forEach(element => { const newPath = (options === null || options === void 0 ? void 0 : options.path) ? [...options.path, '*'] : ['*']; validatedElements.push(elemValidator.validate(element, { ...options, path: newPath })); }); // Handle empty set case if (validatedElements.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(validatedElements), E.map(validElements => new Set(validElements)), E.flatMap(validSet => validations.reduce((acc, validation) => E.flatMap(acc, val => validation(val, options)), E.succeed(validSet)))); }) }; return schema; }