UNPKG

@pesto-io/zod-reify

Version:

Pesto module to intantiate zod schema from Typescript source code as string.

222 lines (220 loc) 7.2 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.reify = exports.InstanceLoader2 = exports.InstanceLoader = void 0; const __1 = require(".."); class ClassA { id; name; address; } class ClassB { id; name; age; } function activator(type) { return new type(); } const classA = activator(ClassA); /** * ///////////////////////////////////// */ class InstanceLoader { context; constructor(context) { this.context = context; } // getInstance(name: string, ...args: any[]) : T { /* getInstance(name: keyof typeof this.context, ...args: any[]) : T { //str: keyof typeof myObj var instance = Object.create(this.context[name].prototype); instance.constructor.apply(instance, args); return <T> instance; } */ getInstance(name, ...args) { //str: keyof typeof myObj var instance = Object.create(this.context[name].prototype); instance.constructor.apply(instance, args); return instance; } } exports.InstanceLoader = InstanceLoader; const myThing = { aLotOfThings: [ { says: `blochain`, is: `bullsh*t`, }, ], }; // var loader = new InstanceLoader<IActivatable>(window); var loader = new InstanceLoader(myThing); var example = loader.getInstance("ClassA"); ///////////////////////////// // None of those below work /// to instantiate a Date const instance = Object.create(Date.prototype); const args = ["1900-01-01"]; const instantiatedDate = instance.constructor.apply(instance, args); const instantiatedDate2 = Object.create(Date.prototype).constructor.apply(Date.prototype, args); console.log(` instantiatedDate is : [${instantiatedDate}]`); console.log(` instantiated Date is : [${new Date("1900-01-01")}]`); console.log(` instantiatedDate2 is : [${instantiatedDate2}]`); class InstanceLoader2 { context; constructor(context) { this.context = context; } getInstance(name, ...args) { let instance = null; if (this.context) { instance = Object.create(this.context[name].prototype); } else if (name == "Date") { instance = Object.create(Date.prototype); } instance.constructor.apply(instance, args); return instance; } } exports.InstanceLoader2 = InstanceLoader2; var loader3 = new InstanceLoader2(); var example2 = loader3.getInstance("Date", args); console.log(` example2 is : [${example2}]`); //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /** * Example with reify.ZodSchemaReifier */ exports.reify = __importStar(require("./../reify")); // const truc = new reify.ZodSchemaReifier(``) // var loader2 = new InstanceLoader<reify.Reifier<z.ZodString>>(reify); // var loader2 = new InstanceLoader<reify.Reifier<z.ZodTypeAny>>(reify); var loader2 = new InstanceLoader(__1.reify); var exampleSchemaReifier = loader2.getInstance("ZodSchemaReifier", `z.string()`); const reifiedSchema = exampleSchemaReifier.reify(); /** * Now I also need to make clear how to develop annotations AOP * https://www.typescriptlang.org/docs/handbook/decorators.html */ /** * --------------------------------------------------- * / Below example requires 2 compiler options: * // "experimentalDecorators": true, // "emitDecoratorMetadata": true * * --------------------------------------------------- */ /** * * @returns */ function first() { console.log("first(): factory evaluated"); return function (target, propertyKey, descriptor) { console.log("first(): called"); }; } /** * * @returns */ function second() { console.log("second(): factory evaluated"); return function (target, propertyKey, descriptor) { console.log("second(): called"); }; } class ExampleClass { method() { } } __decorate([ first(), second(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ExampleClass.prototype, "method", null); /** * Now class decorators */ function reifiable(constructor) { Object.seal(constructor); Object.seal(constructor.prototype); } function sealed(constructor) { Object.seal(constructor); Object.seal(constructor.prototype); } let BugReport = class BugReport { type = "report"; title; constructor(t) { this.title = t; } }; BugReport = __decorate([ sealed, reifiable, __metadata("design:paramtypes", [String]) ], BugReport); function reportableClassDecorator(constructor) { return class extends constructor { reportingURL = "http://www..."; }; } let OtherBugReport = class OtherBugReport { type = "report"; title; constructor(t) { this.title = t; } }; OtherBugReport = __decorate([ reportableClassDecorator, __metadata("design:paramtypes", [String]) ], OtherBugReport); const bug = new BugReport("Needs dark mode"); console.log(bug.title); // Prints "Needs dark mode" console.log(bug.type); // Prints "report" // const what: Rocket = (someC as any).reify() /** * nothing works there: https://dev.to/danywalls/decorators-in-typescript-with-example-part-1-m0f * https://blog.logrocket.com/practical-guide-typescript-decorators/ */ // prints 50