typespec icon indicating copy to clipboard operation
typespec copied to clipboard

Decorator @minValue, @maxValue to support scalar Duration

Open abatishchev opened this issue 2 years ago • 1 comments

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.

abatishchev avatar May 10 '24 15:05 abatishchev

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:

  1. Do nothing. Our client codegen does not do client-side validation. So adding this doesn't add any value to the current scenario.

  2. Extend current @minValue and @maxValue to apply to a duration property and let user to specify and interpret the base for that number. For Azure, we can specify a base such as second for 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

  3. Add @durationMinValue and @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.

  1. 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

allenjzhang avatar Jul 26 '24 18:07 allenjzhang