Allow using variables in enum fields (#2199)

## Changes

It is possible to pass variable to enum fields but json-schema doesn't
accept it. This PR adds `oneOf` for enum types that includes `${var-*}`
pattern

## Tests

Manually checked in VSCode
This commit is contained in:
Ilya Kuznetsov 2025-01-22 11:30:17 +01:00 committed by GitHub
parent fde30ff1ab
commit c224be5c1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 663 additions and 278 deletions

View File

@ -40,6 +40,19 @@ func addInterpolationPatterns(typ reflect.Type, s jsonschema.Schema) jsonschema.
}
}
// Allows using variables in enum fields
if s.Type == jsonschema.StringType && s.Enum != nil {
return jsonschema.Schema{
OneOf: []jsonschema.Schema{
s,
{
Type: jsonschema.StringType,
Pattern: interpolationPattern("var"),
},
},
}
}
switch s.Type {
case jsonschema.ArrayType, jsonschema.ObjectType:
// arrays and objects can have complex variable values specified.

View File

@ -13,6 +13,8 @@ variables:
simplevar:
default: true
description: "simplevar description"
schedule_status:
default: "PAUSED"
complexvar:
default:
@ -42,6 +44,8 @@ resources:
dependencies:
- python=3.7
client: "myclient"
trigger:
pause_status: ${var.schedule_status}
tags:
foo: bar
bar: baz

View File

@ -59,8 +59,8 @@ func TestJsonSchema(t *testing.T) {
}
providers := walk(s.Definitions, "github.com", "databricks", "databricks-sdk-go", "service", "jobs.GitProvider")
assert.Contains(t, providers.Enum, "gitHub")
assert.Contains(t, providers.Enum, "bitbucketCloud")
assert.Contains(t, providers.Enum, "gitHubEnterprise")
assert.Contains(t, providers.Enum, "bitbucketServer")
assert.Contains(t, providers.OneOf[0].Enum, "gitHub")
assert.Contains(t, providers.OneOf[0].Enum, "bitbucketCloud")
assert.Contains(t, providers.OneOf[0].Enum, "gitHubEnterprise")
assert.Contains(t, providers.OneOf[0].Enum, "bitbucketServer")
}

File diff suppressed because it is too large Load Diff