Checks
Checks turn a report from a readout into a verdict: does this recipe or week actually hit the targets? Import the built-in ck macro library once, then assert. Each macro records a pass or a fail and prints nothing itself; you print the results with all_checks() or failed_checks().
The check macros and the functions that track results make no nutrition lookups, so they work for everyone. The numbers you usually check — macros(), aggregate_nutrition(), category_servings() — come from the nutrition service, which needs Cook Basic or Cook Pro.
{% import "ck" as ck %}
{% set t = macros(ingredients) %}
{{ ck.range(t.kcal, {"min": 1800, "max": 2200}, "Daily kcal") }}
{{ ck.min(t.protein_g, {"min": 75}, "Protein (g)") }}
{{ ck.max(t.sat_fat_g, {"max": 20}, "Saturated fat (g)") }}
{% for c in all_checks() -%}
- {{ "PASS" if c.ok else "FAIL" }}: {{ c.label }}
{% endfor %}
The recorded label is your label plus the bound that was checked, for example Protein (g) >= 75.
| Macro | Asserts |
|---|---|
ck.min(value, target, label) | value ≥ target.min |
ck.max(value, target, label) | value ≤ target.max |
ck.range(value, target, label) | target.min ≤ value ≤ target.max (either bound may be left out) |
ck.within(value, target, label) | value within target.tol_pct% of target.max; a target without tol_pct is recorded as a fail |
ck.between(value, lo, hi, label) | lo ≤ value ≤ hi |
ck.absent(name) | the nutrition service did not match name against a client profile's exclusions. Cook Editor passes no client profile, so in the editor this always passes — see Exclusions. |
ck.min, ck.max and ck.range record nothing when the target doesn't have the bound they need, so a missing target is skipped, not failed.
Defining targets
In the editor, define targets at the top of the template with set — they're just data, and keeping them in the template means the whole report stays one self-contained file:
{% set client = {
"targets": {
"energy_kcal": {"min": 1800, "max": 2200},
"protein_g": {"min": 75},
"sat_fat_g": {"max": 20, "tol_pct": 10},
"fiber_g": {"min": 25}
}
} %}
{% import "ck" as ck %}
{% set t = macros(ingredients) %}
{{ ck.range(t.kcal, client.targets.energy_kcal, "Daily kcal") }}
{{ ck.min(t.protein_g, client.targets.protein_g, "Protein (g)") }}
{{ ck.within(t.sat_fat_g, client.targets.sat_fat_g, "Saturated fat (g)") }}
{{ ck.min(t.fiber_g, client.targets.fiber_g, "Fiber (g)") }}
Don't want to write a template? The Core Vitals plugin scores a plan against daily nutrient targets for you — see Check a meal plan against daily nutrient targets.
Exclusions
ck.absent(name) does not look at your recipe. It asks whether the nutrition service matched name against the exclusion list of a client profile, a YAML file handed to the report engine by the program that runs it. Cook Editor doesn't pass a client profile, and a client value you define with set inside the template is not one. So in the editor ck.absent() records a pass every time, even when the ingredient is in the recipe. Don't use it there.
To check for an ingredient by name in the editor, compare the names yourself and record the result:
{% set names = get_ingredient_list(ingredients) | map(attribute="name") | map("lower") | list %}
{{ record_check("No peanut butter", "peanut butter" not in names) }}
get_ingredient_list() follows recipe references, so ingredients of the sub-recipes a recipe uses, and of the recipes on a .menu, are included. Plain ingredients would list only what is written in the open file itself, and a recipe that brings peanut butter in through a sub-recipe would pass.
This matches the whole name as written in the recipes, nothing more: "peanut" does not match peanut butter, and a different spelling or another language doesn't match either. Treat it as a reminder, not as a safety check. For allergen classes (milk, gluten, …) use the allergen summary instead.
Client profiles (targets and exclusions kept in a separate YAML file and reused across reports) are read by the cook-nutrition-demo program described in the developer guides. Released CookCLI (0.41.1) renders plain templates with cook report, but it has no nutrition functions, no ck macros, no client profiles and no plan value.
The summary line
**Summary:** {{ all_checks() | selectattr("ok") | list | length }} of {{ all_checks() | length }} checks passed.
{% for c in failed_checks() -%}
- Not met: {{ c.label }}
{% endfor %}
all_checks() returns every check recorded so far as {label, ok}; failed_checks() returns only the failures. Both see only the checks that ran above them in the template, so put the summary after the checks.