audax
10/24/2024, 8:17 AMJohann Pardanaud
10/24/2024, 8:33 AMJohann Pardanaud
10/24/2024, 8:33 AMJohann Pardanaud
10/24/2024, 8:33 AMaudax
10/24/2024, 8:34 AMaudax
10/24/2024, 8:35 AMaudax
10/24/2024, 8:37 AMaudax
10/24/2024, 8:38 AMaudax
10/24/2024, 8:39 AMJohann Pardanaud
10/24/2024, 8:50 AMisNotEmpty() otherwise {
"not-empty-message".populateWith(value1, value2, value3)
}
Where not empty-message
is an identifier declared somewhere else with its message and contains slots for the values:
translations.en_US["not-empty-message"] = "Should not be empty: %s, %s, %s"
translations.fr_FR["not-empty-message"] = "Ne devrait pas être vide: %s, %s, %s"
(this is a draft, everything here is subject to change 😄)Johann Pardanaud
10/24/2024, 8:51 AMaudax
10/24/2024, 8:53 AMaudax
10/24/2024, 9:06 AMclass EmployeeBirthDateValidationContext(
val currentDate: LocalDate
)
val birthDateValidation = Validator<EmployeeBirthDateValidationContext, EmployeeBirthData?> { context ->
if (birthDate.unwrap() == null) {
fictiveBirthDate.isNotNull() otherwise { BirthDateErrorKey.MUST_NOT_BE_EMPTY.key }
fictiveBirthDate.birthYear.isNotNull() otherwise { BirthDateErrorKey.FICTIVE_BIRTH_YEAR_MUST_NOT_BE_EMPTY.key }
fictiveBirthDate.birthYear.isGreaterThanOrEqualTo(1900) otherwise { BirthDateErrorKey.FICTIVE_BIRTH_YEAR_MIN_1900.key }
fictiveBirthDate.birthYear.isLowerThanOrEqualTo(2999) otherwise { BirthDateErrorKey.FICTIVE_BIRTH_YEAR_MAX_2999.key }
if (fictiveBirthDate.birthMonth.unwrap() != null) {
fictiveBirthDate.birthMonth.isBetween(1..12) otherwise { BirthDateErrorKey.FICTIVE_BIRTH_MONTH_MUST_NOT_BE_BETWEEN_1_AND_12.key }
}
} else {
birthDate.isNotNull() otherwise { BirthDateErrorKey.MUST_NOT_BE_EMPTY.key }
birthDate.map { it?.toKotlinLocalDate()?.year }
.isGreaterThanOrEqualTo(1880) otherwise { BirthDateErrorKey.YEAR_MUST_BE_1880_OR_LATER.key }
birthDate.asDate().isBeforeOrEqualTo(
context.currentDate.minus(
12,
DateTimeUnit.YEAR
)
) otherwise { BirthDateErrorKey.MUST_BE_AT_LEAST_12.key }
}
}
Anastasios Georgousakis
10/24/2024, 9:22 AMJohann Pardanaud
10/24/2024, 9:33 AMJohann Pardanaud
10/24/2024, 9:34 AMaudax
10/24/2024, 9:34 AMaudax
10/24/2024, 9:38 AM@JsExport
fun validateEmployee(employee: Employee, payslipMonth: SharedDate): Array<ValidationError> =
when (val result = employeeValidation(
EmployeeValidationContext(
payslipMonth.toKotlinLocalDate()
),
employee)) {
is ValidationResult.Failure -> result.violations.byPath.map { (path, violations) ->
ValidationError(
path.joinToString("."),
violations.first().message
)
}.toTypedArray()
else -> arrayOf()
}
audax
10/24/2024, 9:39 AMfunction validationErrorsToHookFormFieldErrors(validationErrors: ValidationError[]) {
return mapValidationErrorsToFields(validationErrors).reduce(
(allErrors, currentError) => ({
...allErrors,
[`${currentError.dataPath}`]: {
type: "validation",
// @ts-ignore
message: t(currentError.message),
},
}),
{}
);
}
audax
10/24/2024, 9:39 AMJohann Pardanaud
10/24/2024, 9:43 AMaudax
10/24/2024, 9:46 AMmessage: t(currentError.message, currentError.additionalData)
and I specify the additional data via otherwise { (BirthDateErrorKey.MUST_BE_AT_LEAST_12.key, birthDate, fictiveBirthDate.year) }
Johann Pardanaud
10/24/2024, 9:47 AMJohann Pardanaud
10/24/2024, 9:47 AMJohann Pardanaud
10/24/2024, 9:47 AMaudax
10/24/2024, 9:47 AMAnastasios Georgousakis
10/29/2024, 1:38 PMJohann Pardanaud
10/29/2024, 1:54 PMaudax
10/30/2024, 12:50 PMaudax
10/30/2024, 12:51 PMJohann Pardanaud
10/30/2024, 1:55 PM