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

Add support for construction-time validation #40

Open
mkovacs-coursera opened this issue Dec 13, 2016 · 0 comments
Open

Add support for construction-time validation #40

mkovacs-coursera opened this issue Dec 13, 2016 · 0 comments

Comments

@mkovacs-coursera
Copy link
Contributor

The Problem

While Courier supports data validation, the validator functions it provides need to be called explicitly.

That's feasible when used with the right frameworks - such as Naptime - which call the validator functions at interface boundaries, but it is impractical for other use cases.

For example, consider the following model:

record JeopardyResponse {
  @validate.regex = {
    "regex": "^(What|Who) is"
  }
  question: string
}

A user might prefer if invalid constructions such as new JeopardyResponse(question = "Why is the sky blue") failed early without any explicit call to validation.

Currently the closest they can get is by either

  • calling validation at every construction site, which is error prone and tedious
  • rolling their own wrapper types that call validation (more complicated than it sounds, e.g. generated Scala classes are final)

The bottom line is that users who want their models validated at construction time have to write code that could be generated.

Proposed Solution

Adding a new annotation would let users label types which need to be validated at construction time:

@validateConstruction
record JeopardyResponse {
  @validate.regex = {
    "regex": "^(What|Who) is"
  }
  question: string
}

Courier would then generate constructor code that calls the validator function and signals failure in an idiomatic way. For example, in Java and Scala, new JeopardyResponse(question = "Why is the sky blue") would throw an IllegalArgumentException.

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