UNPKG

fireodm

Version:

A basic and extensible ODM for the Firestore Admin SDK in Node.js with decorators, relationships, and validation.

25 lines 980 B
import "reflect-metadata"; import { z } from "zod"; const VALIDATION_KEY = Symbol("validation:properties"); /** * Decorator to attach a Zod schema to a class property. */ export function Validate(schema) { return function (target, propertyKey) { const existing = Reflect.getOwnMetadata(VALIDATION_KEY, target.constructor) || {}; existing[propertyKey] = schema; Reflect.defineMetadata(VALIDATION_KEY, existing, target.constructor); }; } /** * Builds a Zod schema for the class by collecting all @Validate decorators. * Returns a ZodType that parses into the class instance type T. */ export function getValidationSchema(ctor) { const shape = Reflect.getOwnMetadata(VALIDATION_KEY, ctor) || {}; const base = z.object(shape).strict(); // Transform to cast the parsed object into the class instance shape // Cast to ZodSchema<T> to satisfy TS typing return base.transform((obj) => obj); } //# sourceMappingURL=validation.js.map