Validate golang request data with simple rules. Highly inspired by Laravel's request validation.

xuanbao · · 339 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>If you are looking for an easy request validation package, <a href="https://github.com/govalidator/validator">try this</a> </p> <hr/>**评论:**<br/><br/>tdewolff: <pre><p>I was just looking around libraries that do validation. Your library has a lot of stuff I like, it uses struct tags just like JSON and SQL, so all my model data is in one struct, and it has good error response from which I can display field-specific errors in my forms.</p> <p>However, I really don&#39;t like the way you need to use <code>New</code> and then validate. Passing in the <code>http.Request</code> seems like a restriction if your want to use it for other inputs as well, why not make it a helper function that parses from a request but make the core functions accept a reader? Ideally I&#39;d like to create one instance of validator and use it for every request. Then per request I call <code>Validate</code> with a reader and a destination struct.</p> <p>Also why is <code>messages</code> not a <code>map[string]string</code> instead of a <code>[]string</code>? And I feel like <code>required</code> should be the default, but okay.</p></pre>thedevsaddam: <pre><p>Thank you for such a great suggestion. I will keep in mind for next update. </p></pre>arp242: <pre><p>Sorry, but why do you keep posting this? You posted this <a href="https://www.reddit.com/r/golang/comments/711qdw/validate_input_in_golang_easily/" rel="nofollow">five days ago too</a>, and I&#39;m fairly positive I&#39;ve seen it a few days before that as well.</p></pre>The_Sly_Marbo: <pre><p>Please don&#39;t do verification using regex. It&#39;s hard to read and often subtly wrong.</p></pre>thedevsaddam: <pre><blockquote> <p>Thank you for the suggestion, I&#39;ll try to remove regex in future as possible.</p> </blockquote></pre>DenzelM: <pre><p>Haha, there&#39;s no need to do that. Regular expressions are ubiquitous for a reason. The Go team included support for regexs in the standard library for a reason. Because they&#39;re really useful! And I think your use of them is proper.</p> <p>Regexs for validation should be write-once read-many (or read-little really) meaning that you&#39;ll <em>rarely</em> if ever update a regex that verifies a CSS color, and other developers should <em>rarely</em> if ever have to read such a regex.</p> <p>Now, if one is worried about regexs being subtly wrong, then you can provide a test harness -- maybe include a fuzzer -- that demonstrates their efficacy. If one finds a bug, then a regression test is provided and the regex is fixed. Simple enough.</p> <p>Thanks for sharing your library with us!</p></pre>BurpsWangy: <pre><p>Well, RegEx is extremely powerful for validation; however, I agree that it&#39;s actually VERY often implemented wrong. Properly implemented (and thoroughly tested), it&#39;s actually perfect for such a use.</p> <p>Most of the time when I&#39;m searching for regex (for phone number validation, for example)... almost all the ones I find online are incorrect in one way or another. I usually have to make major adjustments, even to the most commonly accepted ones, to make it work properly against all the tests I throw against it.</p> <p>Like anything else... test... test... test... and then test again.</p></pre>arp242: <pre><p>Regular expressions are fine. Sure, you can do it wrong. But you can also do it wrong when you&#39;re <em>not</em> using regular expressions.</p> <p>It&#39;s all about the right tool for the job...</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

339 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传