[ANN] Package validator

The thing about working in a startup under stealth mode is you can’t often talk about what you’re doing. Thankfully, from time to time, an opportunity appears that lets us at least share something tangential.

A large part of our project at project7.io involves receiving data from a client (generally in JSON) for processing. This involves unmarshaling the JSON into a struct, something that Go does very well. But then comes the boring part: checking that the information sent from the client is correct and complete before doing anything with it.

The boring life of validating stuff

  <td class='code'>
    <pre><code class='go'>&lt;span class='line'>&lt;span class="k">if&lt;/span> &lt;span class="nx">t&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Username&lt;/span> &lt;span class="o">!=&lt;/span> &lt;span class="s">""&lt;/span> &lt;span class="o">&&&lt;/span> &lt;span class="nx">t&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Age&lt;/span> &lt;span class="p">&gt;&lt;/span> &lt;span class="mi">18&lt;/span> &lt;span class="o">&&&lt;/span> &lt;span class="nx">t&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Foo&lt;/span> &lt;span class="o">!=&lt;/span> &lt;span class="kc">nil&lt;/span> &lt;span class="o">&&&lt;/span> &lt;span class="nb">len&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">t&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Bar&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Baz&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">&gt;&lt;/span> &lt;span class="mi">8&lt;/span> &lt;span class="o">&&&lt;/span> &lt;span class="o">...&lt;/span>

</span>

1

We had to do this often and for a large number of different structs and sometimes a struct gained a new field and we had to go back and see that it was being properly validated everywhere. It was so boring that we ended up writing something to make it easier for us. We’ve been using this for a while and now we decided to open source it in the hopes that it might be useful for others.

Package validator implements validation of variables. Initially we had implemented JSONschema but we don’t always deal with JSON, we also get data as XML and sometimes as form-encoded. So we changed our approach and went right to the struct definitions.

A struct definition with some validation rules attached

  <td class='code'>
    <pre><code class='go'>&lt;span class='line'>&lt;span class="kd">type&lt;/span> &lt;span class="nx">T&lt;/span> &lt;span class="kd">struct&lt;/span> &lt;span class="p">{&lt;/span>

</span><span class=‘line’> <span class=“nx”>A</span> <span class=“kt”>int</span> <span class=“s”>validate:"nonzero"</span> </span><span class=‘line’> <span class=“nx”>B</span> <span class=“kt”>string</span> <span class=“s”>validate:"nonzero,max=10"</span> </span><span class=‘line’> <span class=“nx”>C</span> <span class=“kd”>struct</span> <span class=“p”>{</span> </span><span class=‘line’> <span class=“nx”>Ca</span> <span class=“kt”>int</span> <span class=“s”>validate:"min=5,max=10"</span> </span><span class=‘line’> <span class=“nx”>Cb</span> <span class=“kt”>string</span> <span class=“s”>validate:"min=8, regexp:^[a-zA-Z]+"</span> </span><span class=‘line’> <span class=“p”>}</span> </span><span class=‘line’><span class=“p”>}</span> </span>

1
2
3
4
5
6
7
8

This allowed us to attach validation rules right to the data structure definitions. Then instead of large, boring list of if statements, we were able to validate in single function call.

Validating an instance of a struct

  <td class='code'>
    <pre><code class='go'>&lt;span class='line'>&lt;span class="k">if&lt;/span> &lt;span class="nx">valid&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">_&lt;/span> &lt;span class="o">:=&lt;/span> &lt;span class="nx">validator&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Validate&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">t&lt;/span>&lt;span class="p">);&lt;/span> &lt;span class="p">!&lt;/span>&lt;span class="nx">valid&lt;/span> &lt;span class="p">{&lt;/span>

</span><span class=‘line’> <span class=“c1”>// not valid, so return an http.StatusBadRequest of something</span> </span><span class=‘line’><span class=“p”>}</span> </span>

1
2
3

Multiple rules for different situations

We often use the same struct to deal with different data coming from the client. Sometimes we only care about one or two of the fields in one scenario, so we also supported multiple rules like, say

A struct with two different sets of rules

  <td class='code'>
    <pre><code class='go'>&lt;span class='line'>&lt;span class="kd">type&lt;/span> &lt;span class="nx">T&lt;/span> &lt;span class="kd">struct&lt;/span> &lt;span class="p">{&lt;/span>

</span><span class=‘line’> <span class=“nx”>A</span> <span class=“kt”>int</span> <span class=“s”>foo:"nonzero" bar:"min=5,max=10"</span> </span><span class=‘line’> <span class=“nx”>B</span> <span class=“kt”>string</span> <span class=“s”>bar:"nonzero"</span> </span><span class=‘line’><span class=“p”>}</span> </span>

1
2
3
4

In scenario foo, we need A to be non-zero, but we don’t care for what is in B. In the bar scenario, however, we need B to be non-zero and the value of A to be between 5 and 10, inclusive. To validate, we then make validator use a different tag name for each case

WithTag() FTW

  <td class='code'>
    <pre><code class='go'>&lt;span class='line'>&lt;span class="nx">t&lt;/span> &lt;span class="o">:=&lt;/span> &lt;span class="nx">T&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="nx">A&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="p">}&lt;/span>

</span><span class=‘line’><span class=“nx”>validator</span><span class=“p”>.</span><span class=“nx”>WithTag</span><span class=“p”>(</span><span class=“s”>“foo”</span><span class=“p”>).</span><span class=“nx”>Validate</span><span class=“p”>(</span><span class=“nx”>t</span><span class=“p”>)</span> <span class=“c1”>// valid</span> </span><span class=‘line’><span class=“nx”>validator</span><span class=“p”>.</span><span class=“nx”>WithTag</span><span class=“p”>(</span><span class=“s”>“bar”</span><span class=“p”>).</span><span class=“nx”>Validate</span><span class=“p”>(</span><span class=“nx”>t</span><span class=“p”>)</span> <span class=“c1”>// invalid</span> </span>

1
2
3

We can also change the tag by using SetTag with will then make the tag name persistent until changed again by another call to SetTag.

Please refer to http://godoc.org/gopkg.in/validator.v1 for a lot more documentation, use cases, and how to access the individual validation errors found for each field.

Roberto Selbach
Roberto Selbach
Software Engineer

Ever-learning gopher at HashiCorp.