splendo / kaluga

Collection of multiplatform kotlin components, mainly using coroutines and flow
Apache License 2.0
276 stars 6 forks source link

Validate Identifier string (MAC address) on Android #752

Open avdyushin opened 7 months ago

avdyushin commented 7 months ago

Describe the bug A clear and concise description of what the bug is.

Currently on iOS we check if Identifier is a valid UUID (done automatically by NSUUID object). However on Android we just use raw string value. We can check if it's a valid MAC address as well.

How to fix, some ideas from @Daeda88

private val regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$".toRegex()
fun main() {
    println("3D:F2:C9:A6:B3:4F".asValidIdentifier())
}

private fun String.asValidIdentifier(): String? = if (regex.matches(this)) {
    this
} else {
    null
}

or

private fun String.asValidIdentifier(): String? = regex.matchEntire(this)?.let {
    it.groupValues[0]
}

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.