Created
November 13, 2022 18:32
-
-
Save mattiamanzati/4e90a88f5a6695f999b0c241edae00e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Show } from "@fp-ts/codec/Show" | |
import * as C from "@fp-ts/data/Context" | |
interface Meta<P> { | |
meta: P | |
} | |
interface MetaSchema<C> extends Meta<unknown> { | |
_tag: unknown | |
_C?: C | |
} | |
function make<T extends Meta<unknown>>(_tag: C.Tag<T>, meta: T["meta"]): MetaSchema<T> { | |
return { _tag, meta } | |
} | |
interface StringType extends Meta<{}> {} | |
export const StringType = C.Tag<StringType>() | |
export const string: MetaSchema<StringType> = make(StringType, {}) | |
interface MinLengthRefinement extends Meta<{ of: Meta<unknown>; length: number }> {} | |
export const MinLengthRefinement = C.Tag<MinLengthRefinement>() | |
export function minLength<P>( | |
of: MetaSchema<P>, | |
length: number | |
): MetaSchema<P | MinLengthRefinement> { | |
return make(MinLengthRefinement, { of, length }) | |
} | |
interface ArrayType extends Meta<{ of: Meta<unknown> }> {} | |
export const ArrayType = C.Tag<ArrayType>() | |
export function array<P>(of: MetaSchema<P>): MetaSchema<P | ArrayType> { | |
return make(ArrayType, { of }) | |
} | |
interface ShowBuilder<T>{ | |
build: (meta: Meta<T>) => Show<any> | |
} | |
function showFor<C>(schema: MetaSchema<C>) { | |
return (context: C.Context<C extends infer T ? ShowBuilder<T> : never>): Show<any> { | |
} | |
} | |
const schema = array(minLength(string, 3)) | |
const show = showFor(schema) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment