Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
jameslporter opened this issue Apr 8, 2024 · 4 comments
Closed
1 task done

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

jameslporter opened this issue Apr 8, 2024 · 4 comments
Labels
question Further information is requested

Comments

@jameslporter
Copy link

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

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

@jameslporter jameslporter added the question Further information is requested label Apr 8, 2024
@TheEdoRan
Copy link
Owner

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
Copy link
Author

jameslporter commented Apr 8, 2024

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
Copy link
Owner

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants