-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from oliver-oloughlin/patch/typing-fix
Patch/typing fix
- Loading branch information
Showing
27 changed files
with
471 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
}, | ||
"test": { | ||
"include": ["./tests"] | ||
}, | ||
"imports": { | ||
"#/": "./src/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./zod/zod_model.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type { | ||
TypeOf, | ||
ZodDefault, | ||
ZodObject, | ||
ZodRawShape, | ||
ZodType, | ||
} from "https://deno.land/x/[email protected]/mod.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { | ||
TypeOf, | ||
ZodDefault, | ||
ZodObject, | ||
ZodRawShape, | ||
ZodType, | ||
} from "./deps.ts" | ||
import type { | ||
KeysOfThatDontExtend, | ||
KeysOfThatExtend, | ||
KvValue, | ||
Model, | ||
} from "#/types.ts" | ||
|
||
/*****************/ | ||
/* */ | ||
/* TYPES */ | ||
/* */ | ||
/*****************/ | ||
|
||
export type ZodInsertModel<T extends ZodType<KvValue>> = T extends | ||
ZodDefault<infer Z extends ZodType> ? ( | ||
( | ||
Z extends ZodObject<ZodRawShape> ? ZodInsertModel<Z> | ||
: TypeOf<Z> | ||
) | undefined | ||
) | ||
: T extends ZodObject<infer U> ? ZodObjectInsertModel<U> | ||
: TypeOf<T> | ||
|
||
export type ZodObjectInsertModel<T extends ZodRawShape> = | ||
& { | ||
[K in KeysOfThatExtend<T, ZodDefault<ZodType>>]?: ZodInsertModel< | ||
T[K] | ||
> | ||
} | ||
& { | ||
[K in KeysOfThatDontExtend<T, ZodDefault<ZodType>>]: ZodInsertModel<T[K]> | ||
} | ||
|
||
/*****************/ | ||
/* */ | ||
/* FUNCTIONS */ | ||
/* */ | ||
/*****************/ | ||
|
||
/** | ||
* Create a model from a Zod schema. | ||
* | ||
* Correctly parses the insert model from the given schema. | ||
* | ||
* @param schema - Zod schema. | ||
* @returns A model with base type and insert model. | ||
*/ | ||
export function zodModel< | ||
const T1 extends KvValue, | ||
const T2 extends ZodType<T1>, | ||
>( | ||
schema: T2, | ||
): Model<T1, ZodInsertModel<T2>> { | ||
return { | ||
parse: (data) => schema.parse(data), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.