TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js (App Router) project.
https://next.next-safe-action.dev
BSD 3-Clause "New" or "Revised" License
1.38k stars 26 forks source link

[ASK] can I not destructure the validated object? #99

Closed jameslporter closed 1 month ago

jameslporter commented 1 month ago

Is there an existing issue for this?

Library version (optional)

6.2.0

Ask a question

Overall love this library, was really struggling with the useFormState hook and never could get to it to work right.

My question is regarding the action arguments. For small examples like on the website it makes sense to destructure the particular properties you need. However for a large data model with a dozen or more properties I'd much prefer to just take the object and pass it along to my DB without a huge destructure in the function signature and then restructuring it for saving it in the db. Hoping it's possible, if not this would be a feature request.

Thanks for your contributions!

Additional context

No response

TheEdoRan commented 1 month ago

was really struggling with the useFormState hook and never could get to it to work right.

Form Actions support is on the way, I'm waiting for useActionState hook to land in Next.js, since it will replace useFormState with a better implementation.

My question is regarding the action arguments. For small examples like on the website it makes sense to destructure the particular properties you need. However for a large data model with a dozen or more properties I'd much prefer to just take the object and pass it along to my DB without a huge destructure in the function signature and then restructuring it for saving it in the db. Hoping it's possible, if not this would be a feature request.

Can you please provide an example via code snippet? Thank you.

jameslporter commented 1 month ago

For clarity my validator is called tourClient because I have separate validators for frontend UX and more complete validators we use before allowing publishing of content. Here I'd love to just get the data object and save it to the DB instead of destructuring and restructuring the whole object.

export const createTour = action(tourClient, async ({_id,title,description,rentalPrice,rentalTimeFrame,tourScene,tourType, tourLanguage, dateNoEnd})=>{

    const data = {_id,title,description,rentalPrice,rentalTimeFrame,tourScene,tourType, tourLanguage, dateNoEnd}

    //save data object to db

    //return success/fail/etc

}

vs what I'd like is something like this

export const createTour = action(tourClient, async(data)=>{

    //save data object to db

    // return success/fail/etc

}
TheEdoRan commented 1 month ago

Oh, I see. Yes, you absolutely can use parsedInput without destructuring it. That input data is parsed, validated and/or transformed by the validator schema you provided, so it's safe to use it. Does it work for you?

jameslporter commented 1 month ago

Okay, now I got it!

Turns out my server action issues were due to middleware for my auth solution(supertokens). Now next-safe-actions are working properly. Before I was seeing the network request being made and json come back but the result was undefinded. Haven't tested the react useFormState again since discovering the underlying problem but I'm pretty sure that was the cause.

Thanks!