Compare commits

..

125 Commits

Author SHA1 Message Date
Julien Valverdé
b98a7cf3fc Build work
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-09 03:52:54 +01:00
Julien Valverdé
663db5fab2 Build system work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:45:13 +01:00
Julien Valverdé
2cfe4fc54e ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:31:51 +01:00
Julien Valverdé
34f57f017f Tests
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:22:15 +01:00
Julien Valverdé
b7e224d89b ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:17:12 +01:00
Julien Valverdé
f57360645b ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 02:07:56 +01:00
Julien Valverdé
732780342d ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 01:52:17 +01:00
Julien Valverdé
12b73e39e3 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 01:29:38 +01:00
Julien Valverdé
81efdaab01 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 00:43:53 +01:00
Julien Valverdé
24b549ffaa JsonifiableZodSchemaObject done
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 00:31:10 +01:00
Julien Valverdé
dd0610264d CreateArgs
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 00:18:05 +01:00
Julien Valverdé
ce66ffd0c5 JsonifiableZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-08 23:59:07 +01:00
Julien Valverdé
b680295b6c Dependencies upgrade
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-08 20:11:14 +01:00
Julien Valverdé
4392d43211 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 05:02:50 +01:00
Julien Valverdé
aefca6657b ZodSchemaObject fix
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:53:33 +01:00
Julien Valverdé
69d47ba3cf instantiationTransform
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:50:55 +01:00
Julien Valverdé
98ae430249 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:30:33 +01:00
Julien Valverdé
0edbe3f45c ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:02:14 +01:00
Julien Valverdé
c3c454eb15 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 02:57:30 +01:00
Julien Valverdé
d204b71a05 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 02:44:02 +01:00
Julien Valverdé
1956aae555 ZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 01:59:33 +01:00
Julien Valverdé
b1f73a2671 Traitify upgrade
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 01:29:36 +01:00
Julien Valverdé
af8d4e5f86 Cleanup
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-27 05:25:55 +01:00
Julien Valverdé
9f8cc4f895 Cleanup
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-27 04:01:08 +01:00
Julien Valverdé
0e956c0c78 InstantiableZodSchemaObject fix
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-27 03:59:21 +01:00
Julien Valverdé
2d69785add InstantiableZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-27 03:44:08 +01:00
Julien Valverdé
a6414b680c InstantiableZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-27 02:49:53 +01:00
Julien Valverdé
055caf1320 SchemaWithDefaultValues
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-26 22:55:44 +01:00
Julien Valverdé
c652aae238 ZodSchemaClassExtender work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-26 00:59:58 +01:00
Julien Valverdé
fdd267adb0 ZodSchemaClassExtender work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-26 00:54:21 +01:00
Julien Valverdé
0b0af7b128 ZodSchemaClassExtender.jsonifiable work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 23:57:46 +01:00
Julien Valverdé
f79143d821 jsonifiable
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-25 23:13:27 +01:00
Julien Valverdé
2ad276b84d jsonifiable
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 18:11:48 +01:00
Julien Valverdé
b7030be720 Fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 06:18:13 +01:00
Julien Valverdé
e5e7d48b4c Fixed MobXObservableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 06:06:35 +01:00
Julien Valverdé
8ca2805f4f MobXObservableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 05:49:50 +01:00
Julien Valverdé
93edcde258 Extend fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 05:38:27 +01:00
Julien Valverdé
29d539ec32 Override
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 05:21:35 +01:00
Julien Valverdé
1a6bd44c05 Extend work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 04:49:04 +01:00
Julien Valverdé
bf429fc63b ExtendableZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-25 04:45:58 +01:00
Julien Valverdé
e7c65c0a07 ZodSchemaClassExtender
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 04:32:09 +01:00
Julien Valverdé
34ae4b9375 Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-25 03:35:01 +01:00
Julien Valverdé
997ea4a95b ZodSchemaClassBuilder
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 23:48:09 +01:00
Julien Valverdé
af279116bc Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 23:43:51 +01:00
Julien Valverdé
4672a7e0b8 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-24 23:37:42 +01:00
Julien Valverdé
957cfdf6b8 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-24 23:25:50 +01:00
Julien Valverdé
aa15e053a7 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-24 23:11:24 +01:00
Julien Valverdé
ac5e6e444e ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 05:48:07 +01:00
Julien Valverdé
9a9b0edf5b Tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 05:27:17 +01:00
Julien Valverdé
7da3d50bcc Box tests
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-24 04:10:01 +01:00
Julien Valverdé
9e18a4a0ee Generic tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 03:03:43 +01:00
Julien Valverdé
eb071bfe86 ZodSchemaClassBuilder.extends
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 02:21:42 +01:00
Julien Valverdé
466b51aa79 Legacy cleanup
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 01:00:57 +01:00
Julien Valverdé
f731ed407f JsonifiableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 00:54:46 +01:00
Julien Valverdé
5009537839 Fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 00:47:56 +01:00
Julien Valverdé
3e8dbce574 ZodSchemaClass fix
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-24 00:37:18 +01:00
Julien Valverdé
ec2b940f5c ZodSchemaClass work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-24 00:11:06 +01:00
Julien Valverdé
53bd30aa7a ZodSchemaClass work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-23 23:45:03 +01:00
Julien Valverdé
dce10c3b6d JsonifiableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-23 22:49:52 +01:00
Julien Valverdé
ed6b8c5979 Tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-23 21:35:22 +01:00
Julien Valverdé
cffc56e7f6 DejsonifiableZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-23 20:11:06 +01:00
Julien Valverdé
03b769b74f DejsonifiableZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-23 18:01:51 +01:00
Julien Valverdé
6c6e2cce7c Jsonifiable schemas
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 23:59:28 +01:00
Julien Valverdé
68b39f4997 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-22 21:42:35 +01:00
Julien Valverdé
70012fdaf2 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-22 21:24:37 +01:00
Julien Valverdé
bddbe428a7 ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 21:00:14 +01:00
Julien Valverdé
55d8bb0c1b ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 06:06:40 +01:00
Julien Valverdé
46461cd373 ZodSchemaClassBuilder.schema fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 05:37:17 +01:00
Julien Valverdé
a58b441094 ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 04:52:36 +01:00
Julien Valverdé
718a32052d ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 04:44:24 +01:00
Julien Valverdé
26453038a2 ZodSchemaClassBuilder work
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 04:35:41 +01:00
Julien Valverdé
586eddc898 ZodSchemaClassBuilder
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-22 04:00:39 +01:00
Julien Valverdé
a6214c470e Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-22 01:10:18 +01:00
Julien Valverdé
5f18d2310c JsonifiableZodSchemaClass
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-21 21:22:56 +01:00
Julien Valverdé
cf12c2f617 Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-21 17:46:16 +01:00
Julien Valverdé
e18817be0f Cleanup
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-21 05:48:43 +01:00
Julien Valverdé
79c763fe15 Tests fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-21 04:48:14 +01:00
Julien Valverdé
0c7e1d2ab5 Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-21 04:46:20 +01:00
Julien Valverdé
1b9fa09186 ObservableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-21 04:45:43 +01:00
Julien Valverdé
4020f99d18 Fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 16:45:37 +01:00
Julien Valverdé
eaaef3bfd1 ZodSchemaClassOf refactoring
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 16:40:05 +01:00
Julien Valverdé
8297685453 Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 03:56:39 +01:00
Julien Valverdé
c00fc1f517 Tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 03:39:20 +01:00
Julien Valverdé
43876391fe Tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 03:21:39 +01:00
Julien Valverdé
021928da8c Made ObservableZodSchemaObject into a subtrait
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 02:07:50 +01:00
Julien Valverdé
ad197fe585 Changed constraints
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 02:06:24 +01:00
Julien Valverdé
a788f0ddb2 Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 01:59:03 +01:00
Julien Valverdé
0e609f7185 Fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 01:48:55 +01:00
Julien Valverdé
7af7fe9b65 Package upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-20 01:44:19 +01:00
Julien Valverdé
f2d932f534 Package upgrade 2024-02-20 01:44:13 +01:00
Julien Valverdé
73ce4e11ff Cleanup
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-20 01:19:51 +01:00
Julien Valverdé
93f1038e27 ZodSchemaClassOf
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-20 01:08:22 +01:00
Julien Valverdé
3bf22f1f49 Traitify API update
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-20 00:55:21 +01:00
Julien Valverdé
880d77019d Fix
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-09 01:16:36 +01:00
Julien Valverdé
a4d4c87f54 Fixed instance inference
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-09 01:15:23 +01:00
Julien Valverdé
0d20e7ad69 Package upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-08 20:04:45 +01:00
Julien Valverdé
443ae01d2d Tests
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-08 18:35:16 +01:00
Julien Valverdé
65602eac7d types -> shapes
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-08 18:33:58 +01:00
Julien Valverdé
99f071147d Cleanup
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-08 18:28:39 +01:00
Julien Valverdé
5f7fd30459 Working extendable
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 18:26:37 +01:00
Julien Valverdé
041b3f566e ExtendableZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 03:25:10 +01:00
Julien Valverdé
9adfe2366c Refactoring
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 03:11:11 +01:00
Julien Valverdé
408d3a7f3a Tests
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 02:52:27 +01:00
Julien Valverdé
e1c298c08a Fixed InstantiableZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 02:50:16 +01:00
Julien Valverdé
ae4abf6b9c defaultValues fix
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 02:42:02 +01:00
Julien Valverdé
ed3f8fb643 ZodSchemaClassOf extend
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 02:39:38 +01:00
Julien Valverdé
ffce582e03 ZodSchemaClass work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-08 01:17:01 +01:00
Julien Valverdé
fa7d222f76 ZodSchemaClass work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-07 00:39:37 +01:00
Julien Valverdé
f40bebbc9b InstantiableZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-07 00:32:23 +01:00
Julien Valverdé
6ee9337426 Trait work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-06 05:24:00 +01:00
Julien Valverdé
5297468d17 ZodSchemaClass.old.ts
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-06 03:32:52 +01:00
Julien Valverdé
ff9bbb37e8 Dependencies
All checks were successful
continuous-integration/drone/push Build is passing
2024-02-06 03:30:23 +01:00
Julien Valverdé
9ba149fb4c instantiate -> create
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-28 16:36:02 +01:00
Julien Valverdé
d867a3b83a Refactoring
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-28 16:31:44 +01:00
Julien Valverdé
e35db63da8 Refactoring
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-28 02:33:05 +01:00
Julien Valverdé
a4b8166af8 Inheritance logic refactoring
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-28 01:57:58 +01:00
Julien Valverdé
d34885a6e4 Dependencies upgrade
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-28 01:39:10 +01:00
Julien Valverdé
ab328f2cb5 Util refactoring
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-28 01:08:15 +01:00
Julien Valverdé
147034e95d ZodSchemaClassOf refactoring
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-24 21:49:19 +01:00
Julien Valverdé
516ce0ade2 Class type detection
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-24 21:40:27 +01:00
Julien Valverdé
338c750a4b TZodSchemaClass work
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-24 19:06:05 +01:00
Julien Valverdé
b6c2c4bec7 ZodSchemaClassOf work
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-21 23:29:11 +01:00
Julien Valverdé
c073154eeb Working new implementation
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-21 22:58:44 +01:00
Julien Valverdé
6873fae86b ZodSchemaClass
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-21 20:15:25 +01:00
Julien Valverdé
fc95a5d53a Moved current version to legacy
All checks were successful
continuous-integration/drone/push Build is passing
2024-01-20 22:02:14 +01:00
36 changed files with 1006 additions and 805 deletions

View File

@@ -16,8 +16,8 @@ local lint_step = {
local build_step = { local build_step = {
name: "build", name: "build",
image: node_image, image: bun_image,
commands: ["npm run build"], commands: ["bun run build"],
}; };
local pack_step = { local pack_step = {

View File

@@ -1,3 +1,3 @@
# zod-schema-class # schemable-class
Create TypeScript classes out of Zod schemas Create TypeScript classes out of Zod schemas

BIN
bun.lockb

Binary file not shown.

View File

@@ -1,6 +1,6 @@
{ {
"name": "@thilawyn/zod-schema-class", "name": "@thilawyn/schemable-class",
"version": "0.1.3", "version": "0.1.2",
"type": "module", "type": "module",
"publishConfig": { "publishConfig": {
"registry": "https://git.jvalver.de/api/packages/thilawyn/npm/" "registry": "https://git.jvalver.de/api/packages/thilawyn/npm/"
@@ -8,12 +8,11 @@
"files": [ "files": [
"./dist" "./dist"
], ],
"types": "./dist/lib.d.ts",
"exports": { "exports": {
".": { ".": {
"import": { "import": {
"types": "./dist/lib.d.ts", "types": "./dist/lib.d.mts",
"default": "./dist/lib.js" "default": "./dist/lib.mjs"
}, },
"require": { "require": {
"types": "./dist/lib.d.cts", "types": "./dist/lib.d.cts",
@@ -22,45 +21,42 @@
}, },
"./schema": { "./schema": {
"import": { "import": {
"types": "./dist/schema/lib.d.ts", "types": "./dist/schema.d.mts",
"default": "./dist/schema/lib.js" "default": "./dist/schema.mjs"
}, },
"require": { "require": {
"types": "./dist/schema/lib.d.cts", "types": "./dist/schema.d.cts",
"default": "./dist/schema/lib.cjs" "default": "./dist/schema.cjs"
} }
} }
}, },
"scripts": { "scripts": {
"build": "tsup", "build": "rollup -c rollup.config.ts",
"lint:tsc": "tsc --noEmit", "lint:tsc": "tsc --noEmit",
"clean:cache": "rm -f tsconfig.tsbuildinfo", "clean:cache": "rm -f tsconfig.tsbuildinfo",
"clean:dist": "rm -rf dist", "clean:dist": "rm -rf dist",
"clean:node": "rm -rf node_modules" "clean:node": "rm -rf node_modules"
}, },
"dependencies": { "dependencies": {
"@thilawyn/traitify-ts": "^0.1.19", "@thilawyn/traitify-ts": "^0.1.14",
"decimal.js": "^10.4.3",
"effect": "^2.4.1",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"type-fest": "^4.14.0", "mobx": "^6.12.0",
"type-fest": "^4.12.0",
"zod": "^3.22.4" "zod": "^3.22.4"
}, },
"peerDependencies": {
"mobx": "^6.12.1",
"decimal.js": "^10.4.3",
"effect": "^2.4.11"
},
"peerDependenciesMeta": {
"mobx": { "optional": true },
"decimal.js": { "optional": true },
"effect": { "optional": true }
},
"devDependencies": { "devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"bun-types": "latest", "bun-types": "latest",
"npm-check-updates": "^16.14.17", "npm-check-updates": "^16.14.15",
"npm-sort": "^0.0.4", "npm-sort": "^0.0.4",
"tsup": "^8.0.2", "rollup": "^4.12.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-ts": "^3.4.5",
"ts-functional-pipe": "^3.1.2",
"tsx": "^4.7.1", "tsx": "^4.7.1",
"typescript": "^5.4.3" "typescript": "^5.4.2"
} }
} }

43
rollup.config.ts Normal file
View File

@@ -0,0 +1,43 @@
import { nodeResolve } from "@rollup/plugin-node-resolve"
import { defineConfig } from "rollup"
import cleanup from "rollup-plugin-cleanup"
import ts from "rollup-plugin-ts"
import pkg from "./package.json" assert { type: "json" }
export const createBundleConfig = (
input: string,
name: keyof typeof pkg.exports,
) =>
defineConfig({
input,
output: [
{
file: pkg.exports[name].import.default,
format: "esm",
},
{
file: pkg.exports[name].require.default,
format: "cjs",
},
],
external: id => !/^[./]/.test(id),
plugins: [
nodeResolve(),
ts(),
cleanup({
comments: "jsdoc",
extensions: ["ts"],
}),
],
})
export default [
createBundleConfig("src/lib.ts", "."),
createBundleConfig("src/schema/lib.ts", "./schema"),
]

View File

@@ -1,56 +0,0 @@
import { TraitInstance, TraitStaticMembers, expression } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest"
import { z } from "zod"
import { ZodSchemaObject, ZodSchemaObjectTrait } from "./traits/ZodSchemaObject"
import { Extend, StaticMembers } from "./util"
export function ExtendZodSchemaClass<
Self extends (
AbstractClass<
Values & TraitInstance<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
[values: Values]
> &
TraitStaticMembers<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
),
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
ExtendedT extends z.ZodRawShape,
ExtendedCatchall extends z.ZodTypeAny,
ExtendedValues extends Values,
ExtendedPartialValues extends Partial<ExtendedValues>,
>(
class_: Self | (
AbstractClass<
Values & TraitInstance<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
[values: Values]
> &
TraitStaticMembers<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
),
schemaWithDefaults: (
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
) => z.ZodObject<ExtendedT, "strip", ExtendedCatchall, ExtendedValues, ExtendedPartialValues>,
) {
return expression
.extends(
class_ as (
AbstractClass<
Omit<
Extend<[InstanceType<Self>, ExtendedValues]>,
keyof TraitInstance<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
>,
[values: ExtendedValues]
> &
Omit<
StaticMembers<Self>,
keyof TraitStaticMembers<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
>
)
)
.expresses(ZodSchemaObject(schemaWithDefaults((class_ as Self).schemaWithDefaults)))
}

View File

@@ -1,42 +0,0 @@
import { expression } from "@thilawyn/traitify-ts"
import { Class, Jsonifiable } from "type-fest"
import { z } from "zod"
import { JsonifiedZodSchemaObject, OfClass, OfClassInstance } from "./traits/JsonifiedZodSchemaObject"
export function JsonifiedZodSchemaClass<
Of extends OfClass<OfInstance, T, Catchall, Values, PartialValues>,
OfInstance extends OfClassInstance<T, Catchall, Values, PartialValues>,
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
JsonifyT extends z.ZodRawShape,
JsonifyCatchall extends z.ZodTypeAny,
DejsonifyT extends z.ZodRawShape,
DejsonifyCatchall extends z.ZodTypeAny,
JsonifiedValues extends object & Jsonifiable,
>(
of: Of | OfClass<OfInstance, T, Catchall, Values, PartialValues>,
props: {
jsonifySchema: (
schema: typeof of.schema
) => z.ZodObject<JsonifyT, "strip", JsonifyCatchall, JsonifiedValues, Values>,
dejsonifySchema: (
schema: typeof of.schema
) => z.ZodObject<DejsonifyT, "strip", DejsonifyCatchall, Values, JsonifiedValues>,
},
) {
return expression
.extends(
class JsonifiedZodSchemaObjectConstructor {
constructor(values: JsonifiedValues) {
Object.assign(this, values)
}
} as Class<JsonifiedValues, [values: JsonifiedValues]>
)
.expresses(JsonifiedZodSchemaObject(of as Of, props))
}

View File

@@ -1,24 +0,0 @@
import { expression } from "@thilawyn/traitify-ts"
import { Class } from "type-fest"
import { z } from "zod"
import { ZodSchemaObject } from "./traits/ZodSchemaObject"
export function ZodSchemaClass<
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
) {
return expression
.extends(
class ZodSchemaObjectConstructor {
constructor(values: Values) {
Object.assign(this, values)
}
} as Class<Values, [values: Values]>
)
.expresses(ZodSchemaObject(schemaWithDefaults))
}

View File

@@ -0,0 +1,133 @@
import { Trait, TraitExpressionBuilder, expression } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest"
import { EmptyObject } from "type-fest/source/empty-object"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
import { ZodSchemaObject } from "../traits/ZodSchemaObject"
export class ZodSchemaClassBuilder<
Superclass extends AbstractClass<object>,
const Traits extends readonly Trait<any, any, any, any>[],
Schemas extends object,
> {
declare ["constructor"]: typeof ZodSchemaClassBuilder
constructor(
protected readonly expression: TraitExpressionBuilder<Superclass, Traits>,
protected readonly schemas: Schemas,
) {}
schema<
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: ZodSchemaClassBuilder<Superclass, Traits, EmptyObject>,
props: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
schemaWithDefaultValues: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
},
) {
const schema = props.schema
const schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
abstract class ZodSchemaObjectConstructor {
constructor(values: Values) {
Object.assign(this, values)
}
}
return new this.constructor(
this.expression
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues)),
{ schema, schemaWithDefaultValues } as const,
)
}
jsonifiable<
S extends {
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema?: never
dejsonifySchema?: never
},
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
>(
this: ZodSchemaClassBuilder<Superclass, Traits, S | {
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema?: never
dejsonifySchema?: never
}>,
props: {
jsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
dejsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
},
) {
const jsonifySchema = props.jsonifySchema(this.schemas.schema)
const dejsonifySchema = props.dejsonifySchema(this.schemas.schema)
return new this.constructor(
this.expression.expresses(
JsonifiableZodSchemaObject(
this.schemas.schema,
this.schemas.schemaWithDefaultValues,
jsonifySchema,
dejsonifySchema,
)
),
{ ...this.schemas as S, jsonifySchema, dejsonifySchema } as const,
)
}
toExpression() {
return this.expression
}
}
export const zodSchemaClass = new ZodSchemaClassBuilder(expression, {})

View File

@@ -0,0 +1,196 @@
// import { expression } from "@thilawyn/traitify-ts"
// import { AbstractClass, Simplify } from "type-fest"
// import { JsonifiableObject } from "type-fest/source/jsonifiable"
// import { z } from "zod"
// import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass"
// import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
// import { Extend, Override, StaticMembers } from "../util"
// export class ZodSchemaClassExtender<
// Superclass extends AbstractClass<object>,
// Subclass extends AbstractClass<object>,
// > {
// declare ["constructor"]: typeof ZodSchemaClassExtender
// constructor(
// readonly superclass: Superclass,
// readonly subclass: Subclass,
// ) {}
// schema<
// Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
// SuperSchemaT extends z.ZodRawShape,
// SuperSchemaUnknownKeys extends z.UnknownKeysParam,
// SuperSchemaCatchall extends z.ZodTypeAny,
// SuperValues extends object,
// SuperDefaultValues extends Partial<SuperValues>,
// SchemaT extends z.ZodRawShape,
// SchemaUnknownKeys extends z.UnknownKeysParam,
// SchemaCatchall extends z.ZodTypeAny,
// Values extends SuperValues,
// DefaultValues extends Partial<Values>,
// >(
// this: ZodSchemaClassExtender<
// Super | ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
// any
// >,
// props: {
// schema: (schema: Super["schema"]) => z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
// defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
// },
// ) {
// const schema = props.schema(this.superclass.schema)
// const defaultValues = props.defaultValues(this.superclass.defaultValues)
// class Schemas extends (this.superclass as AbstractClass<object, any[]>) {
// static readonly schema = schema
// static readonly defaultValues = defaultValues
// }
// return new this.constructor(
// this.superclass as Super,
// Schemas as unknown as AbstractClass<
// Simplify<
// Extend<[InstanceType<Super>, Values]>
// >,
// [values: Values]
// > &
// Simplify<
// Override<[
// StaticMembers<Super>,
// StaticMembers<typeof Schemas>,
// ]>
// >
// )
// }
// jsonifiable<
// /** Superclass jsonifiable schemas */
// Super extends JsonifiableZodSchemaAbstractClass<
// any,
// SuperJsonifySchemaT,
// SuperJsonifySchemaUnknownKeys,
// SuperJsonifySchemaCatchall,
// SuperDejsonifySchemaT,
// SuperDejsonifySchemaUnknownKeys,
// SuperDejsonifySchemaCatchall,
// SuperJsonifiedValues,
// SuperValues
// >,
// SuperJsonifySchemaT extends z.ZodRawShape,
// SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam,
// SuperJsonifySchemaCatchall extends z.ZodTypeAny,
// SuperDejsonifySchemaT extends z.ZodRawShape,
// SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
// SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
// SuperJsonifiedValues extends JsonifiableObject,
// SuperValues extends object,
// /** New schemas */
// Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>,
// SelfSchemaT extends z.ZodRawShape,
// SelfSchemaUnknownKeys extends z.UnknownKeysParam,
// SelfSchemaCatchall extends z.ZodTypeAny,
// SelfValues extends object,
// SelfDefaultValues extends Partial<SelfValues>,
// /* New jsonifiable schemas */
// JsonifySchemaT extends z.ZodRawShape,
// JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
// JsonifySchemaCatchall extends z.ZodTypeAny,
// DejsonifySchemaT extends z.ZodRawShape,
// DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
// DejsonifySchemaCatchall extends z.ZodTypeAny,
// JsonifiedValues extends SuperJsonifiedValues,
// Values extends SelfValues,
// >(
// this: ZodSchemaClassExtender<
// Super | JsonifiableZodSchemaAbstractClass<
// any,
// SuperJsonifySchemaT,
// SuperJsonifySchemaUnknownKeys,
// SuperJsonifySchemaCatchall,
// SuperDejsonifySchemaT,
// SuperDejsonifySchemaUnknownKeys,
// SuperDejsonifySchemaCatchall,
// SuperJsonifiedValues,
// SuperValues
// >,
// Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
// >,
// props: {
// jsonifySchema: (
// schema: Self["schema"],
// jsonifySchema: Super["jsonifySchema"],
// ) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
// dejsonifySchema: (
// schema: Self["schema"],
// dejsonifySchema: Super["dejsonifySchema"],
// ) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
// },
// ) {
// const jsonifySchema = props.jsonifySchema(this.subclass.schema, this.superclass.jsonifySchema)
// const dejsonifySchema = props.dejsonifySchema(this.subclass.schema, this.superclass.dejsonifySchema)
// class JsonifiableSchemas extends (this.subclass as AbstractClass<object>) {
// static readonly jsonifySchema = jsonifySchema
// readonly jsonifySchema = jsonifySchema
// static readonly dejsonifySchema = dejsonifySchema
// readonly dejsonifySchema = dejsonifySchema
// }
// return new this.constructor(
// this.superclass as Super,
// JsonifiableSchemas as unknown as AbstractClass<
// Simplify<
// Override<[InstanceType<Self>, JsonifiableSchemas]>
// >,
// ConstructorParameters<
// ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
// >
// > &
// Simplify<
// Override<[
// StaticMembers<Self>,
// StaticMembers<typeof JsonifiableSchemas>,
// ]>
// >,
// )
// }
// toClass() {
// return this.subclass
// }
// toExpressionBuilder() {
// return expression.extends(this.subclass)
// }
// }

View File

@@ -1,6 +1,5 @@
export { ExtendZodSchemaClass } from "./ExtendZodSchemaClass" export { ZodSchemaClassBuilder } from "./builders/ZodSchemaClassBuilder"
export { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass" // export { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
export { ZodSchemaClass } from "./ZodSchemaClass" export { JsonifiableZodSchemaObject } from "./traits/JsonifiableZodSchemaObject"
export { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject"
export { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" export { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
export { ZodSchemaObject, ZodSchemaObjectTrait } from "./traits/ZodSchemaObject" export { ZodSchemaObject, ZodSchemaObjectTrait } from "./traits/ZodSchemaObject"

View File

@@ -1,19 +0,0 @@
import { z } from "zod"
type JsonifiedBigIntBrand = "@thilawyn/zod-schema-class/JsonifiedBigInt"
export function jsonify<S extends z.ZodType<bigint>>(schema: S) {
return schema
.transform(v => v.toString())
.brand<JsonifiedBigIntBrand>()
}
export function dejsonify<S extends z.ZodType<bigint>>(schema: S) {
return z
.custom<string & z.BRAND<JsonifiedBigIntBrand>>()
.pipe(z.string())
.pipe(z.coerce.bigint())
.pipe(schema)
}

View File

@@ -1,19 +0,0 @@
import { z } from "zod"
type JsonifiedDateBrand = "@thilawyn/zod-schema-class/JsonifiedDate"
export function jsonify<S extends z.ZodType<Date>>(schema: S) {
return schema
.transform(v => v.toString())
.brand<JsonifiedDateBrand>()
}
export function dejsonify<S extends z.ZodType<Date>>(schema: S) {
return z
.custom<string & z.BRAND<JsonifiedDateBrand>>()
.pipe(z.string())
.pipe(z.coerce.date())
.pipe(schema)
}

View File

@@ -1,36 +0,0 @@
import Decimal from "decimal.js"
import { z } from "zod"
type JsonifiedDecimalBrand = "@thilawyn/zod-schema-class/JsonifiedDecimal"
export function decimal() {
return z.custom<Decimal>(v => Decimal.isDecimal(v))
}
export module decimal {
export function jsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
return schema
.transform(v => v.toJSON())
.brand<JsonifiedDecimalBrand>()
}
export function dejsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
return z
.custom<string & z.BRAND<JsonifiedDecimalBrand>>()
.pipe(
z
.string()
.transform(v => {
try {
return new Decimal(v)
}
catch (e) {
return v
}
})
)
.pipe(schema)
}
}

View File

@@ -1 +0,0 @@
export { option } from "./option"

View File

@@ -1,146 +0,0 @@
import { Option } from "effect"
import { identity } from "lodash-es"
import { z } from "zod"
type JsonifiedEffectOptionSomeBrand = "@thilawyn/zod-schema-class/JsonifiedEffectOptionSome"
export function option<InnerS extends z.ZodTypeAny>(schema: InnerS) {
return z.union([option.some(schema), option.none(schema)])
}
export module option {
export type Option<InnerS extends z.ZodTypeAny> = ReturnType<typeof option<InnerS>>
function getSomeSchema<InnerS extends z.ZodTypeAny>(schema: Option<InnerS>): some.Some<InnerS> {
return schema.options[0]
}
function getNoneSchema<InnerS extends z.ZodTypeAny>(schema: Option<InnerS>): none.None<InnerS> {
return schema.options[1]
}
export function jsonify<
InnerS extends z.ZodTypeAny,
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
>(
schema: Option<InnerS>,
jsonifySchema: (schema: InnerS) => JsonifiedInnerS = identity,
) {
return z.union([
some.jsonify(getSomeSchema(schema), jsonifySchema),
none.jsonify(getNoneSchema(schema), jsonifySchema),
])
}
export function dejsonify<
InnerS extends z.ZodTypeAny,
DejsonifiedInnerS extends z.ZodTypeAny = InnerS,
>(
schema: Option<InnerS>,
dejsonifySchema: (schema: InnerS) => DejsonifiedInnerS = identity,
) {
return z.union([
some.dejsonify(getSomeSchema(schema), dejsonifySchema),
none.dejsonify(getNoneSchema(schema), dejsonifySchema),
])
}
export function some<
InnerS extends z.ZodTypeAny
>(
schema: InnerS
) {
return z
.custom<Option.Some<z.output<InnerS>>>(
v => Option.isOption(v) && Option.isSome(v),
"Not an Option.Some",
)
.pipe(z.object({ value: schema }).passthrough())
.transform(v => Option.some<z.output<InnerS>>(v.value))
}
export module some {
export type Some<InnerS extends z.ZodTypeAny> = ReturnType<typeof some<InnerS>>
function getInnerSchema<InnerS extends z.ZodTypeAny>(schema: Some<InnerS>): InnerS {
return schema._def.schema._def.out.shape.value
}
export function jsonify<
InnerS extends z.ZodTypeAny,
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
>(
schema: Some<InnerS>,
jsonifySchema: (schema: InnerS) => JsonifiedInnerS = identity,
) {
return schema
.transform(v => Option.getOrThrow(v))
.pipe(jsonifySchema(getInnerSchema(schema)))
.brand<JsonifiedEffectOptionSomeBrand>()
}
export function dejsonify<
InnerS extends z.ZodTypeAny,
DejsonifiedInnerS extends z.ZodTypeAny = InnerS,
>(
schema: Some<InnerS>,
dejsonifySchema: (schema: InnerS) => DejsonifiedInnerS = identity,
) {
return z
.custom<z.input<DejsonifiedInnerS> & z.BRAND<JsonifiedEffectOptionSomeBrand>>()
.pipe(dejsonifySchema(getInnerSchema(schema)))
.transform(v => Option.some<z.output<DejsonifiedInnerS>>(v))
.pipe(schema)
}
}
export function none<
InnerS extends z.ZodTypeAny | unknown = unknown
>(
_schema?: InnerS
) {
return z.custom<
Option.None<
InnerS extends z.ZodTypeAny
? z.output<InnerS>
: unknown
>
>(
v => Option.isOption(v) && Option.isNone(v),
"Not an Option.None",
)
}
export module none {
export type None<InnerS extends z.ZodTypeAny> = ReturnType<typeof none<InnerS>>
export function jsonify<
InnerS extends z.ZodTypeAny,
JsonifiedInnerS extends z.ZodTypeAny = InnerS,
>(
schema: None<InnerS>,
_jsonifySchema?: (schema: InnerS) => JsonifiedInnerS,
) {
return schema.transform(() => null)
}
export function dejsonify<
InnerS extends z.ZodTypeAny,
DejsonifiedInnerS extends z.ZodTypeAny = InnerS,
>(
schema: None<InnerS>,
_dejsonifySchema?: (schema: InnerS) => DejsonifiedInnerS,
) {
return z
.null()
.transform(() => Option.none<z.output<DejsonifiedInnerS>>())
.pipe(schema)
}
}
}

View File

@@ -0,0 +1,28 @@
import { identity } from "lodash-es"
import { Opaque } from "type-fest"
import { z } from "zod"
export type JsonifiedBigInt = Opaque<string, "@thilawyn/schemable-class/JsonifiedBigInt">
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
return schema.transform(v => v.toString() as JsonifiedBigInt)
}
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
return z
.custom<JsonifiedBigInt>(identity)
.pipe(z
.string()
.transform(v => {
try {
return BigInt(v)
}
catch (e) {
return v
}
})
.pipe(schema)
)
}

View File

@@ -0,0 +1,28 @@
import { identity } from "lodash-es"
import { Opaque } from "type-fest"
import { z } from "zod"
export type JsonifiedDate = Opaque<string, "@thilawyn/schemable-class/JsonifiedDate">
export function jsonifyDateSchema<S extends z.ZodDate>(schema: S) {
return schema.transform(v => v.toString() as JsonifiedDate)
}
export function dejsonifyDateSchema<S extends z.ZodDate>(schema: S) {
return z
.custom<JsonifiedDate>(identity)
.pipe(z
.string()
.transform(v => {
try {
return new Date(v)
}
catch (e) {
return v
}
})
.pipe(schema)
)
}

View File

@@ -0,0 +1,33 @@
import { Decimal } from "decimal.js"
import { identity } from "lodash-es"
import { Opaque } from "type-fest"
import { z } from "zod"
export type JsonifiedDecimal = Opaque<string, "@thilawyn/schemable-class/JsonifiedDecimal">
export function jsonifyDecimalSchema<
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
>(schema: S) {
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
}
export function dejsonifyDecimalSchema<
S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>
>(schema: S) {
return z
.custom<JsonifiedDecimal>(identity)
.pipe(z
.string()
.transform(v => {
try {
return new Decimal(v)
}
catch (e) {
return v
}
})
.pipe(schema)
)
}

View File

@@ -0,0 +1,16 @@
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./bigint"
import { dejsonifyDateSchema, jsonifyDateSchema } from "./date"
import { dejsonifyDecimalSchema, jsonifyDecimalSchema } from "./decimal"
export const jsonify = {
bigint: jsonifyBigIntSchema,
date: jsonifyDateSchema,
decimal: jsonifyDecimalSchema,
} as const
export const dejsonify = {
bigint: dejsonifyBigIntSchema,
date: dejsonifyDateSchema,
decimal: dejsonifyDecimalSchema,
} as const

View File

@@ -1,12 +1 @@
import * as bigint from "./bigint" export * from "./jsonify"
import * as date from "./date"
import { decimal } from "./decimal"
import * as effect from "./effect"
export const s = {
bigint,
date,
decimal,
effect,
} as const

View File

@@ -0,0 +1,78 @@
import { AbstractClass, Class } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
export type JsonifiableZodSchemaClass<
Instance extends Values,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
> = (
Class<Instance, [values: Values]> &
JsonifiableZodSchemas<
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>
)
export type JsonifiableZodSchemaAbstractClass<
Instance extends Values,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
> = (
AbstractClass<Instance, [values: Values]> &
JsonifiableZodSchemas<
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>
)
export type JsonifiableZodSchemas<
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
> = {
readonly jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
readonly dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
}

View File

@@ -0,0 +1,55 @@
import { AbstractClass, Class } from "type-fest"
import { z } from "zod"
export type ZodSchemaClass<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = (
Class<Instance, [values: Values]> &
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
)
export type ZodSchemaAbstractClass<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = (
AbstractClass<Instance, [values: Values]> &
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
)
export type ZodSchemas<
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = {
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
}

View File

@@ -1,60 +1,64 @@
import { Implements } from "@thilawyn/traitify-ts" import { Implements } from "@thilawyn/traitify-ts"
import { Option } from "effect"
import { z } from "zod" import { z } from "zod"
import { ExtendZodSchemaClass } from "./ExtendZodSchemaClass" import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
import { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass" import { dejsonify, jsonify } from "./schema/jsonify"
import { ZodSchemaClass } from "./ZodSchemaClass"
import { s } from "./schema/lib"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
const userExp = ZodSchemaClass( const exp = zodSchemaClass
z.object({ .schema({
schema: z.object({
/** User ID */ /** User ID */
id: s.effect.option(z.bigint()).default(Option.none()), id: z.bigint(),
/** Username */ /** Username */
name: z.string(), name: z.string(),
}),
/** Role */ schemaWithDefaultValues: s => s.extend({
role: z.enum(["User", "Moderator", "Admin"]).default("User"), id: s.shape.id.default(-1n),
}),
}) })
) .jsonifiable({
jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id)
}),
dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id)
}),
})
.toExpression()
.expresses(MobXObservableZodSchemaObject) .expresses(MobXObservableZodSchemaObject)
.build() .build()
@userExp.staticImplements
class User extends userExp.extends implements Implements<typeof userExp> {}
const userInst = User.create({ id: Option.some(1n), name: "User" }) @exp.staticImplements
const userInstEffect = User.createEffect({ id: Option.some(1n), name: "User" }) class User extends exp.extends implements Implements<typeof exp> {}
const inst = User.create({ id: 1n, name: "User" }, )
// console.log(inst.name)
const instEffect = User.createEffect({ id: 1n, name: "User" })
const jsonifiedUser = await inst.jsonifyPromise()
const dejsonifiedInst = await User.dejsonifyPromise(jsonifiedUser)
const jsonifiedUserExp = JsonifiedZodSchemaClass(User, { // const AdminUserProto = User.extend()
jsonifySchema: schema => schema.extend({ // .schema({
id: s.effect.option.jsonify(schema.shape.id, s.bigint.jsonify) // schema: s => s.extend({
}), // name: z.literal("Admin"),
// prout: z.string(),
dejsonifySchema: schema => schema.extend({ // }),
id: s.effect.option.dejsonify(schema.shape.id, s.bigint.dejsonify) // defaultValues: v => ({ ...v, name: "Admin" as const }),
}), // })
}).build() // .jsonifiable({
// jsonifySchema: (s, json) => json.extend({
@jsonifiedUserExp.staticImplements // prout: s.shape.prout
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {} // })
// })
const jsonifiedUserInst = JsonifiedUser.jsonify(userInst)
// console.log(JSON.stringify(jsonifiedUserInst))
console.log(jsonifiedUserInst.dejsonify().id)
const adminUserExp = ExtendZodSchemaClass(User, // class AdminUser extends AdminUserProto.toClass() {}
schema => schema.extend({ // const subInst = await AdminUser.createPromise({ id: 2n, prout: "" })
role: z.literal("Admin").default("Admin") // console.log(subInst)
})
).build()
@adminUserExp.staticImplements
class AdminUser extends adminUserExp.extends implements Implements<typeof adminUserExp> {}
const admin = AdminUser.create({ id: Option.some(2n), name: "Admin" })

View File

@@ -1,20 +0,0 @@
import { Option } from "effect"
import { z } from "zod"
import { s } from "./schema/lib"
const schema = z.object({
id: s.effect.option(z.bigint())
})
const jsonifySchema = schema.extend({
id: s.effect.option.jsonify(schema.shape.id, s.bigint.jsonify)
})
const dejsonifySchema = schema.extend({
id: s.effect.option.dejsonify(schema.shape.id, s.bigint.dejsonify)
})
const jsonified = jsonifySchema.parse({ id: Option.some(1n) })
console.log(dejsonifySchema.parse(jsonified))

View File

@@ -1,51 +1,16 @@
import { ImplStatic, expression, implStaticThis } from "@thilawyn/traitify-ts" // import { trait } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest" // import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender"
import { z } from "zod" // import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
import { Extend, StaticMembers } from "../util"
import { ZodSchemaObject } from "./ZodSchemaObject"
export const ExtendableZodSchemaObject = < // export const ExtendableZodSchemaObject = trait
T extends z.ZodRawShape, // .implement(Super => class ExtendableZodSchemaObject extends Super {
Catchall extends z.ZodTypeAny, // static extend<
Values extends object, // Self extends ZodSchemaClass<any, any, any, any, any, any, any, any, any>,
PartialValues extends Partial<Values>, // >(
>( // this: Self
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>, // ) {
) => expression // return new ZodSchemaClassExtender(this, this)
.expresses(ZodSchemaObject(schemaWithDefaults)) // }
.build() // })
.subtrait() // .build()
.implement(Super => class ExtendableZodSchemaObjectImpl extends Super {
static extend<
Self extends AbstractClass<Values, [values: Values]>,
ExtendedT extends z.ZodRawShape,
ExtendedCatchall extends z.ZodTypeAny,
ExtendedValues extends Values,
ExtendedPartialValues extends Partial<ExtendedValues>,
>(
this: Self,
schemaWithDefaults: (
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
) => z.ZodObject<ExtendedT, "strip", ExtendedCatchall, ExtendedValues, ExtendedPartialValues>,
) {
const t = implStaticThis(ExtendableZodSchemaObjectImpl, this)
return expression
.extends(
t as unknown as (
AbstractClass<
Omit<
Extend<[InstanceType<Self>, ExtendedValues]>,
keyof ExtendableZodSchemaObjectImpl
>,
[values: ExtendedValues]
> &
Omit<StaticMembers<Self>, keyof ImplStatic<typeof ExtendableZodSchemaObjectImpl>>
)
)
.expresses(ExtendableZodSchemaObject(schemaWithDefaults(t.schemaWithDefaults)))
}
})
.build()

View File

@@ -0,0 +1,112 @@
import { ImplStatic, expression } from "@thilawyn/traitify-ts"
import { Class } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { parseZodSchemaEffect } from "../util"
import { ZodSchemaObject } from "./ZodSchemaObject"
export const JsonifiableZodSchemaObject = <
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
>(
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>,
dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>,
) => expression
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues))
.build()
.subtrait()
.implement(Super => class JsonifiableZodSchemaObject extends Super {
static readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema
jsonify(params?: Partial<z.ParseParams>) {
return (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>)
.jsonifySchema
.parse(this, params)
}
jsonifyPromise(params?: Partial<z.ParseParams>) {
return (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>)
.jsonifySchema
.parseAsync(this, params)
}
jsonifyEffect(params?: Partial<z.ParseParams>) {
return parseZodSchemaEffect(
(this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>).jsonifySchema,
this,
params,
)
}
static dejsonify<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this
.transform(this.dejsonifySchema)
.parse(values, params)
}
static dejsonifyPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this
.transform(this.dejsonifySchema)
.parseAsync(values, params)
}
static dejsonifyEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return parseZodSchemaEffect(
this.transform(this.dejsonifySchema),
values,
params,
)
}
})
.build()

View File

@@ -1,174 +0,0 @@
import { TraitInstance, TraitStaticMembers, implStaticInstantiableThis, trait } from "@thilawyn/traitify-ts"
import { Class, Jsonifiable } from "type-fest"
import { z } from "zod"
import { parseZodSchemaEffect } from "../util"
import { ZodSchemaObjectTrait } from "./ZodSchemaObject"
export type OfClass<
Instance extends OfClassInstance<T, Catchall, Values, PartialValues>,
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = (
Class<Instance, [values: Values]> &
TraitStaticMembers<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
)
export type OfClassInstance<
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = (
Values & TraitInstance<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
)
export const JsonifiedZodSchemaObject = <
Of extends OfClass<OfInstance, T, Catchall, Values, PartialValues>,
OfInstance extends OfClassInstance<T, Catchall, Values, PartialValues>,
T extends z.ZodRawShape,
Catchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
JsonifyT extends z.ZodRawShape,
JsonifyCatchall extends z.ZodTypeAny,
DejsonifyT extends z.ZodRawShape,
DejsonifyCatchall extends z.ZodTypeAny,
JsonifiedValues extends object & Jsonifiable,
>(
of: Of | OfClass<OfInstance, T, Catchall, Values, PartialValues>,
props: {
jsonifySchema: (
schema: typeof of.schema
) => z.ZodObject<JsonifyT, "strip", JsonifyCatchall, JsonifiedValues, Values>,
dejsonifySchema: (
schema: typeof of.schema
) => z.ZodObject<DejsonifyT, "strip", DejsonifyCatchall, Values, JsonifiedValues>,
},
) => trait
.implement(Super => class JsonifiedZodSchemaObjectImpl extends Super {
declare ["constructor"]: typeof JsonifiedZodSchemaObjectImpl
static readonly of = of as Of
static readonly jsonifySchema = props.jsonifySchema(of.schema)
static readonly dejsonifySchema = props.dejsonifySchema(of.schema)
static pipeSchemaIntoInstance<
Instance extends JsonifiedValues,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaOutput extends JsonifiedValues,
SchemaInput,
>(
this: Class<Instance, [values: JsonifiedValues]>,
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaOutput, SchemaInput>,
) {
return schema.transform(values => new this(values))
}
static jsonify<
Instance extends JsonifiedValues
>(
this: Class<Instance, [values: JsonifiedValues]>,
values: Values,
params?: Partial<z.ParseParams>,
) {
const t = implStaticInstantiableThis(JsonifiedZodSchemaObjectImpl, this)
return t
.pipeSchemaIntoInstance(t.jsonifySchema)
.parse(values, params)
}
static jsonifyPromise<
Instance extends JsonifiedValues
>(
this: Class<Instance, [values: JsonifiedValues]>,
values: Values,
params?: Partial<z.ParseParams>,
) {
const t = implStaticInstantiableThis(JsonifiedZodSchemaObjectImpl, this)
return t
.pipeSchemaIntoInstance(t.jsonifySchema)
.parseAsync(values, params)
}
static jsonifyEffect<
Instance extends JsonifiedValues
>(
this: Class<Instance, [values: JsonifiedValues]>,
values: Values,
params?: Partial<z.ParseParams>,
) {
const t = implStaticInstantiableThis(JsonifiedZodSchemaObjectImpl, this)
return parseZodSchemaEffect(
t.pipeSchemaIntoInstance(t.jsonifySchema),
values,
params,
)
}
static dejsonify(
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this.of
.pipeSchemaIntoInstance(this.dejsonifySchema)
.parse(values, params)
}
static dejsonifyPromise(
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this.of
.pipeSchemaIntoInstance(this.dejsonifySchema)
.parseAsync(values, params)
}
static dejsonifyEffect(
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return parseZodSchemaEffect(
this.of.pipeSchemaIntoInstance(this.dejsonifySchema),
values,
params,
)
}
dejsonify(params?: Partial<z.ParseParams>) {
return this.constructor.of.pipeSchemaIntoInstance(
this.constructor.dejsonifySchema
).parse(this, params)
}
dejsonifyPromise(params?: Partial<z.ParseParams>) {
return this.constructor.of.pipeSchemaIntoInstance(
this.constructor.dejsonifySchema
).parseAsync(this, params)
}
dejsonifyEffect(params?: Partial<z.ParseParams>) {
return parseZodSchemaEffect(
this.constructor.of.pipeSchemaIntoInstance(this.constructor.dejsonifySchema),
this,
params,
)
}
})
.build()

View File

@@ -8,15 +8,13 @@ export const MobXObservableZodSchemaObject = trait
.staticAbstract(Super => class extends Super { .staticAbstract(Super => class extends Super {
declare readonly schema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, object, object> declare readonly schema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, object, object>
}) })
.implement(Super => class MobXObservableZodSchemaObjectImpl extends Super { .implement(Super => class ObservableZodSchemaObject extends Super {
declare ["constructor"]: typeof MobXObservableZodSchemaObjectImpl
constructor(...args: any[]) { constructor(...args: any[]) {
super(...args) super(...args)
makeObservable(this, makeObservable(this,
mapValues( mapValues(
this.constructor.schema.shape, (this.constructor as typeof ObservableZodSchemaObject).schema.shape,
() => observable, () => observable,
) )
) )

View File

@@ -1,7 +1,7 @@
import { implStaticInstantiableThis, trait } from "@thilawyn/traitify-ts" import { ImplStatic, trait } from "@thilawyn/traitify-ts"
import { Class, HasRequiredKeys } from "type-fest" import { Class, HasRequiredKeys } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { parseZodSchemaEffect, stripZodObjectDefaults } from "../util" import { parseZodSchemaEffect } from "../util"
type CreateArgs<Input extends object> = ( type CreateArgs<Input extends object> = (
@@ -12,104 +12,111 @@ type CreateArgs<Input extends object> = (
export const ZodSchemaObject = < export const ZodSchemaObject = <
T extends z.ZodRawShape, SchemaT extends z.ZodRawShape,
Catchall extends z.ZodTypeAny, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>, schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
) => trait ) => trait
.implement(Super => { .implement(Super => class ZodSchemaObject extends Super {
class ZodSchemaObjectImpl extends Super { static readonly schema = schema
static readonly schema = stripZodObjectDefaults(schemaWithDefaults) static readonly schemaWithDefaultValues = schemaWithDefaultValues
static readonly schemaWithDefaults = schemaWithDefaults
static transform<
static pipeSchemaIntoInstance<
Instance extends Values, Instance extends Values,
SchemaT extends z.ZodRawShape, T extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam, UnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, Catchall extends z.ZodTypeAny,
SchemaOutput extends Values, Output extends Values,
SchemaInput, Input,
>( >(
this: Class<Instance, [values: Values]>, this: Class<Instance, [values: Values]>,
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaOutput, SchemaInput>, schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>,
) { ) {
return schema.transform(values => new this(values)) return schema.transform(values => new this(values))
} }
static pipeInstanceIntoSchema<
Self extends Class<Values, [values: Values]>,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaOutput,
SchemaInput extends Values,
>(
this: Self,
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaOutput, SchemaInput>,
) {
return z.instanceof(this).pipe(schema)
}
static create< static create<
Instance extends Values Instance extends Values
>( >(
this: Class<Instance, [values: Values]>, this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues> ...[values, params]: CreateArgs<PartialValues>
) { ) {
const t = implStaticInstantiableThis(ZodSchemaObjectImpl, this) return this
.transform(this.schemaWithDefaultValues)
return t
.pipeSchemaIntoInstance(t.schemaWithDefaults)
.parse(values, params) .parse(values, params)
} }
static createPromise< static createPromise<
Instance extends Values Instance extends Values
>( >(
this: Class<Instance, [values: Values]>, this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues> ...[values, params]: CreateArgs<PartialValues>
) { ) {
const t = implStaticInstantiableThis(ZodSchemaObjectImpl, this) return this
.transform(this.schemaWithDefaultValues)
return t
.pipeSchemaIntoInstance(t.schemaWithDefaults)
.parseAsync(values, params) .parseAsync(values, params)
} }
static createEffect< static createEffect<
Instance extends Values Instance extends Values
>( >(
this: Class<Instance, [values: Values]>, this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues> ...[values, params]: CreateArgs<PartialValues>
) { ) {
const t = implStaticInstantiableThis(ZodSchemaObjectImpl, this)
return parseZodSchemaEffect( return parseZodSchemaEffect(
t.pipeSchemaIntoInstance(t.schemaWithDefaults), this.transform(this.schemaWithDefaultValues),
values, values,
params, params,
) )
} }
}
return ZodSchemaObjectImpl
}) })
.build() .build()
export type ZodSchemaObjectTrait< export type ZodSchemaObjectTrait<
T extends z.ZodRawShape, SchemaT extends z.ZodRawShape,
Catchall extends z.ZodTypeAny, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
> = ( > = (
ReturnType< ReturnType<
typeof ZodSchemaObject<T, Catchall, Values, PartialValues> typeof ZodSchemaObject<
SchemaT,
SchemaUnknownKeys,
SchemaCatchall,
SchemaWithDefaultValuesT,
SchemaWithDefaultValuesUnknownKeys,
SchemaWithDefaultValuesCatchall,
Values,
PartialValues
>
> >
) )

View File

@@ -1,6 +1,69 @@
import { AbstractClass } from "type-fest" import { AbstractClass, Class as ConcreteClass } from "type-fest"
/**
* Represents the possible types of a class.
*/
export type ClassType = "AbstractClass" | "Class"
/**
* Represents a class based on the specified type.
* @template Type - The type of the class ("AbstractClass" or "Class").
* @template T - The type parameter of the class.
* @template Arguments - The type of arguments the class constructor takes.
*/
export type Class<
Type extends ClassType,
T,
Arguments extends unknown[] = any[],
> = (
Type extends "AbstractClass"
? AbstractClass<T, Arguments>
: Type extends "Class"
? ConcreteClass<T, Arguments>
: never
)
/**
* Gets the type of a class (either "Class" or "AbstractClass").
* @template C - The class type to determine.
*/
export type GetClassType<C> = (
C extends ConcreteClass<any>
? "Class"
: C extends AbstractClass<any>
? "AbstractClass"
: never
)
/**
* Represents an array of instances corresponding to the provided classes.
* @template Classes - An array of classes extending AbstractClass.
*/
export type ClassesInstances<Classes extends readonly AbstractClass<any>[]> = (
Classes extends [infer Class, ...infer Rest]
? Class extends AbstractClass<any>
? Rest extends AbstractClass<any>[]
? [InstanceType<Class>, ...ClassesInstances<Rest>]
: never
: never
: []
)
/**
* Represents an intersection of instances of the provided classes.
* @template Classes - An array of classes extending AbstractClass.
*/
export type ClassesInstancesIntersection<Classes extends readonly AbstractClass<any>[]> = (
Classes extends [infer Class, ...infer Rest]
? Class extends AbstractClass<any>
? Rest extends AbstractClass<any>[]
? InstanceType<Class> & ClassesInstancesIntersection<Rest>
: never
: never
: {}
)
/** /**
* Represents the static members of a class. * Represents the static members of a class.
* @template Class - A class extending AbstractClass. * @template Class - A class extending AbstractClass.
@@ -8,3 +71,31 @@ import { AbstractClass } from "type-fest"
export type StaticMembers<Class extends AbstractClass<any>> = ( export type StaticMembers<Class extends AbstractClass<any>> = (
Omit<Class, "prototype"> Omit<Class, "prototype">
) )
/**
* Represents an array of static members corresponding to the provided classes.
* @template Classes - An array of classes extending AbstractClass.
*/
export type ClassesStaticMembers<Classes extends readonly AbstractClass<any>[]> = (
Classes extends [infer Class, ...infer Rest]
? Class extends AbstractClass<any>
? Rest extends AbstractClass<any>[]
? [StaticMembers<Class>, ...ClassesStaticMembers<Rest>]
: never
: never
: []
)
/**
* Represents an intersection of static members of the provided classes.
* @template Classes - An array of classes extending AbstractClass.
*/
export type ClassesStaticMembersIntersection<Classes extends readonly AbstractClass<any>[]> = (
Classes extends [infer Class, ...infer Rest]
? Class extends AbstractClass<any>
? Rest extends AbstractClass<any>[]
? StaticMembers<Class> & ClassesStaticMembersIntersection<Rest>
: never
: never
: {}
)

View File

@@ -1,4 +1,4 @@
export * from "./class" export * from "./class"
export * from "./effect" export * from "./effect"
export * from "./extend" export * from "./extend"
export * from "./zod" export * from "./misc"

3
src/util/misc.ts Normal file
View File

@@ -0,0 +1,3 @@
export function identity<T>(value: T) {
return value
}

View File

@@ -1,25 +0,0 @@
import { mapValues } from "lodash-es"
import { z } from "zod"
export function stripZodObjectDefaults<
T extends z.ZodRawShape,
UnknownKeys extends z.UnknownKeysParam,
Catchall extends z.ZodTypeAny,
Output,
Input,
>(
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>
) {
return schema.extend(
mapValues(schema.shape, v =>
v instanceof z.ZodDefault
? v.removeDefault()
: v
) as {
[K in keyof T]: T[K] extends z.ZodDefault<z.ZodTypeAny>
? ReturnType<T[K]["removeDefault"]>
: T[K]
}
)
}

View File

@@ -8,7 +8,7 @@
// "allowImportingTsExtensions": true, // "allowImportingTsExtensions": true,
// "noEmit": true, // "noEmit": true,
"declaration": true, "declaration": true,
// "composite": true, "composite": true,
"strict": true, "strict": true,
"downlevelIteration": true, "downlevelIteration": true,
"skipLibCheck": true, "skipLibCheck": true,

View File

@@ -1,11 +0,0 @@
import { defineConfig } from "tsup"
export default defineConfig({
entry: ["src/lib.ts", "src/schema/lib.ts"],
format: ["esm", "cjs"],
dts: true,
splitting: false,
sourcemap: true,
clean: true,
})