Form work
Some checks failed
Lint / lint (push) Failing after 15s

This commit is contained in:
Julien Valverdé
2025-04-16 00:44:01 +02:00
parent ab0dce107d
commit fc4295894f
3 changed files with 38 additions and 31 deletions

View File

@@ -1,26 +1,6 @@
import { Schema } from "effect"
import type * as FormField from "./FormField.js"
export interface Form<A, I, R> {
readonly schema: Schema.Schema<A, I, R>
}
export type FormTree<S extends Schema.Schema<any>> = (
S extends Schema.Array$<any> ? FormField.ArrayFormField<S> :
S extends Schema.Struct<any> ? FormField.StructFormField<S> :
FormField.GenericFormField<S>
)
const MySchema = Schema.Struct({
name: Schema.String,
symbol: Schema.SymbolFromSelf,
values: Schema.Array(Schema.String),
})
type TestFormTree = FormTree<typeof MySchema>
declare const testFormTree: TestFormTree
testFormTree.fields.values.elements

View File

@@ -1,25 +1,31 @@
import type { Schema } from "effect"
import type * as Form from "./Form.ts"
import type * as FormTree from "./FormTree.ts"
export interface FormField<S extends Schema.Schema.Any> {
readonly schema: S
}
export interface GenericFormField<S extends Schema.Schema.Any> extends FormField<S> {
readonly _tag: "GenericFormField"
readonly schema: S
readonly value: S["Type"]
}
export interface ArrayFormField<S extends Schema.Array$<Schema.Schema.Any>> extends FormField<S> {
readonly _tag: "ArrayFormField"
readonly schema: S
readonly elements: readonly Form.FormTree<S["value"]>[]
export interface TupleFormField<S extends Schema.Tuple<readonly Schema.Schema.AnyNoContext[]>> extends FormField<S> {
readonly _tag: "TupleFormField"
readonly elements: { readonly [K in keyof S["elements"]]: FormTree.FormTree<S["elements"][K]> }
}
export interface StructFormField<S extends Schema.Struct<Schema.Struct.Fields>> extends FormField<S> {
readonly _tag: "StructFormField"
readonly schema: S
readonly fields: { [K in keyof S["fields"]]: Form.FormTree<S["fields"][K]> }
export interface ArrayFormField<S extends Schema.Array$<Schema.Schema.AnyNoContext>> extends FormField<S> {
readonly _tag: "ArrayFormField"
readonly elements: readonly FormTree.FormTree<S["value"]>[]
}
export interface StructFormField<S extends Schema.Struct<{
readonly [x: string]: Schema.Schema.AnyNoContext
readonly [x: number]: Schema.Schema.AnyNoContext
readonly [x: symbol]: Schema.Schema.AnyNoContext
}>> extends FormField<S> {
readonly _tag: "StructFormField"
readonly fields: { [K in keyof S["fields"]]: FormTree.FormTree<S["fields"][K]> }
}

View File

@@ -0,0 +1,21 @@
import { Schema } from "effect"
import type * as FormField from "./FormField.js"
export type FormTree<S extends Schema.Schema<any>> = (
S extends Schema.Array$<any> ? FormField.ArrayFormField<S> :
S extends Schema.Struct<any> ? FormField.StructFormField<S> :
FormField.GenericFormField<S>
)
const MySchema = Schema.Struct({
name: Schema.String,
symbol: Schema.SymbolFromSelf,
values: Schema.Array(Schema.String),
})
type TestFormTree = FormTree<typeof MySchema>
declare const testFormTree: TestFormTree
testFormTree.fields.values.elements