-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
71 additions
and
33 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {SuperType} from "../SuperType"; | ||
import {TypeAbstract} from "../TypeAbstract"; | ||
|
||
export type MutateFunction<TIn, TOut> = (value: TIn) => TOut; | ||
|
||
export class Mutate<TValue, SValue> extends SuperType<TValue, SValue> { | ||
readonly #mutate: MutateFunction<SValue, TValue>; | ||
constructor(type: TypeAbstract<SValue>, mutate: MutateFunction<SValue, TValue>) { | ||
super(type); | ||
this.#mutate = mutate; | ||
} | ||
|
||
public async validate(payload: unknown): Promise<TValue> { | ||
const value = await this.type.validate(payload); | ||
|
||
return this.#mutate(value); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,23 +1,15 @@ | ||
import {Fields, List, Union, RecordType} from "./Type"; | ||
import {KeyOf} from "@bunt/util"; | ||
import {Fields, Int, Nullable} from "./Type"; | ||
import {TypeAbstract} from "./TypeAbstract"; | ||
|
||
export type FieldFn<T> = () => T; | ||
export type FieldType<T> = T | FieldFn<T>; | ||
export type FieldMayFn<T> = T | (() => T); | ||
|
||
export type FieldsSchema<T> = { | ||
[K in keyof T]-?: T[K] extends Array<infer S> | ||
? FieldType<List<S>> | ||
: T[K] extends Date | ||
? FieldType<TypeAbstract<T[K]>> | ||
: T[K] extends Record<any, any> | ||
? FieldType<Fields<T[K]> | Union<T[K]> | typeof RecordType> | ||
: FieldType<TypeAbstract<T[K]>>; | ||
export type FieldsSchema<T extends Record<string, any>> = { | ||
[K in KeyOf<T>]-?: FieldSelectType<T[K]>; | ||
}; | ||
|
||
export type ObjectFields<T> = T extends Promise<infer A> | ||
? FieldsSchema<Exclude<A, undefined | null>> | ||
: FieldsSchema<Exclude<T, undefined | null>>; | ||
export type FieldSelectType<T> = FieldMayFn<TypeAbstract<T>>; | ||
|
||
export type FieldSelectType<T> = FieldType<TypeAbstract<T>>; | ||
|
||
export type ObjectTypeMerge<T extends Record<string, any>> = Fields<T> | ObjectFields<T>; | ||
type Foo = {foo?: number; bar: number}; | ||
new Fields<Foo>({foo: Int, bar: Int}); | ||
new Fields<Foo>({foo: new Nullable(Int), bar: Int}); |
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