Skip to content

Commit

Permalink
fixup! feat!: improve TD serialization behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 3, 2024
1 parent 6207769 commit 89bbde5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
35 changes: 21 additions & 14 deletions lib/src/core/augmented_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,39 @@ final class AugmentedForm implements Form {

void _validateUriVariables(
List<String> hrefUriVariables,
Map<String, Object?> affordanceUriVariables,
Map<String, Object> uriVariables,
Map<String, Object?> affordanceLevelUriVariables,
Map<String, Object> tdLevelUriVariables,
) {
final missingTdDefinitions =
hrefUriVariables.where((element) => !uriVariables.containsKey(element));
final missingTdLevelUserInput = hrefUriVariables.where(
(uriVariableName) => !tdLevelUriVariables.containsKey(uriVariableName),
);

if (missingTdDefinitions.isNotEmpty) {
if (missingTdLevelUserInput.isNotEmpty) {
throw UriVariableException(
"$missingTdDefinitions do not have defined "
"uriVariables in the TD",
"The following URI template variables defined at the TD level are not "
"covered by the values provided by the user: "
"${missingTdLevelUserInput.join(", ")}. "
"Values for the following variables were received: "
"${tdLevelUriVariables.keys.join(", ")}.",
);
}

final missingUserInput = hrefUriVariables
.where((element) => !affordanceUriVariables.containsKey(element));
final missingAffordanceLevelUserInput = hrefUriVariables
.where((element) => !affordanceLevelUriVariables.containsKey(element));

if (missingUserInput.isNotEmpty) {
if (missingAffordanceLevelUserInput.isNotEmpty) {
throw UriVariableException(
"$missingUserInput did not have defined "
"Values in the provided InteractionOptions.",
"The following URI template variables defined at the affordance level "
"are not covered by the values provided by the user: "
"${missingAffordanceLevelUserInput.join(", ")}. "
"Values for the following variables were received: "
"${affordanceLevelUriVariables.keys.join(", ")}.",
);
}

// We now assert that all user provided values comply to the Schema
// definition in the TD.
for (final affordanceUriVariable in affordanceUriVariables.entries) {
for (final affordanceUriVariable in affordanceLevelUriVariables.entries) {
final key = affordanceUriVariable.key;
final value = affordanceUriVariable.value;

Expand All @@ -172,7 +179,7 @@ final class AugmentedForm implements Form {
}

final schema = JsonSchema.create(value);
final result = schema.validate(uriVariables[key]);
final result = schema.validate(tdLevelUriVariables[key]);

if (!result.isValid) {
throw ValidationException("Invalid type for URI variable $key");
Expand Down
34 changes: 32 additions & 2 deletions test/core/augmented_form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import "package:dart_wot/dart_wot.dart";
import "package:dart_wot/src/definitions/security/no_security_scheme.dart";
import "package:dart_wot/src/definitions/validation/validation_exception.dart";
import "package:test/test.dart";

void main() {
Expand Down Expand Up @@ -135,7 +136,7 @@ void main() {
);
final affordance = thingDescription.properties?["test"];

final augmentedForm = AugmentedForm(
final augmentedForm1 = AugmentedForm(
affordance!.forms.first,
affordance,
thingDescription,
Expand All @@ -146,9 +147,38 @@ void main() {
);

expect(
augmentedForm.resolvedHref,
augmentedForm1.resolvedHref,
Uri.parse("http://example.org/weather/?lat=5&long=10"),
);

final augmentedForm2 = AugmentedForm(
affordance.forms.first,
affordance,
thingDescription,
const {
"lat": 5,
},
);

expect(
() => augmentedForm2.resolvedHref,
throwsA(isA<UriVariableException>()),
);

final augmentedForm3 = AugmentedForm(
affordance.forms.first,
affordance,
thingDescription,
const {
"lat": "hi",
"long": 10,
},
);

expect(
() => augmentedForm3.resolvedHref,
throwsA(isA<ValidationException>()),
);
});
});
}

0 comments on commit 89bbde5

Please sign in to comment.