Home > Software design >  arrow validation more then 10 fields
arrow validation more then 10 fields

Time:06-28

all I have a question how can I validate more then 10 values with arrow in kotlin.

fun CreateEventDTO.validate(): Validated<IncorrectInput, CreateEventDTO> =
  name.isEventNameValid()
    .zip(
      about.isAboutValid(),
      phone.isPhoneValid(),
      price.isPriceValid(),
      location.isLocationValid(),
      startDate.isStartDateValid(),
   // TODO add common validation for date
      // endDate.isEndDateValid(),
      status.isEventStatusValid(),
      access.isEventAccessValid(),
      category.isEventCategoryValid(),
      musicStyles.isMusicStyleValid()
    )
    { name, _, _, price, location, status, access, category, musicStyles ->
      CreateEventDTO(
        name = name,
        about = about,
        phone = phone,
        price = price,
        location = location,
        startDate = startDate,
        endDate = endDate,
        status = status,
        access = access,
        category = category,
        musicStyles = musicStyles
      )
    }
    .mapLeft(ApiError::IncorrectInput)

If I will try to add one more validation then I will get an error because zip just allows up to 10values

Required:
Semigroup<TypeVariable(E)>
Found:
ValidatedNel<InvalidAbout, String?> /* = Validated<NonEmptyList<InvalidAbout>, String?> */

is there any other elegant ways to handle this?

CodePudding user response:

Kotlin requires all functions to be explicitly defined, and we cannot define an infinite amount of methods. Therefore Arrow made the decision to limit to 9 arguments.

However, you can easily combine different zip methods with each-other using tuples. For reaching 10 arguments, you can combine 9 2 in the following manner.

fun CreateEventDTO.validate(): Validated<IncorrectInput, CreateEventDTO> =
  name.isEventNameValid()
    .zip(
      about.isAboutValid(),
      phone.isPhoneValid(),
      price.isPriceValid(),
      location.isLocationValid(),
      startDate.isStartDateValid(),
      endDate.isEndDateValid(),
      status.isEventStatusValid(),
      access.isEventAccessValid(),
      category.isEventCategoryValid().zip(musicStyles.isMusicStyleValid(), ::Pair)
    )
    { name, _, _, price, location, startDate, endDate, status, access, (category, musicStyles) ->
      CreateEventDTO(
        name = name,
        about = about,
        phone = phone,
        price = price,
        location = location,
        startDate = startDate,
        endDate = endDate,
        status = status,
        access = access,
        category = category,
        musicStyles = musicStyles
      )
    }
    .mapLeft(ApiError::IncorrectInput)
  • Related