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
129 lines (128 loc) • 4.24 kB
TypeScript
/**
* Registry and metadata system for VEffect
*/
import type { Schema } from './types';
import * as E from './internal/effect';
/**
* Type placeholder for schema input type (for type constraints in metadata)
*/
export type $input = any;
/**
* Type placeholder for schema output type (for type constraints in metadata)
*/
export type $output = any;
/**
* Base interface for the global registry metadata
*/
export interface GlobalMetadata {
id?: string;
title?: string;
description?: string;
examples?: $output[];
[key: string]: unknown;
}
/**
* Registry implementation for schema metadata management
*/
export declare class Registry<TMeta, TSchema extends Schema<any, any> = Schema<any, any>> {
private storage;
/**
* Add a schema to the registry with associated metadata
*/
add(schema: TSchema, metadata: TMeta): TSchema;
/**
* Check if a schema exists in the registry
*/
has(schema: TSchema): boolean;
/**
* Get metadata associated with a schema
*/
get(schema: TSchema): TMeta | undefined;
/**
* Remove a schema from the registry
*/
remove(schema: TSchema): boolean;
/**
* Get all schemas in the registry
*/
getAllSchemas(): TSchema[];
/**
* Get all metadata in the registry
*/
getAllMetadata(): TMeta[];
/**
* Get all schemas and their associated metadata
*/
getAll(): Array<[TSchema, TMeta]>;
/**
* Find schemas that match a predicate
*/
find(predicate: (schema: TSchema, metadata: TMeta) => boolean): Array<[TSchema, TMeta]>;
/**
* Apply a transformation to all schemas in the registry
*/
map<R>(mapper: (schema: TSchema, metadata: TMeta) => R): R[];
/**
* Create an Effect that processes the registry data
*/
toEffect<R, E = never>(effectFn: (schemas: Array<[TSchema, TMeta]>) => E.Effect<R, E>): E.Effect<R, E>;
}
/**
* Create a new registry with the specified metadata type
*/
export declare function createRegistry<TMeta, TSchema extends Schema<any, any> = Schema<any, any>>(): Registry<TMeta, TSchema>;
/**
* The global registry for all schemas with common metadata
*/
export declare const globalRegistry: Registry<GlobalMetadata, Schema<any, any>>;
/**
* Utility to register a schema in a registry with metadata
* This is an alternative to the prototype extension approach
*/
export declare function registerSchema<TMeta, TSchema extends Schema<any, any>>(schema: TSchema, registry: Registry<TMeta, TSchema>, metadata: TMeta): TSchema;
/**
* Set metadata for a schema in the global registry
*/
export declare function setMetadata<TSchema extends Schema<any, any>>(schema: TSchema, metadata: GlobalMetadata): TSchema;
/**
* Set description for a schema in the global registry
*/
export declare function describe<TSchema extends Schema<any, any>>(schema: TSchema, description: string): TSchema;
/**
* Utility to extract examples that match the schema's type
*/
export declare function extractExamples<T, I>(schema: Schema<T, I>): T[];
/**
* Registry utilities for common operations
*/
export declare const registryUtils: {
/**
* Extract metadata of all schemas and convert to desired format
*/
generateDocumentation: <TOutput>(registry: Registry<GlobalMetadata>, formatter: (schema: Schema<any, any>, metadata: GlobalMetadata) => TOutput) => TOutput[];
/**
* Find schemas by tag or property in metadata
*/
findSchemasByTag: (registry: Registry<GlobalMetadata>, tag: string) => Array<[Schema<any, any>, GlobalMetadata]>;
/**
* 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: <TMeta extends {
examples?: any[];
}>(registry: Registry<TMeta>) => Array<{
schema: Schema<any, any>;
valid: boolean;
results?: Array<{
example: any;
valid: boolean;
error?: any;
}>;
error?: any;
}>;
};
};