From 4a895d6b639811bf6d38ae65845cc29dc431c101 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Fri, 29 Jan 2021 15:27:23 +0500 Subject: [PATCH] Add method to validate if values has required keys --- .../common/templates/lib/schema/_utils.tpl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 library/common/templates/lib/schema/_utils.tpl diff --git a/library/common/templates/lib/schema/_utils.tpl b/library/common/templates/lib/schema/_utils.tpl new file mode 100644 index 0000000000..b3a4cd0549 --- /dev/null +++ b/library/common/templates/lib/schema/_utils.tpl @@ -0,0 +1,19 @@ +{{/* +Checks if a list of keys are present in a dictionary +*/}} +{{- define "common.validateKeys" -}} +{{- $values := . -}} +{{- if and (hasKey $values "values") (hasKey $values "checkKeys") -}} +{{- $missingKeys := list -}} +{{- range $values.checkKeys -}} +{{- if eq (hasKey $values.values . ) false -}} +{{- $missingKeys = mustAppend $missingKeys . -}} +{{- end -}} +{{- end -}} +{{- if $missingKeys -}} +{{- fail (printf "Missing %s from dictionary" ($missingKeys | join ", ")) -}} +{{- end -}} +{{- else -}} +{{- fail "A dictionary and list of keys to check must be provided" -}} +{{- end -}} +{{- end -}}