Files
zod-schema-class/src/tests.ts
Julien Valverdé c652aae238
Some checks failed
continuous-integration/drone/push Build is failing
ZodSchemaClassExtender work
2024-02-26 00:59:58 +01:00

91 lines
2.3 KiB
TypeScript

import { Implements } from "@thilawyn/traitify-ts"
import { z } from "zod"
import { zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
import { dejsonify, jsonify } from "./schema/jsonify"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
import { StaticMembers } from "./util"
const exp = zodSchemaClass
.schema({
schema: z.object({
/** User ID */
id: z.bigint(),
/** Username */
name: z.string(),
}),
defaultValues: { id: -1n },
})
.jsonifiable({
jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id)
}),
dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id)
}),
})
.expresses(MobXObservableZodSchemaObject)
.build()
@exp.staticImplements
class User extends exp.extends implements Implements<typeof exp> {}
User.defaultValues
const inst = User.create({ id: 1n, name: "" })
// console.log(inst)
const jsonifiedUser = await inst.jsonifyPromise()
const AdminUserProto = User.extend()
.schema({
schema: s => s.extend({
name: z.literal("Admin"),
prout: z.string(),
}),
defaultValues: v => ({ ...v, name: "Admin" as const }),
})
.jsonifiable({
jsonifySchema: (s, json) => json.extend({
prout: s.shape.prout
})
})
class AdminUser extends AdminUserProto.toClass() {}
const subInst = await AdminUser.createPromise({ id: 2n, prout: "" })
console.log(subInst)
// class Box<T> {
// declare ["constructor"]: typeof Box
// constructor(public value: T) {}
// otherMethod() {
// return "prout" as const
// }
// newBox<
// C extends Class<I, ConstructorParameters<typeof Box>>,
// I extends Box<any>,
// T,
// >(
// // this: Omit<BaseClass<T>, "constructor"> & { ["constructor"]: Class<I> }
// this: { ["constructor"]: C | Class<I, ConstructorParameters<typeof Box>> },
// value: T,
// ) {
// // this.otherMethod()
// return new this.constructor(value)
// }
// }
// class UnrelatedClass {}
// class SuperBox<T> extends Box<T> {
// declare ["constructor"]: typeof SuperBox
// }
// const value = new SuperBox(69).newBox(69)