Skip to content

Commit

Permalink
Add deep link validation in addDeeplink
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Jul 20, 2021
1 parent a76a2f5 commit 2a2d790
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ class NavDestinationAndroidTest {
val destination = NoOpNavigator().createDestination()
destination.addArgument("testString", stringArgument())
destination.addDeepLink("android-app://androidx.navigation.test/{testString}")
val deepLink = Uri.parse("android-app://androidx.navigation.test/test")
destination.addDeepLink(deepLink.toString())

val deepLink = Uri.parse("android-app://androidx.navigation.test/test")
assertWithMessage("Deep link should match")
.that(destination.hasDeepLink(deepLink)).isTrue()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class NavDeepLink internal constructor(

private var mimeTypePattern: Pattern? = null

/** Arguments present in the deep link, including both path and query arguments. */
internal val argumentsNames: List<String>
get() = arguments + paramArgMap.keys

public var isExactDeepLink: Boolean = false
/** @suppress */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ public open class NavDestination(
* @see NavController.navigate
*/
public fun addDeepLink(navDeepLink: NavDeepLink) {
val missingRequiredArguments = arguments.filterValues { !it.isDefaultValuePresent }
.keys
.filter { it !in navDeepLink.argumentsNames }
require(missingRequiredArguments.isEmpty()) {
"Deep link ${navDeepLink.uriPattern} can't be used to open destination $this.\n" +
"Following required arguments are missing: $missingRequiredArguments"
}

deepLinks.add(navDeepLink)
}

Expand Down

0 comments on commit 2a2d790

Please sign in to comment.