-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impure validation #17
Comments
Thanks for an example @CLOVIS-AI, haven't thought about such usage before 🤔 There are actually two problems
Taking all that into account, I suggest that impure logic can be incapsulated in the type itself ( value class Ref private constructor(
val id: String
) {
companion object : Exact<String, IO<Ref>> by exact({
ensure(isUUID(raw))
Ref(it)
})
}
// IO<Ref>
class MaybeRef(
private val value: ref: Ref,
private val predicate: suspend (T) -> Boolean // to validate that id is present at the moment
) {
suspend fun getOrThrow(): Ref
suspend fun get(): Either<NonExistingRef, Ref>
}
class MaybeRefFactory(private val service: Service) : Exact<String, MaybeRef> by suspendExact({
val ref = ensure(Ref)
MaybeRef(ref) {
service.find(raw.id).isPresent()
}
})
suspend fun main() {
val service = FileService(…)
val id = service.create(…)
val maybeRef: MaybeRef = MaybeRefFactory.fromOrThrow(id)
val ref: Ref = maybeRef.getOrThrow() // will check that id is present, otherwise throws an exception
} The only thing that is missing now is a In your case maybe an Thanks once again for an example, it gave me plenty of new thoughts of how we can work with types in Exact! |
@ustits Arrow doesn't have |
Related to both of your notes:
Yes, that's why I proposed creating another interface for impure validation. This way, the pure variant keeps its niceness.
That's true, but there's also no way around it. It's not possible to have a value be continuously validated depending on changes happening on some other machines. But I often know that the lifetime of the |
I see your point, actually you are right, we will need something like But I struggle to get the point of
|
To me, having to create an intermediary object is less of an issue than having some Exact-enabled classes for which the Exact instance is not the companion object. I'm interested in what the other members of the project think of this tradeoff. |
Although arrow-exact is originally meant for type refinements, our current DSL can handle any kind of validation.
In my projects, I have two major kinds of validation needs: pure and impure. So far, we have concentrated on pure validation (and I think we're doing a good job of it).
Here's an example of impure validation, that I have seen in the real world (for the curious). We can simplify this example to: the class we want to do validation on is called
Reference
. It stores an identifier to another business entity calledFile
(here, we don't care what it is). AReference
is only valid if the referencedFile
exists at the time of instantiation. For this, we need two things:suspend
, to make a network requestFileService
Here's an example of how it could look like:
Usage:
What do you think?
The text was updated successfully, but these errors were encountered: