fireodm
Version:
A basic and extensible ODM for the Firestore Admin SDK in Node.js with decorators, relationships, and validation.
29 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validate = Validate;
exports.getValidationSchema = getValidationSchema;
require("reflect-metadata");
const zod_1 = require("zod");
const VALIDATION_KEY = Symbol("validation:properties");
/**
* Decorator to attach a Zod schema to a class property.
*/
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.
*/
function getValidationSchema(ctor) {
const shape = Reflect.getOwnMetadata(VALIDATION_KEY, ctor) || {};
const base = zod_1.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