Move packages + remove circular dependencies
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
package be.simplenotes.domain.validation
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.left
|
||||
import arrow.core.right
|
||||
import be.simplenotes.types.User
|
||||
import be.simplenotes.domain.usecases.users.delete.DeleteError
|
||||
import be.simplenotes.domain.usecases.users.delete.DeleteForm
|
||||
import be.simplenotes.domain.usecases.users.login.InvalidLoginForm
|
||||
import be.simplenotes.domain.usecases.users.login.LoginForm
|
||||
import be.simplenotes.domain.usecases.users.register.InvalidRegisterForm
|
||||
import be.simplenotes.domain.usecases.users.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()
|
||||
}
|
||||
|
||||
private val deleteValidator = Validation<DeleteForm> {
|
||||
DeleteForm::username required {
|
||||
minLength(3)
|
||||
maxLength(50)
|
||||
}
|
||||
DeleteForm::password required {
|
||||
minLength(8)
|
||||
maxLength(72)
|
||||
}
|
||||
DeleteForm::checked required {
|
||||
addConstraint("Should be checked") { it }
|
||||
}
|
||||
}
|
||||
|
||||
fun validateDelete(form: DeleteForm): Either<DeleteError.InvalidForm, User> {
|
||||
val errors = deleteValidator.validate(form).errors
|
||||
return if (errors.isEmpty()) User(form.username!!, form.password!!).right()
|
||||
else return DeleteError.InvalidForm(errors).left()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user