diff --git a/SPEC.md b/SPEC.md index 7abcca4..7d5bc5a 100644 --- a/SPEC.md +++ b/SPEC.md @@ -55,7 +55,7 @@ The endpoint MUST be same-origin with the card unless a client policy explicitly The well-known suffix is intentionally `latch.json`; it is more precise than squatting on a generic metadata name. Deployments seeking formal interoperability should follow the registration process in RFC 8615. A path-local `latch.json` and an HTML `` MAY be used on shared hosting where the origin root is unavailable. -`input` uses JSON Schema. The dependency-free reference runtime validates the documented subset in `validateInput`; providers remain responsible for enforcing their complete schema. Unknown protocol fields MUST be ignored unless they are required by a negotiated extension. +`input` uses JSON Schema. When its schema declares a type, it MUST include object; an omitted type remains allowed. The dependency-free reference runtime validates the documented subset in `validateInput`; providers remain responsible for enforcing their complete schema. Unknown protocol fields MUST be ignored unless they are required by a negotiated extension. ## 3. Envelope diff --git a/latch.mjs b/latch.mjs index 4b4cb04..fe98bd5 100644 --- a/latch.mjs +++ b/latch.mjs @@ -151,9 +151,11 @@ export function validateCard(card) { errors.push(`${at} read capabilities must have no effects and no approval`); } if (capability.mode === "write" && capability.effects === "none") errors.push(`${at} write capability cannot have no effects`); - validateInput({}, capability.input || {}, `${at}.input`).forEach((error) => { - if (error.includes("must be object")) errors.push(`${at}.input must be an object schema`); - }); + const declaredType = capability.input?.type; + if (declaredType !== undefined && !(Array.isArray(declaredType) + ? declaredType.includes("object") : declaredType === "object")) { + errors.push(at + " (" + capability.id + ").input must be an object schema; declared type: " + JSON.stringify(declaredType)); + } }); } if (errors.length) fail("invalid_card", "The LATCH card is invalid.", 422, { details: errors }); diff --git a/test/latch.test.mjs b/test/latch.test.mjs index 3dfd8d8..1ae10a5 100644 --- a/test/latch.test.mjs +++ b/test/latch.test.mjs @@ -17,7 +17,8 @@ import { listen, runDemoTrace, sha256, - validateInput + validateInput, + validateCard } from "../latch.mjs"; const ROOT = fileURLToPath(new URL("../", import.meta.url)); @@ -530,3 +531,25 @@ test("fifty concurrent identical commits create exactly one task and invalid inp test.after(async () => { await rm(TEMP, { recursive: true, force: true }); }); + +for (const type of ["string", "array", ["string", "number"]]) { + test("card refuses non-object input schema " + JSON.stringify(type), () => { + const card = createDemoProvider().card; + card.capabilities[0].input = { type }; + assert.throws(() => validateCard(card), (error) => { + assert.equal(error.code, "invalid_card"); + assert.equal(error.status, 422); + assert.ok(error.details.some((detail) => + detail.includes("materials.list") && detail.includes(JSON.stringify(type)))); + return true; + }); + }); +} + +test("card keeps object, object unions and unspecified input types", () => { + for (const input of [{ type: "object" }, { type: ["object", "null"] }, {}]) { + const card = createDemoProvider().card; + card.capabilities[0].input = input; + assert.equal(validateCard(card), true); + } +});