Cookwala / SDK and scenarios / 073

Validate documents against the Humanitarian Profile schema

The validate operation takes a schema name and a document. Validate the example offer, claim and manifest as humanitarian documents, then validate the offer as a recipe to see what errors look like.

Audience: developer · Goal: Help end hunger · Level: beginner

Read first: /docs/HUMANITARIAN-PROFILE/ /humanitarian/

Steps

  1. The example offer against the humanitarian schema. validate

  2. The example claim. validate

  3. The manifest that lists the example documents. validate

  4. The offer validated as a recipe: ok false and a list of errors, one per missing required recipe field plus one for the unknown properties. validate

Outcome

You can validate documents from any language through the hub. Next: scenario 075 validates an impact summary.

Command line

cookwala validate
cookwala humanitarian examples/humanitarian/offer.json examples/humanitarian/claim.json

Recorded output

Status: ok

[1] validate: {"ok": true, "errors": []}
[2] validate: {"ok": true, "errors": []}
[3] validate: {"ok": true, "errors": []}
[4] 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", ": 'donor', 'items', 'kind', 'needs', 'note', 'profile', 'readings', 'site', 'state', 'version', 'window' do not match any of the regexes: '^x-'"]}

Code

Run: python scenarios/out/073-validate-humanitarian-documents/python.py

# Scenario 073: Validate documents against the Humanitarian Profile schema
# The validate operation takes a schema name and a document. Validate the example offer, claim and manifest as humanitarian documents, then validate the offer as a recipe to see what errors look like.
# 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: The example offer against the humanitarian schema.
offer = c.validate("humanitarian", load("examples/humanitarian/offer.json"))
assert pick(offer, "ok") == True, pick(offer, "ok")
print("ok", "ok", "=", True)
assert pick(offer, "errors.length") == 0, pick(offer, "errors.length")
print("ok", "errors.length", "=", 0)

# Step 2: The example claim.
claim = c.validate("humanitarian", load("examples/humanitarian/claim.json"))
assert pick(claim, "ok") == True, pick(claim, "ok")
print("ok", "ok", "=", True)

# Step 3: The manifest that lists the example documents.
manifest = c.validate("humanitarian", load("examples/humanitarian/manifest.json"))
assert pick(manifest, "ok") == True, pick(manifest, "ok")
print("ok", "ok", "=", True)

# Step 4: The offer validated as a recipe: ok false and a list of errors, one per missing required recipe field plus one for the unknown properties.
wrong = c.validate("recipe", load("examples/humanitarian/offer.json"))
assert pick(wrong, "ok") == False, pick(wrong, "ok")
print("ok", "ok", "=", False)
assert pick(wrong, "errors.length") == 12, pick(wrong, "errors.length")
print("ok", "errors.length", "=", 12)

print("scenario complete")

scenarios/073-validate-humanitarian-documents.json

Previous: 072 Next: 074