atinux / atidone

A full-stack application with Auth, SSR and SQL, running on the edge with Nuxt.
https://todos.nuxt.dev
MIT License
730 stars 73 forks source link

refactor: use vinejs instead of zod #7

Closed Barbapapazes closed 1 year ago

Barbapapazes commented 1 year ago

Hello,

@vinejs is just announce and it could be a better validation library (I never used zod but vine is extracted from Adonis and it's a great joy to use).

vercel[bot] commented 1 year ago

Someone is attempting to deploy a commit to a Personal Account owned by @Atinux on Vercel.

@Atinux first needs to authorize it.

netlify[bot] commented 1 year ago

Deploy Preview for nuxt-todos-edge ready!

Name Link
Latest commit d6bf14e8cd66d64cfc4dd7719f99bd439006da47
Latest deploy log https://app.netlify.com/sites/nuxt-todos-edge/deploys/648c20ed61d5c90008cb1ec7
Deploy Preview https://deploy-preview-7--nuxt-todos-edge.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Barbapapazes commented 1 year ago

Thanks to @thetutlage (thanks 🙏), schemas are now pre-compiled!

Read more in VineJS docs

https://github.com/Barbapapazes/nuxt-todos-edge/pull/1

atinux commented 1 year ago

Made changes to actually have utils:

As well as v object to not have to import vine each time.

Also added the throw createError into it

Barbapapazes commented 1 year ago

Made changes to actually have utils:

  • validateParams(event, schema)
  • validateQuery(event, schema)
  • validateBody(event, schema)

As well as v object to not have to import vine each time.

Also added the throw createError into it

I'm curious, could you explain why you choose to create validator in API instead using a schema? Is it to keep related code close orto be able to refector using a single function to handle errors? 🤔

atinux commented 1 year ago

Trying to keep each controller into its own file so when you delete it, you don't have to look elsewhere to find dead code.

Barbapapazes commented 1 year ago

Thanks! That's make a lot a sens!

atinux commented 1 year ago

Just added the support for pre-compiled schema so you can keep you schema elsewhere if you want, ex:

const createTodoValidator = v.compile(
  v.object({
    title: v.string().minLength(1).maxLength(100)
  })
)

export default eventHandler(async (event) => {
  const { title } = await validateBody(event, createTodoValidator)
  // ...
})
vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuxt-todos-edge ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 16, 2023 8:46am
atinux commented 1 year ago

I had to revert this PR since VineJS does not seems to work on Edge runtime:

CleanShot_2023-06-16_at_11 07 512x