Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
7.74k stars 246 forks source link

Tools for working with FormData #3986

Open MrOxMasTer opened 6 days ago

MrOxMasTer commented 6 days ago

What is the problem this feature would solve?

FormData validation

What is the feature you are proposing to solve the problem?

Use schemas to validate formData

What alternatives have you considered?

https://www.npmjs.com/package/zod-form-data

MrOxMasTer commented 4 days ago

I'm not so versed in Effect yet that I can come up with anything, but I guess you could probably do something like this:

import { Schema as S } from “effect”;

const TodoSchema = S.Struct({
    id: S.String,
    content: S.String
});

const TodoFormDataSchema = S.formData(Todo);
const decoded = S.decodeUnknownPromise(TodoFormDataSchema)
const encoded = S.encodePromise(TodoFormDataSchema)

const serverAction = async (formData: FormData) => {
    const decode = await decoded(formData) // -> { id: “...”, content: “...” };
    const encode = await encoded(decode) // -> formData
}

await serverAction();

It's also worth considering other things that can be passed in formData: image

MrOxMasTer commented 12 hours ago

You should add more types related to FormData, these are: image

const SchemaFormData = S.FormData(S.Struct({
  text: S.Text(),
  numeric: S.Numeric(),
  checkbox: S.Checkbox(),
  file: S.File(),
  repeatable: S.Repeatable(),
  repeatableOfType: S.RepeatableOfType()
}))