Validation of the generated template #1681
-
Hello! Thanks for making this tool. I'm using it atm to template out some YAML files, and want to do some validation to make sure that the values are within a certain range, and are of a certain type. Which is the recommended way? I want to do all the validation inside the template, and don't want to have to invoke a downstream tool to parse the generated YAML file. I tried a mixture of the below, but could never quite get it working correctly. What am I doing wrong? Assuming an input JSON data source of {
"tenant": {
"name": "foobar",
"account": 123123,
}
} I want to verify that the tenant.name is a string, and that the account is an integer, and between 0-999999. I tried things like: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @jeremych1000, I've transferred this to a Discussion since this wasn't a bug report or feature request. Here's a sample template that will do what I think you're looking for: {{ $nameKind := test.Kind .tenant.name -}}
{{ $acctKind := test.Kind .tenant.account -}}
{{ eq "string" $nameKind | test.Assert (printf "name must be a string, but was a %s" $nameKind) -}}
{{ eq "int" $acctKind | test.Assert (printf "account must be an int, but was a %s" $acctKind) -}} Given an (invalid) input of: {
"tenant": {
"name": "foobar",
"account": true
}
} This will fail as follows: $ gomplate -c .=in.json -f t.tmpl
09:21:12 ERR error="renderTemplate: failed to render template t.tmpl: template: t.tmpl:4:28: executing \"t.tmpl\" at <test.Assert>: error calling Assert: assertion failed: account must be an int, but was a bool" The main issue here is that the arguments to Have a look at the Hope that helps! |
Beta Was this translation helpful? Give feedback.
Hi @jeremych1000, I've transferred this to a Discussion since this wasn't a bug report or feature request.
Here's a sample template that will do what I think you're looking for:
Given an (invalid) input of:
This will fail as follows: