DEV Community

Sleeyax
Sleeyax

Posted on

Dynamic string validation using go's text/template package

Imagine you could validate the following string:

id: "d416e1b0-97b2-4a49-8ad5-2e6b2b46eae0"
static-string: "abc"
invalid-string: def
random-number: 150
Enter fullscreen mode Exit fullscreen mode

Using go template syntax like this:

id: "{{isUUID}}"
static-string: "abc"
invalid-string: def
random-number: {{inRange 100 200}}
Enter fullscreen mode Exit fullscreen mode

Well, that would be cool wouldn't it? Unfortunately this isn't supported by go's text/template package.

I've built a library which uses a subset of the template syntax to cover this specific use-case: github.com/sleeyax/templatex-go. Check it out for a more detailed example!


Hi 👋 thanks for reading! If you enjoyed reading my content, consider following me on Twitter to stay in the loop ❤️.

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up