Esta página fue traducida por una máquina y aún no ha sido revisada por una persona. El inglés es la referencia; las correcciones son bienvenidas en GitHub. GitHub

Cookwala / SDK y escenarios / 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.

Audiencia: recipe publisher · Objetivo: Hacer que las personas estén más sanas · Nivel: beginner

Leer primero: /docs/CORE/ /docs/QUICKSTART/

Pasos

  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.

Línea de comandos

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"}}'

Salida grabada

Estado: 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

Ejecutar: 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 Siguiente: 009