Skip to content

Commit

Permalink
fix: fix handling of uri variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 5, 2024
1 parent 3a2d79a commit d265c4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
27 changes: 9 additions & 18 deletions lib/src/core/implementation/augmented_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,20 @@ final class AugmentedForm implements Form {
"level: ${uncoveredHrefUriVariables.join(", ")}.");
}

final missingTdLevelUserInput = uriVariablesInHref.where(
(uriVariableName) =>
!userProvidedUriVariables.containsKey(uriVariableName),
);

if (missingTdLevelUserInput.isNotEmpty) {
throw UriVariableException(
"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: "
"${userProvidedUriVariables.keys.join(", ")}.",
);
}

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

final schema = JsonSchema.create(value);
final result = schema.validate(userProvidedUriVariables[key]);
final userProvidedValue = userProvidedUriVariables[key];

if (userProvidedValue == null) {
continue;
}

final schemaValue = affordanceUriVariable.value;
final schema = JsonSchema.create(schemaValue);
final result = schema.validate(userProvidedValue);

if (!result.isValid) {
throw ValidationException("Invalid type for URI variable $key");
Expand Down
18 changes: 4 additions & 14 deletions test/core/augmented_form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ void main() {
);

expect(
() => augmentedForm2.resolvedHref,
throwsA(isA<UriVariableException>()),
augmentedForm2.resolvedHref,
Uri.parse("http://example.org/weather/?lat=5"),
);

final augmentedForm3 = AugmentedForm(
Expand All @@ -178,18 +178,8 @@ void main() {
);

expect(
() => augmentedForm3.resolvedHref,
throwsA(
predicate(
(exception) =>
exception is UriVariableException &&
exception.toString() ==
"UriVariableException: The following URI template "
"variables defined at the TD level are not covered by "
"the values provided by the user: lat. Values for the "
"following variables were received: long.",
),
),
augmentedForm3.resolvedHref,
Uri.parse("http://example.org/weather/?long=10"),
);

final augmentedForm4 = AugmentedForm(
Expand Down

0 comments on commit d265c4f

Please sign in to comment.