carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.68k stars 137 forks source link

Annotate a Data Value with validation #604

Closed pivotaljohn closed 2 years ago

pivotaljohn commented 2 years ago

Given a Data Values document (and only a Data Values document), annotated with a validation rule:

#! values.yml

@data/values
---
#@assert/validate ("a non-empty string", lambda v: assert.fail("length = {}", len(v)) if len(v) > 0 else None)
username: ""

Scenario: Invalid Data Value

Given the final Data Value's value does not satisfies the rule: When ytt runs Then reports a Data Values violation error message:

$ ytt -f values.yml --data-values-inspect
ytt: Error: One or more data values were invalid:
- "adminPort" requires a valid value: a non-empty string; length = 0 (validated by values.yml:6).

Scenario: Valid Data Value

Given the final Data Value's value satisfies the rule: Then no error message is displayed.

$ ytt -f values.yml -v username=alice --data-values-inspect
username: alice

Note: validations are checked after all overlays have been applied.


Notes:

gcheadle-vmware commented 2 years ago

Should the #@assert/validate annotation work on data values contained in a data values file?

#@ load("@ytt:assert", "assert")
@data/values
---
#@assert/validate ("a non-empty string", lambda v: assert.fail("length = {}", len(v)) if len(v) > 0 else None)
username: "andrew"
pivotaljohn commented 2 years ago

Should the #@assert/validate annotation work on data values contained in a data values file?

Insightful question. And I think you intuition is right: that an @assert/validate should mean the same thing everywhere (just like an @overlay/match does).

This points to a need to be able to declare the need for an @assert/validate (specifically in a document containing schema).

Folding this into the overall proposal.

🙌🏻

pivotaljohn commented 2 years ago

Split this story into two:

gcheadle-vmware commented 2 years ago

Closing since follow up PR was merged.