kiliman / remix-params-helper

This package makes it simple to use Zod with standard URLSearchParams and FormData which are typically used in Remix apps.
MIT License
246 stars 16 forks source link

Is there a way to pass FormData that's already consumed? #43

Open nobleach opened 1 month ago

nobleach commented 1 month ago

Is there a way to pass FormData that's already consumed?

I realize I could just use straight Zod at that point but I really like the API of this project. Instead of passing my Request object, it'd be cool to do

const formData = await request.formData();
// Pull some value that helps me decide which validation schema to use
const employeeType = formData.get('employeeType');

  const result = await getFormData(formData, getValidationSchema(employeeType));

  if (!result.success) {
    return json(result.errors, { status: 400 });
  }

I didn't know if there was a technical reason that the entire request object needed to be passed. As we know the body cannot be consumed twice so there's no potential for getting data from it to assist in making validation decisions.

Thoughts?

nobleach commented 1 month ago

Just to argue with myself here.... this would leave the responsibility of handling multiple instances in an object (like an array of checkboxs) to the caller.

hilja commented 1 week ago

That would be nice. If I need to read the request body before getFormData then I need to const reqClone = request.clone(). I could just pass in the body I just read.