<p>Most existing Go validation packages rely on struct tags to specify validation rules associated with struct fields. While this approach is handy for specifying simple rules, it is error prone and can quickly become unmanageable for complicate rules that often need to be parameterized.</p>
<p>ozzo-validation takes a different approach. It lets you to specify validation rules in usual program constructs so that coding mistakes can be early detected by IDEs or compilers. Out-of-box it provides dozens of validators which can be easily customized and extended. It supports validating structs, arrays/slices, or scalar values. It also provides special support for database/sql data types.</p>
<p>Below is the repo link. Your comments are very welcome!</p>
<p><a href="https://github.com/go-ozzo/ozzo-validation">https://github.com/go-ozzo/ozzo-validation</a></p>
<hr/>**评论:**<br/><br/>mwholt: <pre><p>Nice. Validation logic does not belong in struct tags, so +1 on writing this package.</p></pre>schumacherfm: <pre><p>+1 but it still uses too much reflection. The API should be something like the logger of uber-go/zap, just type safe.</p></pre>proud_to_be_proud: <pre><p>I completely agree and blame developers who want to look "clever" and "show off" for this absolute travesty. Overcomplicating things and using the wrong tools for the job are never cool.</p></pre>LadyDascalie: <pre><p>This is great! I agree 100% with the premise that you should't be doing this in struct tags, this is much more sane, definitely going to give it a shot on a project I'm building currently!</p></pre>mustafaakin: <pre><p>Its really nice, while struct based ones were useful enough, they were easily becoming hard to main as you said.</p></pre>chrj: <pre><p>Very cool!</p></pre>daveddev: <pre><p>Dynamic field names and self-referential methods feel dirty to me.</p></pre>qiangxue: <pre><p>Thanks for your feedback! Totally agree with you about the dynamic field name issue which was already reported. nsf gave a clever solution which I'm going to implement.</p>
<p>Could you elaborate a bit more about self-referential method issue?</p></pre>daveddev: <pre><p>Instead of:</p>
<pre><code>return valid.Validate(&a).
Mandatory(&a.FirstName, valid.MaxLength(250)).
Mandatory(&a.LastName, valid.MaxLength(250)).
Mandatory(&a.Email, valid.Email).
Optional(&a.Age, valid.PositiveInteger).
Done()
</code></pre>
<p>Rules could be in a separate library and, using a named import ("vr" for validation rule), the code would be:</p>
<pre><code>return validation.Validate(
vr.Mandatory(&a.FirstName, vr.MaxLength(250),
vr.Mandatory(&a.LastName, vr.MaxLength(250),
vr.Mandatory(&a.Email, vr.Email),
vr.Optional(&a.Age, vr.PositiveInteger),
)
</code></pre>
<p>This would be a more functional approach. Also, I'm not certain that the first arg "&a" is necessary, so I left it out.
<a href="https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis" rel="nofollow">https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis</a></p>
<p>Edit to add: I had planned to implement this exact thing by forking someone's (not yet sure whose) validation rules and forming the API to my liking. So, if you make this happen, I'd use it immediately.</p></pre>FourSigma: <pre><p>Check out --- <a href="https://github.com/FourSigma/validate" rel="nofollow">https://github.com/FourSigma/validate</a>
Let me know what you think. Uses the functional optional pattern. Still in beta.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传