Skip to content

Commit 357126b

Browse files
refactor: do validation for synchronous action
remove ignoreValidation for generic InteractionOutput
1 parent 4ec7354 commit 357126b

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

packages/core/src/consumed-thing.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
564564

565565
const content = await client.readResource(form);
566566
try {
567-
return this.handleInteractionOutput(content, form, tp, false);
567+
return this.handleInteractionOutput(content, form, tp);
568568
} catch (e) {
569569
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
570570
throw new Error(`Error while processing property for ${tp.title}. ${error.message}`);
@@ -574,14 +574,13 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
574574
private handleInteractionOutput(
575575
content: Content,
576576
form: Form,
577-
outputDataSchema: WoT.DataSchema | undefined,
578-
ignoreValidation: boolean
577+
outputDataSchema: WoT.DataSchema | undefined
579578
): InteractionOutput {
580579
// infer media type from form if not in response metadata
581580
content.type ??= form.contentType ?? "application/json";
582581
// check if returned media type is the same as expected media type (from TD)
583582
this.checkMediaTypeOrThrow(content, form);
584-
return new InteractionOutput(content, form, outputDataSchema, { ignoreValidation });
583+
return new InteractionOutput(content, form, outputDataSchema);
585584
}
586585

587586
// check if returned media type is the same as expected media type (from TD)
@@ -601,13 +600,13 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
601600
content: Content,
602601
form: Form,
603602
outputDataSchema: WoT.DataSchema | undefined,
604-
ignoreValidation: boolean
603+
synchronous?: boolean
605604
): ActionInteractionOutput {
606605
// infer media type from form if not in response metadata
607606
content.type ??= form.contentType ?? "application/json";
608607
// check if returned media type is the same as expected media type (from TD)
609608
this.checkMediaTypeOrThrow(content, form);
610-
return new ActionInteractionOutput(content, form, outputDataSchema, { ignoreValidation });
609+
return new ActionInteractionOutput(content, form, outputDataSchema, synchronous);
611610
}
612611

613612
async _readProperties(propertyNames: string[]): Promise<WoT.PropertyReadMap> {
@@ -726,8 +725,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
726725

727726
const content = await client.invokeResource(form, input);
728727
try {
729-
const ignoreValidation = ta.synchronous === undefined ? true : !ta.synchronous;
730-
return this.handleActionInteractionOutput(content, form, ta.output, ignoreValidation);
728+
return this.handleActionInteractionOutput(content, form, ta.output, ta.synchronous);
731729
} catch (e) {
732730
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
733731
throw new Error(`Error while processing action for ${ta.title}. ${error.message}`);
@@ -770,7 +768,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
770768
// next
771769
(content) => {
772770
try {
773-
listener(this.handleInteractionOutput(content, form, tp, false));
771+
listener(this.handleInteractionOutput(content, form, tp));
774772
} catch (e) {
775773
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
776774
warn(`Error while processing observe property for ${tp.title}. ${error.message}`);
@@ -826,7 +824,7 @@ export default class ConsumedThing extends Thing implements IConsumedThing {
826824
formWithoutURITemplates,
827825
(content) => {
828826
try {
829-
listener(this.handleInteractionOutput(content, form, te.data, false));
827+
listener(this.handleInteractionOutput(content, form, te.data));
830828
} catch (e) {
831829
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
832830
warn(`Error while processing event for ${te.title}. ${error.message}`);

packages/core/src/interaction-output.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export class InteractionOutput implements WoT.InteractionOutput {
4242
dataUsed: boolean;
4343
form?: WoT.Form;
4444
schema?: WoT.DataSchema;
45-
ignoreValidation: boolean; // by default set to false
4645

4746
public get data(): ReadableStream {
4847
if (this.#stream) {
@@ -58,11 +57,10 @@ export class InteractionOutput implements WoT.InteractionOutput {
5857
return (this.#stream = ProtocolHelpers.toWoTStream(this.#content.body) as ReadableStream);
5958
}
6059

61-
constructor(content: Content, form?: WoT.Form, schema?: WoT.DataSchema, options = { ignoreValidation: false }) {
60+
constructor(content: Content, form?: WoT.Form, schema?: WoT.DataSchema) {
6261
this.#content = content;
6362
this.form = form;
6463
this.schema = schema;
65-
this.ignoreValidation = options.ignoreValidation ?? false;
6664
this.dataUsed = false;
6765
}
6866

@@ -129,7 +127,7 @@ export class InteractionOutput implements WoT.InteractionOutput {
129127
// validate the schema
130128
const validate = ajv.compile<T>(this.schema);
131129

132-
if (!this.ignoreValidation && !validate(json)) {
130+
if (!this.ignoreValidation() && !validate(json)) {
133131
debug(`schema = ${util.inspect(this.schema, { depth: 10, colors: true })}`);
134132
debug(`value: ${json}`);
135133
debug(`Error: ${validate.errors}`);
@@ -139,9 +137,24 @@ export class InteractionOutput implements WoT.InteractionOutput {
139137
this.#value = json;
140138
return json as T;
141139
}
140+
141+
protected ignoreValidation(): boolean {
142+
return false; // by default set to false
143+
}
142144
}
143145

144146
export class ActionInteractionOutput extends InteractionOutput implements WoT.ActionInteractionOutput {
147+
synchronous?: boolean;
148+
149+
constructor(content: Content, form?: WoT.Form, schema?: WoT.DataSchema, synchronous?: boolean) {
150+
super(content, form, schema);
151+
this.synchronous = synchronous;
152+
}
153+
154+
protected ignoreValidation(): boolean {
155+
return this.synchronous === undefined ? true : !this.synchronous; // validate data for synchronous action only
156+
}
157+
145158
async query<T extends WoT.InteractionOutput>(
146159
params?: WoT.InteractionInput,
147160
options?: WoT.InteractionOptions

0 commit comments

Comments
 (0)