<@U02SEA99VJB> AFAIK `otherwise { }` only supports...
# akkurate
d
@Johann Pardanaud AFAIK
otherwise { }
only supports strings right? It's not so great when validating forms in Jetpack Compose where an error is an R.string.XXX (an Int...)... any chance of expanding that to support other types?
j
Yes! I will improve this in the future, we already have two issues to track this: • https://github.com/nesk/akkurate/issues/34https://github.com/users/nesk/projects/1/views/1?pane=issue&amp;itemId=38823597
Clearly, this is something missing
However, I would appreciate an exemple of your code to have a better comprehension of your own issues and your expectations. Could you provide me a sample?
d
I'm not so sure Akkurate needs to be an i18n library... lets take your overview example:
Copy code
val validateBook = Validator<Book> {
    title.isNotEmpty() otherwise { R.string.missingTitle }

    releaseDate.isInPast() otherwise { R.string.releaseDateMustBeInPast to listOf(releaseDate.value) } // In some cases a stringResource is a template with interpolation of values so here it would be nice to pass in a Pair...

    authors.hasSizeBetween(1..10) otherwise { R.string.wrongAuthorCount }

    authors.each { 
        (firstName and lastName) {
            isNotEmpty() otherwise { R.string.missingName }
        }
    }
}
🙏 1
Then let the user choose whatever framework they're already using...
👍 1
j
Akkurate won't be an i18n library, but it has to connect to existing ones, which means providing more context.
Including support for other types than String 🙂
d
Basically a resource id and some parameters for interpolation is what's needed (sometimes the id is not a String and the parameters could be any type)... that's why the simplest way would just be to allow the user to decide what they want to get back in their constraint violations... metadata forces the user to have a MapString, String (maybe I'm wrong, but that's the example from that issue...) and translateTo forces to map strings to R.string.XXX keys... or maybe I'm misunderstanding?
j
This is currently under consideration, the issue has been created by an akkurate user but it probably wont be implemented as-is. I agree with you, the real feature is being able to return whatever you want
👍🏼 1
j
I think this is the same problem
Result
has and
Either
fixed. The left part not being a generic and force to use
Exception
.
Copy code
Validator<Int, Book> { }

Validator<() -> String, Book> { }
If it uses a generic you wouldn't need any integration with any library, but you can still provide bindings for them anyway. Not good names but to show some samples:
Copy code
ValidatorOrResource<Book> { }

ValidatorOrComposeResource<Book> { }
j
That's good examples actually, thank you! I will probably implement something like that 🙂
d
I'm just wondering if the user has access to the current value being validated, so that when returning a resource one can return values to be interpolated in them like
The book %s is invalid
needs the book name there...
That's why I had a
Pair<Int, List<Any>>
returned in one validation up there...
Not all of the messages would need interpolation though
j
The validated value will be available, this is something that has already been requested in this thread: https://kotlinlang.slack.com/archives/C061JJM6YUQ/p1729757849008129