microsoft / microsoft/typespec

Add model constraints

Open
#961 7 comments 2 reactions 0 assignees View on GitHub
design:needed
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

A feature that Bosque has that I covet is the ability to describe constraints on the values of a model in order to e.g. generate code to construct a model that ensures that the data inside the model is correct. We have decorators for simple scenarios but there are many cases where the validations are more complex than the decorators can handle.

Imagine syntax like the following:

```
model AccountOwner {
name: string;
age: int8;

ensure age > 18;
ensure name.length > 2;
}

model Account {
balance: int32;
kind: "joint" | "single";
owners: AccountOwner[];
ensure balance > 0;
ensure if (kind == "joint") { owners.length > 1 }
}
```

Syntax wise I think we leverage what we've already done for projections. Semantically, I think these clauses are validated but not run in Cadl. Emitters can access the AST and convert these expressions into target languages.

There are various constraints that don't necessarily apply to projections to keep in mind, i.e. these expressions must be pure in the sense that they should not make modifications to the cadl program at all.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.