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

209 lines (208 loc) 9.73 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.object = object; /** * Object schema implementation */ const E = __importStar(require("../internal/effect")); const EffectMod = __importStar(require("effect/Effect")); const validator_1 = require("../validator"); const errors_1 = require("../errors"); /** * Create an object schema */ function object(properties) { const schema = { _tag: 'ObjectSchema', properties, // Refinement implementation refine: (refinement, message) => { // Check if the refinement is async const testIsAsync = refinement({}) instanceof Promise; if (testIsAsync) { // For async refinements, return a special schema return { ...schema, toValidator: () => { const baseValidator = schema.toValidator(); return { ...baseValidator, validateAsync: async (input, options) => { // First validate using the base object validator const validatedInput = await baseValidator.validateAsync(input, options); // Then apply the async refinement try { const isValid = await refinement(validatedInput); if (!isValid) { throw { _tag: 'RefinementValidationError', message: typeof message === 'function' ? message(validatedInput) : message || 'Failed refinement', path: options === null || options === void 0 ? void 0 : options.path }; } return validatedInput; } catch (error) { if (error && typeof error === 'object' && '_tag' in error) { throw error; } throw { _tag: 'RefinementValidationError', message: error instanceof Error ? error.message : 'Failed refinement', path: options === null || options === void 0 ? void 0 : options.path }; } } }; } }; } // For synchronous refinements, use the standard approach return { ...schema, toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { const baseValidator = schema.toValidator(); return E.pipe(baseValidator.validate(input, options), E.flatMap(value => refinement(value) ? E.succeed(value) : E.fail({ _tag: 'RefinementValidationError', message: typeof message === 'function' ? message(value) : message || 'Failed refinement', path: options === null || options === void 0 ? void 0 : options.path }))); }) }; }, // 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) => { return { ...schema, toValidator: () => (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); }) }; }, // Nullable implementation nullable: () => { return { ...schema, 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); }) }; }, toValidator: () => (0, validator_1.createEffectValidator)((input, options) => { if (typeof input !== 'object' || input === null || Array.isArray(input)) { return E.fail(new errors_1.TypeValidationError('Value must be an object', 'object', typeof input, options === null || options === void 0 ? void 0 : options.path)); } // Check for discriminating field (typically "type") if (Object.prototype.hasOwnProperty.call(properties, 'type') && properties.type._tag === 'LiteralSchema') { const typeSchema = properties.type; const typeValidator = typeSchema.toValidator(); const typeResult = EffectMod.runSync(EffectMod.either(typeValidator.validate(input.type, options))); if (E.isLeft(typeResult)) { return E.fail(new errors_1.ObjectValidationError(`Invalid discriminated union: type field does not match expected value`, [typeResult.left], options === null || options === void 0 ? void 0 : options.path)); } } const validationResults = []; for (const key in properties) { if (Object.prototype.hasOwnProperty.call(properties, key)) { const path = (options === null || options === void 0 ? void 0 : options.path) ? [...options.path, key] : [key]; const value = input[key]; const validator = properties[key].toValidator(); const validation = E.map(validator.validate(value, { ...options, path }), validValue => [key, validValue]); validationResults.push(validation); } } return E.pipe(E.all(validationResults, { concurrency: (options === null || options === void 0 ? void 0 : options.stopOnFirstError) ? 1 : 'unbounded' }), E.map(entries => { const result = {}; for (const [key, value] of entries) { result[key] = value; } return result; })); }) }; return schema; }