microsoft / microsoft/typespec
feat: support enum object types
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
### Clear and concise description of the problem
I have the following type/model definitions:
```typescript
NonEmptyString:
type: string
minLength: 1
pattern: ^\S(?:.*\S)?$
PickValue:
type: object
properties:
text:
$ref: "#/$defs/NonEmptyString"
value:
type: integer
required:
- text
- value
```
I want to derive a type from `PickValue` so that only specific combinations of `text` and `value` values are valid. In a plain JSON Schema I'd create the following definition
```yaml
PartnerRole:
allOf:
- $ref: "#/$defs/PickValue"
- type: object
enum:
- text: "Insurance holder"
value: 805190000
- text: "Facilitator"
value: 805190001
- text: "Tenant"
value: 805190002
- text: "Insurer"
value: 805190003
- text: "Broker"
value: 805190004
- text: "other"
value: 805190008
required:
- text
- value
additionalProperties: false
```
Currently there is no way to express this in TypeSpec. Or I didn't find the right documentation yet.
The only way I found was to specify enum values for each property separately.
```typescript
model PartnerRole extends PickValue {
/*
Insurance holder 805190000
Facilitator 805190001
Tenant 805190002
Insurer 805190003
Broker 805190004
other 805190008
*/
@oneOf
text:
"Insurance holder" |
"Facilitator" |
"Tenant" |
"Insurer" |
"Broker" |
"other";
@oneOf
value:
805190000 |
805190001 |
805190002 |
805190003 |
805190004 |
805190008;
}
```
But that it not the same because with that approach invalid combinations could be created.
### Checklist
- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
Assessment
This issue has not been assessed yet.