Cookwala / SDK and scenarios / 087
Validate a household context and the roles file, and hash the context
Check the example HouseholdContext and the RecipientRoles file with the validate tool, hash the context so a local log can refer to it, and see the same document fail when checked as a recipe.
Read first: /docs/HOUSEHOLD-CONTEXT/ /trust/ /docs/CORE/
Steps
Validate the example household context against the household schema. The reference hub's validate checks the document against the schema file's root; this schema keeps its kinds under $defs, so today this call reports ok for any document, and `cookwala validate` (the CLI line) is the real check.
validateHash the context: sha256 over RFC 8785 canonical JSON, the same hash in every language.
hashThe same document checked as a recipe fails with twelve errors: kinds are not interchangeable.
validateThe recipient roles file is a household-profile document too.
validate
Outcome
You can check and hash household documents from any language. The hash never leaves the home either; it is for the local incident memory. Next: 088 compares three roles side by side.
Command line
cookwala validate
cookwala hash examples/household/context.jsonRecorded output
Status: ok
[1] validate: {"ok": true, "errors": []}
[2] hash: {"hash": "sha256:8fc1fdf5246943f0fa191a499af1641688278906dd41453dadaa07089aaba34b"}
[3] validate: {"ok": false, "errors": [": 'cookwala' is a required property", ": 'revision' is a required property", ": 'verification' is a required property", ": 'dish' is a required property", ": 'yield' is a required property", ": 'ingredients' is a required property", ": 'equipment' is a required property", ": 'process' is a required property", ": 'safety' is a required property", ": 'text' is a required property", ": 'license' is a required property", ": 'consents', 'erasures', 'facets', 'holder', 'incidents', 'kind', 'profile', 'retention' do not match any of the regexes: '^x-'"]}
[4] validate: {"ok": true, "errors": []}Code
Run: python scenarios/out/087-validate-household-context-and-roles/python.py
# Scenario 087: Validate a household context and the roles file, and hash the context
# Check the example HouseholdContext and the RecipientRoles file with the validate tool, hash the context so a local log can refer to it, and see the same document fail when checked as a recipe.
# Run a hub first: python hub/cookwala_hub.py --recipes examples
import json, os
from cookwala.client import CookwalaClient, CookwalaProblem
def load(path):
with open(path, encoding="utf-8") as f:
return json.load(f)
def pick(obj, path):
for part in path.replace("]", "").replace("[", ".").split("."):
obj = obj[int(part)] if part.isdigit() else (len(obj) if part == "length" else obj.get(part))
return obj
c = CookwalaClient(os.environ.get("COOKWALA_HUB", "http://localhost:7878"))
# Step 1: Validate the example household context against the household schema. The reference hub's validate checks the document against the schema file's root; this schema keeps its kinds under $defs, so today this call reports ok for any document, and `cookwala validate` (the CLI line) is the real check.
ctx = c.validate("household", load("examples/household/context.json"))
assert pick(ctx, "ok") == True, pick(ctx, "ok")
print("ok", "ok", "=", True)
assert pick(ctx, "errors.length") == 0, pick(ctx, "errors.length")
print("ok", "errors.length", "=", 0)
# Step 2: Hash the context: sha256 over RFC 8785 canonical JSON, the same hash in every language.
h = c.hash(load("examples/household/context.json"))
assert pick(h, "hash") == "sha256:8fc1fdf5246943f0fa191a499af1641688278906dd41453dadaa07089aaba34b", pick(h, "hash")
print("ok", "hash", "=", "sha256:8fc1fdf5246943f0fa191a499af1641688278906dd41453dadaa07089aaba34b")
# Step 3: The same document checked as a recipe fails with twelve errors: kinds are not interchangeable.
asRecipe = c.validate("recipe", load("examples/household/context.json"))
assert pick(asRecipe, "ok") == False, pick(asRecipe, "ok")
print("ok", "ok", "=", False)
assert pick(asRecipe, "errors.length") == 12, pick(asRecipe, "errors.length")
print("ok", "errors.length", "=", 12)
# Step 4: The recipient roles file is a household-profile document too.
roles = c.validate("household", load("profiles/household/recipient-roles.json"))
assert pick(roles, "ok") == True, pick(roles, "ok")
print("ok", "ok", "=", True)
print("scenario complete")