Validate several ids in constructors (#336)
* Validate ids in constructors. * Remove redundant `.value` usage in string interpolation. * Make a distinction between `SessionId` and `UserId` in `TestData`.
This commit is contained in:
parent
638b45930e
commit
fae3417181
36 changed files with 193 additions and 199 deletions
|
|
@ -20,10 +20,12 @@ import io.element.android.libraries.matrix.api.BuildConfig
|
|||
import java.io.Serializable
|
||||
|
||||
@JvmInline
|
||||
value class EventId(val value: String) : Serializable
|
||||
value class EventId(val value: String) : Serializable {
|
||||
init {
|
||||
if (BuildConfig.DEBUG && !MatrixPatterns.isEventId(value)) {
|
||||
error("`$value` is not a valid event id.\nExample event id: `\$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg`.")
|
||||
}
|
||||
}
|
||||
|
||||
fun String.asEventId() = if (BuildConfig.DEBUG && !MatrixPatterns.isEventId(this)) {
|
||||
error("`$this` is not a valid event Id")
|
||||
} else {
|
||||
EventId(this)
|
||||
override fun toString(): String = value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,13 +21,12 @@ import java.io.Serializable
|
|||
|
||||
@JvmInline
|
||||
value class RoomId(val value: String) : Serializable {
|
||||
override fun toString(): String {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
fun String.asRoomId() = if (BuildConfig.DEBUG && !MatrixPatterns.isRoomId(this)) {
|
||||
error("`$this` is not a valid room Id")
|
||||
} else {
|
||||
RoomId(this)
|
||||
init {
|
||||
if (BuildConfig.DEBUG && !MatrixPatterns.isRoomId(value)) {
|
||||
error("`$value` is not a valid room id.\n Example room id: `!room_id:domain`.")
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,4 @@
|
|||
|
||||
package io.element.android.libraries.matrix.api.core
|
||||
|
||||
import io.element.android.libraries.matrix.api.BuildConfig
|
||||
|
||||
typealias SessionId = UserId
|
||||
|
||||
fun String.asSessionId() = if (BuildConfig.DEBUG && !MatrixPatterns.isSessionId(this)) {
|
||||
error("`$this` is not a valid session Id")
|
||||
} else {
|
||||
SessionId(this)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,15 +20,21 @@ import io.element.android.libraries.matrix.api.BuildConfig
|
|||
import java.io.Serializable
|
||||
|
||||
@JvmInline
|
||||
value class SpaceId(val value: String) : Serializable
|
||||
value class SpaceId(val value: String) : Serializable {
|
||||
init {
|
||||
if (BuildConfig.DEBUG && !MatrixPatterns.isSpaceId(value)) {
|
||||
error(
|
||||
"`$value` is not a valid space id.\n" +
|
||||
"Space ids are the same as room ids.\n" +
|
||||
"Example space id: `!space_id:domain`."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = value
|
||||
}
|
||||
|
||||
/**
|
||||
* Value to use when no space is selected by the user.
|
||||
*/
|
||||
val MAIN_SPACE = SpaceId("!mainSpace")
|
||||
|
||||
fun String.asSpaceId() = if (BuildConfig.DEBUG && !MatrixPatterns.isSpaceId(this)) {
|
||||
error("`$this` is not a valid space Id")
|
||||
} else {
|
||||
SpaceId(this)
|
||||
}
|
||||
val MAIN_SPACE = SpaceId("!mainSpace:local")
|
||||
|
|
|
|||
|
|
@ -20,10 +20,16 @@ import io.element.android.libraries.matrix.api.BuildConfig
|
|||
import java.io.Serializable
|
||||
|
||||
@JvmInline
|
||||
value class ThreadId(val value: String) : Serializable
|
||||
value class ThreadId(val value: String) : Serializable {
|
||||
init {
|
||||
if (BuildConfig.DEBUG && !MatrixPatterns.isThreadId(value)) {
|
||||
error(
|
||||
"`$value` is not a valid thread id.\n" +
|
||||
"Thread ids are the same as event ids.\n" +
|
||||
"Example thread id: `\$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg`."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun String.asThreadId() = if (BuildConfig.DEBUG && !MatrixPatterns.isThreadId(this)) {
|
||||
error("`$this` is not a valid thread Id")
|
||||
} else {
|
||||
ThreadId(this)
|
||||
override fun toString(): String = value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,13 +21,12 @@ import java.io.Serializable
|
|||
|
||||
@JvmInline
|
||||
value class UserId(val value: String) : Serializable {
|
||||
override fun toString(): String {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
fun String.asUserId() = if (BuildConfig.DEBUG && !MatrixPatterns.isUserId(this)) {
|
||||
error("`$this` is not a valid user Id")
|
||||
} else {
|
||||
UserId(this)
|
||||
init {
|
||||
if (BuildConfig.DEBUG && !MatrixPatterns.isUserId(value)) {
|
||||
error("`$value` is not a valid user id.\nExample user id: `@name:domain`.")
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = value
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue