Esta página foi traduzida por máquina e ainda não foi revisada por uma pessoa. O inglês é a referência; correções são bem-vindas no GitHub. GitHub

Cookwala / SDK e cenários / 008

Validate documents against the schemas and read the errors for a broken one

Every Cookwala document kind has a JSON Schema. The validate tool returns ok or a list of errors with the path of each one, so a publisher can fix a recipe before it reaches any device.

Público: recipe publisher · Objetivo: Torne as pessoas mais saudáveis · Nível: beginner

Leia primeiro: /docs/CORE/ /docs/QUICKSTART/

Passos

  1. The shakshuka example passes the recipe schema. validate

  2. A stub with an id that has spaces and a revision given as a word: eleven errors, each with its path. validate

  3. A capability document is checked against its own schema. validate

  4. The Core schema covers requests, statuses, logs, recalls and incident reports. validate

Resultado

You can validate any document kind over HTTP and read machine-checkable errors. The CLI form validates the whole repository, including recipe targets against operation envelopes. Next: scenario 025 shows a target outside its envelope.

Linha de comando

cookwala validate   # every schema, example, vocabulary and API in the repository
curl -s -X POST http://localhost:7878/v1/tools/validate -H 'Content-Type: application/json' -d '{"kind": "recipe", "doc": {"cookwala": "0.2.0", "id": "Broken Recipe", "revision": "one"}}'

Saída gravada

Status: ok

[1] validate: {"ok": true, "errors": []}
[2] validate: {"ok": false, "errors": [": '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", "id: 'Broken Recipe' does not match '^[a-z0-9][a-z0-9-]{1,63}$'", "revision: 'one' is not of type 'integer'"]}
[3] validate: {"ok": true, "errors": []}
[4] validate: {"ok": true, "errors": []}

Código

Executar: python scenarios/out/008-validate-a-document-and-see-errors/python.py

# Scenario 008: Validate documents against the schemas and read the errors for a broken one
# Every Cookwala document kind has a JSON Schema. The validate tool returns ok or a list of errors with the path of each one, so a publisher can fix a recipe before it reaches any device.
# 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 shakshuka example passes the recipe schema.
good = c.validate("recipe", load("examples/shakshuka.cookwala.json"))
assert pick(good, "ok") == True, pick(good, "ok")
print("ok", "ok", "=", True)
assert pick(good, "errors.length") == 0, pick(good, "errors.length")
print("ok", "errors.length", "=", 0)

# Step 2: A stub with an id that has spaces and a revision given as a word: eleven errors, each with its path.
bad = c.validate("recipe", {"cookwala": "0.2.0", "id": "Broken Recipe", "revision": "one"})
assert pick(bad, "ok") == False, pick(bad, "ok")
print("ok", "ok", "=", False)
assert pick(bad, "errors.length") == 11, pick(bad, "errors.length")
print("ok", "errors.length", "=", 11)
assert pick(bad, "errors[9]") == "id: 'Broken Recipe' does not match '^[a-z0-9][a-z0-9-]{1,63}$'", pick(bad, "errors[9]")
print("ok", "errors[9]", "=", "id: 'Broken Recipe' does not match '^[a-z0-9][a-z0-9-]{1,63}$'")
assert pick(bad, "errors[10]") == "revision: 'one' is not of type 'integer'", pick(bad, "errors[10]")
print("ok", "errors[10]", "=", "revision: 'one' is not of type 'integer'")

# Step 3: A capability document is checked against its own schema.
caps = c.validate("capabilities", load("examples/capabilities/demo-oven.json"))
assert pick(caps, "ok") == True, pick(caps, "ok")
print("ok", "ok", "=", True)

# Step 4: The Core schema covers requests, statuses, logs, recalls and incident reports.
incident = c.validate("core", load("examples/core/incident.json"))
assert pick(incident, "ok") == True, pick(incident, "ok")
print("ok", "ok", "=", True)

print("scenario complete")

scenarios/008-validate-a-document-and-see-errors.json

Anterior: 007 Próximo: 009