Is there a time, when you often get tired of writing the same if logic over and over again just to validate a simple string and you’re not a fan of using OO just for something dead simple. If so this little tutorial maybe for you, or not.

It’s going to be quite similar to Symfony’s raw value validation, the big different is that Symfony’s is OO, but my example is going to be a bit more functional with a little bit of OO, I’m using closures as I find that the more cleaner solution to the problem. To be fair Symfony’s is written in PHP and mine is written in Golang.

Okay, now I’m going to start off the tutorial, first off you need to create a error collector and a validation error type, this is very easy to do.

type ErrorCollector []error

func (e ErrorCollector) Error() string {
    str := []string{}
    for _, err := range e {
        str = append(str, fmt.Sprint(err))
    }
    return strings.Join(str, "\n")
}

func (e ErrorCollector) FilterErrors() error {
    errorCollection := ErrorCollector{}
    for _, err := range e {
        if nil != err {
            errorCollection = append(errorCollection, err)
        }
    }

    if len(errorCollection) == 0 {
        return nil
    }

    return errorCollection
}

type ValidationError string

func (e ValidationError) Error() string {
    return fmt.Sprint(string(e))
}

Both of those data type, implement the builtin error interface, so we can use those data type under the type hint of error easily.

Hello, after three years, I was able to to get this site up and running and why three year, long story short I was very busy with getting my future sorted out.

But hey it’s in a better state now than it’s was a few years ago.

What am I going to do with this blog?

Probably write a lot of technical rambling. I still got a few things to implement and they are

  • A third party comment system (probably with disqus, a free one will do me.)
  • Implement highlight.js (it’s one of those nice to have.)
  • Ability to upload images using mongodb gridfs, as I’m using docker container I can’t just use the normal file system method. I really have to think about this one. For now I stick with a third party image service.
  • A portfolio system.

I will eventually implement them when I get the time and space to do it.

Deviant Art | Facebook | Linked In