Decorator @minValue, @maxValue to support scalar Duration
Clear and concise description of the problem
Context: ARM guidance instructs Resource Providers to expose duration in the ISO 8601#Durations, e.g.:
| Human readable | System.Timespan | Duration |
|---|---|---|
| 1 minute | 00:00:01 | PT1M |
| 1 hour 15 minutes 30 seconds | 01:15:30 | PT1H15M30S |
Here's my TypeSpec model where I have a property of type scalar duration but which is missing the Seagger-side validation for the min and max values:
@doc("Threshold properties")
model ThresholdProperties {
@doc("Window duration")
//@minValue("?")
//@maxValue("?")
windowDuration: duration;
}
Proposal: update the existing decorators minValue and maxValue to support more scalars, such as duration, e.g.:
@minValue("PT1M")
@maxValue("PT1H")
windowDuration: duration;
Checklist
- [X] Follow our Code of Conduct
- [X] Read the docs.
- [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
OpenAPI's minimum, maximum are numeric values. While TypeSpec is richer than OpenAPI, we need to figure out the unit translations from string such as "PT1M" to a numeric value. There will be information loss when generated to OpenAPI.
Proposals:
-
Do nothing. Our client codegen does not do client-side validation. So adding this doesn't add any value to the current scenario.
-
Extend current
@minValueand@maxValueto apply to adurationproperty and let user to specify and interpret the base for that number. For Azure, we can specify a base such assecondfor the value.
Affects: 1P & 3P Pro: Minimal change, Flexible Con: User need to do unit translation, Unclear intension to codegen if validation needs to be generated -
Add
@durationMinValueand@durationMaxValue.
extern dec durationMinValue(target: ModelProperty, minValue: valueof string, encodingUnit: "h" | "m" | "s" | "ms" = "s");
model Foo {
@durationMinValue("PT1M", "s")
prop: duration;
}
=>
properties:
a:
type: string
format: duration
minimum: 60
Affects: 1P & 3P Pro: Minimal impact, Precise Con: Same information loss in OpenAPI.
- Add #2 only in Azure.Core to limit it to 1P. When the RPaaS service codegen is back up, this information can be used to provide precise validation in service code. Affects: 1P & 3P Pro: Limited usage & impact, Precise definition Con: Azure only, Codegen needs specificalization