Skip to content
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

Refactor Constraints classes #60

Open
carltonwhitehead opened this issue Mar 10, 2022 · 0 comments
Open

Refactor Constraints classes #60

carltonwhitehead opened this issue Mar 10, 2022 · 0 comments

Comments

@carltonwhitehead
Copy link
Contributor

Most of the current Constraints classes don't lend themselves well to reuse beyond their original goal of validating objects. They validate properties of the parameterized type, but they don't expose those individual validations for reuse. Those turn out to be really handy for letting UIs provide live validation feedback. I explored refactoring these recently. See EventPersistConstraints

override fun assess(candidate: Event) {
  hasUniqueName(candidate.id to candidate.name).getOrThrow()
  // brevity
}

val hasUniqueName = constraint<Pair<UUID, String>>(
    { (id, name) -> resource.stream { it.id != id }.noneMatch { it.name == name } },
    { "Name is not unique" }
)

UIs can then reference constraints.hasUniqueName() to and pass in candidate values in order to give the user validation feedback.

This is just an example. There's an obvious performance concern with iterating every record as the user types, so UI would need to implement with care.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant