Cette page a été traduite par une machine et n'a pas encore été révisée par une personne. L'anglais est la référence ; les corrections sont les bienvenues sur GitHub. GitHub
Cookwala / SDK et scénarios / 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.
Lire d'abord: /docs/HUMANITARIAN-PROFILE/ /humanitarian/
Étapes
The example offer against the humanitarian schema.
validateThe example claim.
validateThe manifest that lists the example documents.
validateThe 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
Résultat
You can validate documents from any language through the hub. Next: scenario 075 validates an impact summary.
Ligne de commande
cookwala validate
cookwala humanitarian examples/humanitarian/offer.json examples/humanitarian/claim.jsonSortie enregistrée
Statut: 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
Exécuter: 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")