Skip to content

Commit

Permalink
from zod schema
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shutov committed Nov 19, 2023
1 parent 91d8af0 commit 06d9afc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
/log/
*.log
*.pem
*.done
.DS_Store
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
/log/
*.log
*.pem
*.done
.DS_Store
13 changes: 7 additions & 6 deletions meta-class.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
declare const metaClass: {
MetaClass: {
fromKeys: <IDefault = never, I extends IDefault = IDefault>(keys: (keyof I)[]) => new (argsObj: I) => I;
};
};
import { ZodObject, ZodRawShape, infer as zinfer } from "zod";

export = metaClass;
export const MetaClass: {
fromKeys: <IDefault = never, I extends IDefault = IDefault>(keys: (keyof I)[]) => new (argsObj: I) => I;
fromZodSchema: <T extends ZodRawShape>(
schema: ZodObject<T>
) => new (argsObj: zinfer<typeof schema>) => zinfer<typeof schema>;
};
31 changes: 18 additions & 13 deletions meta-class.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
"use strict";

const MetaClassFactory = {
fromKeys: (keys) => {
class MetaClass {
#privateFields;
const fromKeys = (keys) => {
class MetaClass {
#privateFields;

constructor(argsObj) {
this.#privateFields = argsObj;
constructor(argsObj) {
this.#privateFields = argsObj;

for (const field of keys) {
Object.defineProperty(MetaClass.prototype, field, {
get: () => this.#privateFields[field],
});
}
for (const field of keys) {
Object.defineProperty(MetaClass.prototype, field, {
get: () => this.#privateFields[field],
});
}
}
}

return MetaClass;
};

return MetaClass;
},
const fromZodSchema = (schema) => fromKeys(Object.keys(schema.shape));

const MetaClassFactory = {
fromKeys,
fromZodSchema,
};

module.exports = { MetaClass: MetaClassFactory };
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "meta-class",
"name": "@leonid-shutov/meta-class",
"version": "1.0.0",
"author": "Leonid Shutov <leonid.shutov.main@gmail.com>",
"license": "MIT",
Expand All @@ -8,5 +8,11 @@
"repository": {
"type": "git",
"url": "https://github.com/leonid-shutov/meta-class"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"zod": "^3.22.4"
}
}

0 comments on commit 06d9afc

Please sign in to comment.