Menu

InversifyJS + OpenAPI: One Schema to Validate Them All
πŸ“°
0

InversifyJS + OpenAPI: One Schema to Validate Them All

DEV CommunityΒ·notaphploverΒ·about 1 month ago
#mdHgIgBQ
#openapi#how#typescript#node#fullscreen#validation
Reading 0:00
15s threshold

Here's a situation you've probably been in before: # Your OpenAPI spec says this: requestBody : content : application/json : schema : type : object required : [ name , email ] properties : name : { type : string , minLength : 1 } email : { type : string , format : email } Enter fullscreen mode Exit fullscreen mode // Your validation layer says this: const schema = z . object ({ name : z . string (). min ( 1 ), email : z . string (). email (), }); Enter fullscreen mode Exit fullscreen mode Two definitions of the same shape. They start identical. Then someone updates the OpenAPI spec but forgets the Zod schema. Or vice versa. A week later, your Swagger UI shows one contract and your server enforces a completely different one. There's a better way. Your OpenAPI Spec Is Already Your Schema That's the core idea behind @inversifyjs/open-api-validation . If you're using InversifyJS and already decorating your controllers with @OasRequestBody , you've already written your validation schema β€” you just didn't know it.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More