Compare commits
16 Commits
b7b4abcbe2
...
form
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db7608f7c3 | ||
|
|
b78f99e808 | ||
|
|
86dde2d286 | ||
|
|
596e0942c5 | ||
|
|
cb61713cce | ||
|
|
1b2b68fbae | ||
|
|
35a8037f5a | ||
|
|
7aef7ae796 | ||
|
|
1bfbeba934 | ||
|
|
fc4295894f | ||
|
|
ab0dce107d | ||
|
|
9436602443 | ||
|
|
66de31706c | ||
|
|
8925fe6336 | ||
|
|
fe8ca23d37 | ||
|
|
d48f20a59d |
@@ -35,6 +35,6 @@
|
|||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"effect": "^3.13.0",
|
"effect": "^3.13.0",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"reffuse": "^0.1.6"
|
"reffuse": "^0.1.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
|
|
||||||
|
|
||||||
export interface Form<A, I, R> {
|
export interface Form<A, I, R> {
|
||||||
|
|||||||
69
packages/extension-form/src/internal/FormField.ts
Normal file
69
packages/extension-form/src/internal/FormField.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import type { Effect, Schema } from "effect"
|
||||||
|
import type * as Formify from "./Formify.js"
|
||||||
|
|
||||||
|
|
||||||
|
export interface FormField<S extends Schema.Schema.Any> {
|
||||||
|
readonly schema: S
|
||||||
|
}
|
||||||
|
|
||||||
|
export const makeFormField = <S extends Schema.Schema.Any>(
|
||||||
|
schema: S,
|
||||||
|
get: Effect.Effect<S["Type"]>,
|
||||||
|
set: (value: S["Type"]) => Effect.Effect<void>,
|
||||||
|
): FormField<S> => {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UnionFormField<
|
||||||
|
S extends Schema.Union<Members>,
|
||||||
|
Members extends ReadonlyArray<Schema.Schema.All>,
|
||||||
|
> extends FormField<S> {
|
||||||
|
readonly member: Formify.Formify<Members[number]>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TupleFormField<
|
||||||
|
S extends Schema.TupleType<Elements, Rest>,
|
||||||
|
Elements extends Schema.TupleType.Elements,
|
||||||
|
Rest extends Schema.TupleType.Rest,
|
||||||
|
> extends FormField<S> {
|
||||||
|
readonly elements: [...{ readonly [K in keyof Elements]: Formify.Formify<Elements[K]> }]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ArrayFormField<
|
||||||
|
S extends Schema.Array$<Value>,
|
||||||
|
Value extends Schema.Schema.Any,
|
||||||
|
> extends FormField<S> {
|
||||||
|
readonly elements: readonly Formify.Formify<Value>[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type StructFormField<
|
||||||
|
S extends Schema.Struct<Fields>,
|
||||||
|
Fields extends Schema.Struct.Fields,
|
||||||
|
> = (
|
||||||
|
& FormField<S>
|
||||||
|
& { readonly fields: { readonly [K in keyof Fields]: Formify.Formify<Fields[K]> } }
|
||||||
|
& {
|
||||||
|
[K in keyof Fields as Fields[K] extends
|
||||||
|
Schema.tag<infer _> ? K : never
|
||||||
|
]: Fields[K] extends
|
||||||
|
Schema.tag<infer Tag> ? Tag : never
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
export interface GenericFormField<S extends Schema.Schema.Any> extends FormField<S> {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export interface PropertySignatureFormField<
|
||||||
|
S extends Schema.PropertySignature<TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R>,
|
||||||
|
TypeToken extends Schema.PropertySignature.Token,
|
||||||
|
Type,
|
||||||
|
Key extends PropertyKey,
|
||||||
|
EncodedToken extends Schema.PropertySignature.Token,
|
||||||
|
Encoded,
|
||||||
|
HasDefault extends boolean = false,
|
||||||
|
R = never,
|
||||||
|
> {
|
||||||
|
readonly propertySignature: S
|
||||||
|
readonly value: Type
|
||||||
|
}
|
||||||
51
packages/extension-form/src/internal/Formify.ts
Normal file
51
packages/extension-form/src/internal/Formify.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import { Schema } from "effect"
|
||||||
|
import type * as FormField from "./FormField.js"
|
||||||
|
|
||||||
|
|
||||||
|
export type Formify<S> = (
|
||||||
|
S extends Schema.Union<infer Members> ? FormField.UnionFormField<S, Members> :
|
||||||
|
S extends Schema.TupleType<infer Elements, infer Rest> ? FormField.TupleFormField<S, Elements, Rest> :
|
||||||
|
S extends Schema.Array$<infer Value> ? FormField.ArrayFormField<S, Value> :
|
||||||
|
S extends Schema.Struct<infer Fields> ? FormField.StructFormField<S, Fields> :
|
||||||
|
S extends Schema.Schema.Any ? FormField.GenericFormField<S> :
|
||||||
|
S extends Schema.PropertySignature<
|
||||||
|
infer TypeToken,
|
||||||
|
infer Type,
|
||||||
|
infer Key,
|
||||||
|
infer EncodedToken,
|
||||||
|
infer Encoded,
|
||||||
|
infer HasDefault,
|
||||||
|
infer R
|
||||||
|
> ? FormField.PropertySignatureFormField<S, TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R> :
|
||||||
|
never
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
const Login = Schema.Union(
|
||||||
|
Schema.Struct({
|
||||||
|
_tag: Schema.tag("ByEmail"),
|
||||||
|
email: Schema.String,
|
||||||
|
password: Schema.RedactedFromSelf(Schema.String),
|
||||||
|
}),
|
||||||
|
|
||||||
|
Schema.Struct({
|
||||||
|
_tag: Schema.tag("ByPhone"),
|
||||||
|
phone: Schema.String,
|
||||||
|
password: Schema.RedactedFromSelf(Schema.String),
|
||||||
|
}),
|
||||||
|
|
||||||
|
Schema.TaggedStruct("ByKey", {
|
||||||
|
id: Schema.String,
|
||||||
|
password: Schema.RedactedFromSelf(Schema.String),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
type LoginForm = Formify<typeof Login>
|
||||||
|
declare const loginForm: LoginForm
|
||||||
|
|
||||||
|
switch (loginForm.member._tag) {
|
||||||
|
case "ByEmail":
|
||||||
|
loginForm.member
|
||||||
|
break
|
||||||
|
case "ByPhone":
|
||||||
|
break
|
||||||
|
}
|
||||||
37
packages/extension-form/src/internal/guards.ts
Normal file
37
packages/extension-form/src/internal/guards.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { Array, Predicate, Record, Schema, Tuple } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
export const isTupleSchema = (u: unknown): u is Schema.Tuple<any> => (
|
||||||
|
Schema.isSchema(u) &&
|
||||||
|
Predicate.hasProperty(u, "elements") && Array.isArray(u.elements) &&
|
||||||
|
Predicate.hasProperty(u, "rest") && Array.isArray(u.rest)
|
||||||
|
)
|
||||||
|
|
||||||
|
export const isArraySchema = (u: unknown): u is Schema.Array$<any> => (
|
||||||
|
Schema.isSchema(u) &&
|
||||||
|
Predicate.hasProperty(u, "elements") && Array.isArray(u.elements) && Array.isEmptyArray(u.elements) &&
|
||||||
|
Predicate.hasProperty(u, "rest") && Array.isArray(u.rest) && Tuple.isTupleOf(u.rest, 1) &&
|
||||||
|
Predicate.hasProperty(u, "value")
|
||||||
|
)
|
||||||
|
|
||||||
|
export const isStructSchema = (u: unknown): u is Schema.Struct<any> => (
|
||||||
|
Schema.isSchema(u) &&
|
||||||
|
Predicate.hasProperty(u, "fields") && Predicate.isObject(u.fields) &&
|
||||||
|
Predicate.hasProperty(u, "records") && Array.isArray(u.records) && Array.isEmptyArray(u.records)
|
||||||
|
)
|
||||||
|
|
||||||
|
export const isRecordSchema = (u: unknown): u is Schema.Record$<any, any> => (
|
||||||
|
Schema.isSchema(u) &&
|
||||||
|
Predicate.hasProperty(u, "fields") && Predicate.isObject(u.fields) && Record.isEmptyRecord(u.fields) &&
|
||||||
|
Predicate.hasProperty(u, "records") && Array.isArray(u.records) &&
|
||||||
|
Predicate.hasProperty(u, "key") &&
|
||||||
|
Predicate.hasProperty(u, "value")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
const myTuple = Schema.Tuple(Schema.String)
|
||||||
|
const myArray = Schema.Array(Schema.String)
|
||||||
|
const myStruct = Schema.Struct({})
|
||||||
|
const myRecord = Schema.Record({ key: Schema.String, value: Schema.String })
|
||||||
|
|
||||||
|
console.log(isArraySchema(myTuple))
|
||||||
Reference in New Issue
Block a user