0.1.0 #1
19
package.json
19
package.json
@@ -11,12 +11,22 @@
|
|||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"import": {
|
"import": {
|
||||||
"types": "./dist/lib.d.mts",
|
"types": "./dist/schemable.d.mts",
|
||||||
"default": "./dist/lib.mjs"
|
"default": "./dist/schemable.mjs"
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"types": "./dist/lib.d.cts",
|
"types": "./dist/schemable.d.cts",
|
||||||
"default": "./dist/lib.cjs"
|
"default": "./dist/schemable.cjs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"./jsonifiable": {
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/jsonifiable.d.mts",
|
||||||
|
"default": "./dist/jsonifiable.mjs"
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/jsonifiable.d.cts",
|
||||||
|
"default": "./dist/jsonifiable.cjs"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -29,6 +39,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@thilawyn/thilatrait": "^20231230.0.0",
|
"@thilawyn/thilatrait": "^20231230.0.0",
|
||||||
|
"decimal.js": "^10.4.3",
|
||||||
"effect": "^2.0.0-next.62",
|
"effect": "^2.0.0-next.62",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"type-fest": "^4.9.0",
|
"type-fest": "^4.9.0",
|
||||||
|
|||||||
2
src/jsonifiable/index.ts
Normal file
2
src/jsonifiable/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from "./makeJsonifiableSchemableClass"
|
||||||
|
export * from "./schema"
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { SchemableClass, SchemableConfig } from "."
|
import { SchemableClass, SchemableConfig } from ".."
|
||||||
import { parseZodTypeEffect } from "./util"
|
import { parseZodTypeEffect } from "../util"
|
||||||
|
|
||||||
|
|
||||||
export function makeJsonifiableSchemableClass<
|
export function makeJsonifiableSchemableClass<
|
||||||
18
src/jsonifiable/schema/bigint.ts
Normal file
18
src/jsonifiable/schema/bigint.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
|
export const jsonifyBigIntSchema = <S extends z.ZodBigInt>(schema: S) =>
|
||||||
|
schema.transform(v => v.toString())
|
||||||
|
|
||||||
|
export const dejsonifyBigIntSchema = <S extends z.ZodBigInt>(schema: S) =>
|
||||||
|
z
|
||||||
|
.string()
|
||||||
|
.transform(v => {
|
||||||
|
try {
|
||||||
|
return BigInt(v)
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.pipe(schema)
|
||||||
18
src/jsonifiable/schema/date.ts
Normal file
18
src/jsonifiable/schema/date.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
|
export const jsonifyDateSchema = <S extends z.ZodDate>(schema: S) =>
|
||||||
|
schema.transform(v => v.toString())
|
||||||
|
|
||||||
|
export const dejsonifyDateSchema = <S extends z.ZodDate>(schema: S) =>
|
||||||
|
z
|
||||||
|
.string()
|
||||||
|
.transform(v => {
|
||||||
|
try {
|
||||||
|
return new Date(v)
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.pipe(schema)
|
||||||
19
src/jsonifiable/schema/decimal.ts
Normal file
19
src/jsonifiable/schema/decimal.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { Decimal } from "decimal.js"
|
||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
|
export const jsonifyDecimalSchema = <S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>>(schema: S) =>
|
||||||
|
schema.transform(v => v.toJSON())
|
||||||
|
|
||||||
|
export const dejsonifyDecimalSchema = <S extends z.ZodType<Decimal, z.ZodTypeDef, Decimal>>(schema: S) =>
|
||||||
|
z
|
||||||
|
.string()
|
||||||
|
.transform(v => {
|
||||||
|
try {
|
||||||
|
return new Decimal(v)
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.pipe(schema)
|
||||||
3
src/jsonifiable/schema/index.ts
Normal file
3
src/jsonifiable/schema/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export * from "./bigint"
|
||||||
|
export * from "./date"
|
||||||
|
export * from "./decimal"
|
||||||
12
src/tests.ts
12
src/tests.ts
@@ -8,9 +8,17 @@ const UserSchema = z.object({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
const UserSchemaObject = makeSchemableClass({ schema: UserSchema })
|
const UserSchemableObject = makeSchemableClass({ schema: UserSchema })
|
||||||
|
|
||||||
class User extends UserSchemaObject {}
|
// const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemableObject, {
|
||||||
|
// jsonifySchema: ({ schema, s }) => schema.extend({
|
||||||
|
// }),
|
||||||
|
|
||||||
|
// dejsonifySchema: ({ schema, s }) => schema.extend({
|
||||||
|
// }),
|
||||||
|
// })
|
||||||
|
|
||||||
|
class User extends UserSchemableObject {}
|
||||||
|
|
||||||
const user1 = new User({ id: 1n })
|
const user1 = new User({ id: 1n })
|
||||||
const user2 = newSchemable(User, { id: 2n })
|
const user2 = newSchemable(User, { id: 2n })
|
||||||
|
|||||||
Reference in New Issue
Block a user