Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created November 13, 2022 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattiamanzati/4e90a88f5a6695f999b0c241edae00e8 to your computer and use it in GitHub Desktop.
Save mattiamanzati/4e90a88f5a6695f999b0c241edae00e8 to your computer and use it in GitHub Desktop.
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