Don't miss the next edition. Subscribe to the newsletter.
Published at Aug 19, 2024, 3:00 PMValidate Data in Your Nuxt Server Routes Ready for your weekly Vue & Nuxt dose? Weekly Vue News #159 Validate Data in Your Nuxt Server Routes View online Hi 👋 No big news from me again — just enjoying the summer with my family and prepping some fresh content for you. In this issue, you'll find a handy Nuxt tip on validating data in your server routes. Hope you enjoy it! Have a lovely week ☀️ Vue 📕 Simplifying Data Fetching in Vue with Data Loaders 👉🏻 Eduardo introduces "data loaders" - an API he created to streamline data fetching with Vue Router. 📕 Best Practices for Fetching Data in Vue.js 👉🏻 This article shares some of the best practices and common ways for fetching data in Vue.js used by almost all Vue developers. 📹 Vue is getting EVEN FASTER? 👉🏻 Vue 3.5 includes lazy hydration strategies for async components. 👉🏻 Nuxt 4 will provide an easy abstraction. 🎓 Senior Vue.js Developer exam launching on August 27th * 👉🏻 The official certification of competence for the Vue.js Framework. 👉🏻 Demonstrate your Senior Vue.js competency. 👉🏻 Enhance your career opportunities. Nuxt 📕 Code, Draw, Deploy: A drawing app with Nuxt & Cloudflare R2 👉🏻The creator of Nuxt shares his experience of creating a web-based drawing application built with Nuxt and using Cloudflare R2 for storage. 📕 How to Read and Write Cookies in Nuxt 3 * 👉🏻 Learn how to read, write and manage cookies in Nuxt 3 using the useCookie built-in composable. 📕 Nuxt Form Validation with Precognition and Zod 👉🏻 Implement rock-solid form validation using Precognition and Zod in your Nuxt apps. 📕 Easy RPC with Nuxt: Making a Todo App 👉🏻 Learn how to make a simple to-do app with Nuxt. 👉🏻 Instead of manually defining server routes and fetching them on the client, the author uses Prim+RPC to define functions on Nuxt's server and simply call those functions directly from the client. 🛠️ Super SaaS * 👉🏻 A comprehensive Nuxt 3 SaaS starter kit. 👉🏻 Focus on building your product and not worry about setting up auth, database, infrastructure, payments, and more. 💡 Nuxt Tip: Validate Data in Your Server Routes When you are working with Nuxt server routes, you should always validate the data that you receive from the client before processing it. This is important to ensure that your application is secure and that you are not vulnerable to attacks such as SQL injection or cross-site scripting. Additionally, validating your data can help you catch errors early on and prevent them from causing issues later on in your application. Luckily, h3 provides a simple and powerful way to validate data in your Nuxt server routes. h3 is the H(TTP) server framework which is used by Nitro , which is the server engine used in Nuxt 3+. It provides the following utility methods to help you validate your data: getValidatedQuery to validate query parameters getValidatedRouterParams to validate route parameters readValidatedBody to validate the request body h3 allows you to use the validation library of your choice like Zod , Yup , or Joi to validate your data. For this example, we will use Zod to validate the data in our Nuxt server routes with the following schema: 1 import { z } from ' zod ' ; 2 3 const productSchema = z . object ({ 4 name : z . string (). min ( 3 ). max ( 20 ), 5 id : z . number ({ coerce : true }). positive (). int (), 6 }); Let's see how we can use these validation utils to validate query parameters in our Nuxt server routes. Therefore, you can use the getValidatedQuery method provided by h3 instead of getQuery . This method takes the schema that you want to validate the query parameters against and returns the validated query parameters: 1 export default defineEventHandler ( async ( event ) => { 2 const query = await getValidatedQuery ( event , productSchema . parse ); 3 4 return { 5 query , 6 }; 7 }); If you send a valid query like /?name=Product&id=1 , you will get the following response: 1 { 2 " query " : { 3 " name " : " Product " , 4 " id " : 1 5 } 6 } If you send an invalid request that does not match the schema, h3 will throw a 400 Validation Error . 📗 Read the whole article 📅 Events PrageVue 2024 (17 September 2024, Prag, Czech Republic) vuejs.de Conf (8 - 9 October 2024, Bonn, Germany) Vue Fes Japan 2024 (19 October 2024, Otemachi, Japan) VueConf Toronto (18 - 20 November 2024, Toronto, Canada) 💬 Quote of the week 🧑🏻💻 In Other News 📕 How Google handles JavaScript throughout the indexing process 👉🏻 MERJ and Vercel analyzed over 100,000 Googlebot fetches in an attempt to empirically demystify Google’s approach. 📕 Why Unknown Types Are Useful 👉🏻 TypeScript’s unknown, specifically. 📹 Tech Documentaries 👉🏻 Really cool cinematic documentaries on your favourite OSS projects like Vue and TypeScript. 🛠️ Fluid for Tailwind CSS 👉🏻 A shorthand for responsive code in Tailwind to make Tailwind code drier. 😂 Fun 🔗 Want more Vue & Nuxt content? More Exclusive Vue Tips : Join Michael Thiessen's newsletter and get great Vue tips and insights delivered to your inbox each week. Weekly Vue & Nuxt Videos : You must subscribe Alexander Lichter's YouTube channel if you are interested in Vue & Nuxt. DejaVue Podcast : A weekly podcast about Vue.js and the ecosystem around it. Comments? Join the discussion about this issue in our Discord community . Until next week, Michael Hoffmann (Curator) To support me: 😘 Recommend the newsletter to your friends: it really helps! 💸 Sponsor this newsletter 🧵 Repost the latest X thread 📨 Reply to this email: feedback is welcome * Some of my links are affiliate links; if you make a purchase, I gain a small percentage at no extra cost. Thank you for supporting my newsletter. Unsubscribe from this newsletter Holzapfelkreuther Str. 19, 81375 Munich, Germany {{ TrackView }}