<p>When building REST API or web applications using Go, an essential part is to validate the incoming request data.
I have worked in some small and medium project in Golang, most of them are micro-services, providing restful API. I have used a different way to validate incoming data. Let's start with particular one that I use frequently.
For validating application/json or text/plain request, first I build a struct type of that particular request and add a validate method on the struct. Let's see an example <a href="https://medium.com/@thedevsaddam/an-easy-way-to-validate-go-request-c15182fd11b1">Read full post here</a></p>
<hr/>**评论:**<br/><br/>arp242: <pre><p>You keep posting this literally every week:
<a href="https://www.reddit.com/r/golang/comments/711qdw/validate_input_in_golang_easily/">one</a>,
<a href="https://www.reddit.com/r/golang/comments/71wm45/validate_golang_request_data_with_simple_rules/">two</a>,
<a href="https://www.reddit.com/r/golang/comments/75yt2x/golang_request_validation_package_update/">three</a>,
and I'm almost certain there was a fourth (now deleted) post as well.</p>
<p>I'm sure your library is great and all, but don't think we need to know about it every week...</p></pre>thedevsaddam: <pre><p>Does it feel allergic to you? And THIS is a post I am sharing not just the link. Don't tell people to stop contributing open source, to stop contributong on that project. You don't contribute that doesn't mean others can't do it. and don't take it personally. </p></pre>ZetaHunter: <pre><p>I just use <a href="https://github.com/go-chi/render" rel="nofollow">https://github.com/go-chi/render</a> which provides more or less generic Binder interface in which you can do your validation code.</p></pre>manifold360: <pre><p>I use <a href="https://github.com/go-openapi" rel="nofollow">OpenAPI</a> (aka swagger) to define the schema, publish the schema, and validate the request with the schema. It is the best solution I have seen in a long time.</p>
<p>I also validate on the JS client-side against the published api specs.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传