Merge http4k
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package be.simplenotes.domain.validation
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.left
|
||||
import arrow.core.right
|
||||
import be.simplenotes.domain.model.User
|
||||
import be.simplenotes.domain.usecases.login.InvalidLoginForm
|
||||
import be.simplenotes.domain.usecases.login.LoginForm
|
||||
import be.simplenotes.domain.usecases.register.InvalidRegisterForm
|
||||
import be.simplenotes.domain.usecases.register.RegisterForm
|
||||
import io.konform.validation.Validation
|
||||
import io.konform.validation.jsonschema.maxLength
|
||||
import io.konform.validation.jsonschema.minLength
|
||||
|
||||
internal object UserValidations {
|
||||
private val loginValidator = Validation<LoginForm> {
|
||||
LoginForm::username required {
|
||||
minLength(3)
|
||||
maxLength(50)
|
||||
}
|
||||
LoginForm::password required {
|
||||
minLength(8)
|
||||
maxLength(72) // jbcrypt limit, see https://security.stackexchange.com/a/39851
|
||||
}
|
||||
}
|
||||
|
||||
fun validateLogin(form: LoginForm): Either<InvalidLoginForm, User> {
|
||||
val errors = loginValidator.validate(form).errors
|
||||
return if (errors.isEmpty()) User(form.username!!, form.password!!).right()
|
||||
else return InvalidLoginForm(errors).left()
|
||||
}
|
||||
|
||||
fun validateRegister(form: RegisterForm): Either<InvalidRegisterForm, User> {
|
||||
val errors = loginValidator.validate(form).errors
|
||||
return if (errors.isEmpty()) User(form.username!!, form.password!!).right()
|
||||
else return InvalidRegisterForm(errors).left()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user