Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core/interactionOutput): support for no-type based root schemas #1309

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/core/src/interaction-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ export class InteractionOutput implements WoT.InteractionOutput {
throw new NotReadableError("No form defined");
}

if (this.schema.type == null) {
if (
this.schema.const == null &&
this.schema.enum == null &&
this.schema.oneOf == null &&
this.schema.type == null
) {
throw new NotReadableError("No schema type defined");
}

Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/InteractionOutputTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,40 @@ class InteractionOutputTests {
const result = out.value<number>();
return expect(result).to.eventually.be.rejected;
}

@test async "should return null"() {
const stream = Readable.from(Buffer.from("null", "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { type: "null" });
const result = await out.value<null>();
expect(result).to.be.eq(null);
}

@test async "should support oneOf"() {
const stream = Readable.from(Buffer.from('"hello"', "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { oneOf: [{ type: "string" }, { type: "null" }] });
const result = await out.value<string | null>();
expect(result).to.be.eq("hello");
}

@test async "should support const"() {
const stream = Readable.from(Buffer.from("42", "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { const: 42 });
const result = await out.value<42>();
expect(result).to.be.eq(42);
}

@test async "should support enum"() {
const stream = Readable.from(Buffer.from('"red"', "utf-8"));
const content = new Content("application/json", stream);

const out = new InteractionOutput(content, {}, { enum: ["red", "amber", "green"] });
const result = await out.value<"red" | "amber" | "green">();
expect(result).to.be.eq("red");
}
}
Loading