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

204 lines (203 loc) 6.42 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.registryUtils = exports.globalRegistry = exports.Registry = void 0; exports.createRegistry = createRegistry; exports.registerSchema = registerSchema; exports.setMetadata = setMetadata; exports.describe = describe; exports.extractExamples = extractExamples; const E = __importStar(require("./internal/effect")); /** * Registry implementation for schema metadata management */ class Registry { constructor() { this.storage = new Map(); } /** * Add a schema to the registry with associated metadata */ add(schema, metadata) { this.storage.set(schema, metadata); return schema; } /** * Check if a schema exists in the registry */ has(schema) { return this.storage.has(schema); } /** * Get metadata associated with a schema */ get(schema) { return this.storage.get(schema); } /** * Remove a schema from the registry */ remove(schema) { return this.storage.delete(schema); } /** * Get all schemas in the registry */ getAllSchemas() { return Array.from(this.storage.keys()); } /** * Get all metadata in the registry */ getAllMetadata() { return Array.from(this.storage.values()); } /** * Get all schemas and their associated metadata */ getAll() { return Array.from(this.storage.entries()); } /** * Find schemas that match a predicate */ find(predicate) { return this.getAll().filter(([schema, metadata]) => predicate(schema, metadata)); } /** * Apply a transformation to all schemas in the registry */ map(mapper) { return this.getAll().map(([schema, metadata]) => mapper(schema, metadata)); } /** * Create an Effect that processes the registry data */ toEffect(effectFn) { return effectFn(this.getAll()); } } exports.Registry = Registry; /** * Create a new registry with the specified metadata type */ function createRegistry() { return new Registry(); } /** * The global registry for all schemas with common metadata */ exports.globalRegistry = createRegistry(); /** * Utility to register a schema in a registry with metadata * This is an alternative to the prototype extension approach */ function registerSchema(schema, registry, metadata) { registry.add(schema, metadata); return schema; } /** * Set metadata for a schema in the global registry */ function setMetadata(schema, metadata) { exports.globalRegistry.add(schema, metadata); return schema; } /** * Set description for a schema in the global registry */ function describe(schema, description) { const existingMeta = exports.globalRegistry.get(schema) || {}; exports.globalRegistry.add(schema, { ...existingMeta, description }); return schema; } /** * Utility to extract examples that match the schema's type */ function extractExamples(schema) { const metadata = exports.globalRegistry.get(schema); if (!metadata || !metadata.examples) return []; return metadata.examples; } /** * Registry utilities for common operations */ exports.registryUtils = { /** * Extract metadata of all schemas and convert to desired format */ generateDocumentation: (registry, formatter) => { return registry.map(formatter); }, /** * Find schemas by tag or property in metadata */ findSchemasByTag: (registry, tag) => { return registry.find((_, metadata) => { return metadata && typeof metadata === 'object' && (metadata.tag === tag || (Array.isArray(metadata.tags) && metadata.tags.includes(tag))); }); }, /** * Registry operations for validation and other tasks */ effects: { /** * Validate all example data in a registry using their corresponding schemas * Returns results directly to hide Effect implementation from users */ validateAllExamples: (registry) => { // Create the effect internally but run it immediately const effect = E.succeed(registry .getAll() .filter(([_, meta]) => Array.isArray(meta.examples) && meta.examples.length > 0) .map(([schema, meta]) => { const validator = schema.toValidator(); const results = (meta.examples || []).map(example => { const result = validator.safeParse(example); return { example, valid: result.success, error: result.success ? undefined : result.error }; }); return { schema, valid: results.every(r => r.valid), results }; })); // Run the effect synchronously and return the result directly to users return E.runSync(effect); } } };