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.
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
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.
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
1 2 3 4
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
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
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.