<p>I have to check for mandatory values based on some condition.<br />
My <em>values.yaml</em> is as below</p>
<pre class="lang-yaml prettyprint-override"><code>id: 3
test:
 id: 2
 test1:
  id: 1
</code></pre>
<p>In my template, I need to check if <code>id</code> is present at <code>.Values.test.test1.id</code> and assign that value. If not fall back to <code>.Values.test.id</code> and at last <code>.Values.id</code>. But <code>id</code> has to be mandatory and I need to use required function.</p>
<p>My template is as below</p>
<pre><code>{{- if .Values.test.test1.id }}
&lt;assign&gt; {{ .Values.test.test1.id }}
{{- else }}
{{- $id := .Values.test.id }}
{{- $id2 := .Values.id }}
&lt;assign&gt; &lt;need to check required with or of $id $id2&gt; &lt;/assign&gt;
{{- end }}
</code></pre>
<p>I know this can be solved with one <code>elseif</code> in between <code>if</code> and <code>else</code>. But I need to repeat the same logic for many ids.<br />
What is the best way to achieve this?</p>

<p>It seems like a perfect fit for the <a href="https://helm.sh/docs/chart_template_guide/function_list/#coalesce" rel="nofollow noreferrer"><code>coalesce</code> function</a>, which</p>
<blockquote>
<p>takes a list of values and returns the first non-empty one.</p>
</blockquote>
<p><sup><em>Source: <a href="https://helm.sh/docs/chart_template_guide/function_list/#coalesce" rel="nofollow noreferrer">https://helm.sh/docs/chart_template_guide/function_list/#coalesce</a></em></sup></p>
<p>You will also need to use the <a href="https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-default-function" rel="nofollow noreferrer"><code>default</code> function</a> to convert the cases when the <code>test</code> or <code>test.test1</code> dictionaries are not defined.</p>
<p>All this together, gives:</p>
<pre><code>{{- $test := .Values.test | default dict -}}
{{- $test1 := $test.test1 | default dict -}}
{{- $id := required &quot;Please provide an ID&quot; (
  coalesce $test1.id $test.id .Values.id
) -}}
id: {{ $id }}
</code></pre>
<p>And here are the test cases and results:</p>
<ul>
<li>Gives:
<pre class="lang-yaml prettyprint-override"><code>id: 1
</code></pre>
when <em>values.yaml</em> is
<pre class="lang-yaml prettyprint-override"><code>id: 3
test:
  id: 2
  test1:
    id: 1
</code></pre>
</li>
<li>Gives:
<pre class="lang-yaml prettyprint-override"><code>id: 2
</code></pre>
when <em>values.yaml</em> is
<pre class="lang-yaml prettyprint-override"><code>id: 3
test:
  id: 2
</code></pre>
</li>
<li>Gives:
<pre class="lang-yaml prettyprint-override"><code>id: 3
</code></pre>
when <em>values.yaml</em> is
<pre class="lang-yaml prettyprint-override"><code>id: 3
</code></pre>
</li>
<li>Gives:
<pre class="lang-none prettyprint-override"><code>Error: execution error at (demo/templates/test.yaml:3:11): 
  Please provide an ID
</code></pre>
when <em>values.yaml</em> is an empty file</li>
</ul>
