Skip to content

Add support for construction-time validation #40

@mkovacs-coursera

Description

@mkovacs-coursera

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions