Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new constraint to validate if a string is a uuid #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,12 @@ object Email : Constraint
* @since 0.10.0
*/
object Website : Constraint

/**
* Represents a constraint that validate if the value is a valid UUID
*
* @author Renan Gigliotti
* @see Constraint
* @since 0.13.0
*/
object UUID : Constraint
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.valiktor.constraints.NotMatch
import org.valiktor.constraints.NotStartWith
import org.valiktor.constraints.Size
import org.valiktor.constraints.StartsWith
import org.valiktor.constraints.UUID
import org.valiktor.constraints.Website

/**
Expand Down Expand Up @@ -495,3 +496,16 @@ fun <E> Validator<E>.Property<String?>.isWebsite(): Validator<E>.Property<String
Regex("^(https?:\\/\\/)?([a-zA-Z0-9]+(-?[a-zA-Z0-9])*\\.)+[\\w]{2,}(\\/\\S*)?\$")
)
}

/**
* Validates if the [String] property value is a valid uuid
*
* @receiver the property to be validated
* @return the same receiver property
*/
fun <E> Validator<E>.Property<String?>.isUUID(): Validator<E>.Property<String?> =
this.validate(UUID) {
it == null || it.matches(
Regex("^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}\$")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message=Must end with {suffix}
org.valiktor.constraints.NotEndWith.message=Must not end with {suffix}
org.valiktor.constraints.Email.message=Must be a valid email address
org.valiktor.constraints.Website.message=Must be a valid website
org.valiktor.constraints.UUID.message=Must be a valid UUID
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message=Ha d'acabar amb {suffix}
org.valiktor.constraints.NotEndWith.message=No pot acabar amb {suffix}
org.valiktor.constraints.Email.message=Ha de ser una adreça d'e-mail vàlida
org.valiktor.constraints.Website.message=Ha de ser un lloc web vàlid
org.valiktor.constraints.UUID.message=Ha de ser un UUID vàlid

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an end of line to the file, apply to all files

Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message=Muss mit {suffix} enden
org.valiktor.constraints.NotEndWith.message=Darf nicht mit {suffix} enden
org.valiktor.constraints.Email.message=Muss eine gültige E-Mail-Adresse sein
org.valiktor.constraints.Website.message=Muss eine gültige Website sein
org.valiktor.constraints.UUID.message=Muss eine gültige UUID sein
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message=Must end with {suffix}
org.valiktor.constraints.NotEndWith.message=Must not end with {suffix}
org.valiktor.constraints.Email.message=Must be a valid email address
org.valiktor.constraints.Website.message=Must be a valid website
org.valiktor.constraints.UUID.message=Must be a valid UUID
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message=Tiene que terminar con {suffix}
org.valiktor.constraints.NotEndWith.message=No puede terminar con {suffix}
org.valiktor.constraints.Email.message=Tiene que ser una dirección de e-mail válida
org.valiktor.constraints.Website.message=Tiene que ser un sitio web válido
org.valiktor.constraints.UUID.message=Tiene que ser uno UUID válido
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message={suffix} で終わる文字を指定
org.valiktor.constraints.NotEndWith.message={suffix} 以外で終わる文字を指定する必要があります
org.valiktor.constraints.Email.message=有効なメールアドレス形式である必要があります
org.valiktor.constraints.Website.message=有効なウェブサイト形式である必要があります
org.valiktor.constraints.UUID.message=有効なUUIDである必要があります
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ org.valiktor.constraints.EndsWith.message=Deve terminar com {suffix}
org.valiktor.constraints.NotEndWith.message=Não deve terminar com {suffix}
org.valiktor.constraints.Email.message=Deve ser um endereço de e-mail válido
org.valiktor.constraints.Website.message=Deve ser um website válido
org.valiktor.constraints.UUID.message=Deve ser um UUID válido
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,19 @@ class WebsiteTest {
)
}
}

class UUIDTest {

@Test
fun `should validate messages`() {
assertThat(UUID.interpolatedMessages()).containsExactly(
entry(SupportedLocales.DEFAULT, "Must be a valid UUID"),
entry(SupportedLocales.CA, "Ha de ser un UUID vàlid"),
entry(SupportedLocales.DE, "Muss eine gültige UUID sein"),
entry(SupportedLocales.EN, "Must be a valid UUID"),
entry(SupportedLocales.ES, "Tiene que ser uno UUID válido"),
entry(SupportedLocales.JA, "有効なUUIDである必要があります"),
entry(SupportedLocales.PT_BR, "Deve ser um UUID válido")
)
}
}
Loading