Skip to content

Commit

Permalink
Merge pull request #35 from losisin/feature/add-title
Browse files Browse the repository at this point in the history
add title field to schema from comments
  • Loading branch information
losisin authored Feb 20, 2024
2 parents 4d87b13 + 0ded440 commit eed8a2c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
fetch-depth: 0
- name: Set Helm
uses: azure/setup-helm@v3
uses: azure/setup-helm@v4.0.0
with:
version: v3.12.1
- name: Set up Go
Expand All @@ -37,6 +37,6 @@ jobs:
- name: Run plugin
run: helm schema -help
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v3
with:
flags: unittests
19 changes: 19 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The following annotations are supported:
* [minProperties](#minproperties)
* [maxProperties](#maxproperties)
* [required](#required)
* [Meta-Data Annotations](#meta-data-annotations)
* [title](#title)

## Validation Keywords for Any Instance Type

Expand Down Expand Up @@ -364,3 +366,20 @@ image:
"type": "object"
}
```

## Meta-Data Annotations

### title

String. [section 9.1](https://json-schema.org/draft/2020-12/json-schema-validation#section-9.1)

```yaml
fullnameOverride: bar # @schema title: My title
```
```json
"fullnameOverride": {
"title": "My title",
"type": "string"
},
```
6 changes: 6 additions & 0 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func mergeSchemas(dest, src *Schema) *Schema {
if src.MinProperties != nil {
dest.MinProperties = src.MinProperties
}
if src.Title != "" {
dest.Title = src.Title
}

// Merge 'enum' field (assuming that maintaining order doesn't matter)
dest.Enum = append(dest.Enum, src.Enum...)
Expand Down Expand Up @@ -137,6 +140,9 @@ func convertSchemaToMapRec(schema *Schema, visited map[uintptr]bool) (map[string
if schema.MinProperties != nil {
schemaMap["minProperties"] = *schema.MinProperties
}
if schema.Title != "" {
schemaMap["title"] = schema.Title
}

// Arrays
if len(schema.Required) > 0 {
Expand Down
10 changes: 6 additions & 4 deletions pkg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func schemasEqual(a, b *Schema) bool {
return a == b
}
// Compare simple fields
if a.Type != b.Type || a.Pattern != b.Pattern || a.UniqueItems != b.UniqueItems {
if a.Type != b.Type || a.Pattern != b.Pattern || a.UniqueItems != b.UniqueItems || a.Title != b.Title {
return false
}
// Compare pointer fields
Expand Down Expand Up @@ -138,9 +138,9 @@ func TestMergeSchemas(t *testing.T) {
},
{
name: "string properties",
dest: &Schema{Type: "string", Pattern: "^abc", MinLength: uint64Ptr(1), MaxLength: uint64Ptr(10)},
src: &Schema{Type: "string", Pattern: "^abc", MinLength: uint64Ptr(1), MaxLength: uint64Ptr(10)},
want: &Schema{Type: "string", Pattern: "^abc", MinLength: uint64Ptr(1), MaxLength: uint64Ptr(10)},
dest: &Schema{Type: "string", Pattern: "^abc", Title: "My Title", MinLength: uint64Ptr(1), MaxLength: uint64Ptr(10)},
src: &Schema{Type: "string", Pattern: "^abc", Title: "My Title", MinLength: uint64Ptr(1), MaxLength: uint64Ptr(10)},
want: &Schema{Type: "string", Pattern: "^abc", Title: "My Title", MinLength: uint64Ptr(1), MaxLength: uint64Ptr(10)},
},
{
name: "array properties",
Expand Down Expand Up @@ -232,6 +232,7 @@ func TestConvertSchemaToMap(t *testing.T) {
Maximum: float64Ptr(10),
Minimum: float64Ptr(1),
Pattern: "^abc",
Title: "My Title",
Enum: []interface{}{1, 2, 3},
},
want: map[string]interface{}{
Expand All @@ -240,6 +241,7 @@ func TestConvertSchemaToMap(t *testing.T) {
"maximum": 10.0,
"minimum": 1.0,
"pattern": "^abc",
"title": "My Title",
"enum": []interface{}{1, 2, 3},
},
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Schema struct {
Type interface{} `json:"type,omitempty"`
Enum []any `json:"enum,omitempty"`
Title string `json:"title,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Expand Down Expand Up @@ -157,6 +158,8 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
}
case "type":
schema.Type = processList(value, true)
case "title":
schema.Title = value
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ func TestProcessComment(t *testing.T) {
{
name: "Set string",
schema: &Schema{},
comment: "# @schema pattern:^abv$;minLength:2;maxLength:10",
expectedSchema: &Schema{Pattern: "^abv$", MinLength: uint64Ptr(2), MaxLength: uint64Ptr(10)},
comment: "# @schema pattern:^abv$;title:My Title;minLength:2;maxLength:10",
expectedSchema: &Schema{Pattern: "^abv$", Title: "My Title", MinLength: uint64Ptr(2), MaxLength: uint64Ptr(10)},
expectedRequired: false,
},
{
Expand All @@ -308,18 +308,6 @@ func TestProcessComment(t *testing.T) {
expectedSchema: &Schema{MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
expectedRequired: false,
},
// {
// name: "Set required",
// schema: &Schema{
// Type: "object",
// Properties: map[string]*Schema{
// "shared": {Type: "string"},
// },
// },
// comment: "# @schema required:true",
// expectedSchema: &Schema{Required: []string{"shared"}},
// expectedRequired: true,
// },
}

for _, tt := range tests {
Expand Down
3 changes: 1 addition & 2 deletions testdata/full.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@
]
},
"fullnameOverride": {
"maxLength": 10,
"minLength": 1,
"pattern": "^[a-z]$",
"title": "My title",
"type": "string"
},
"image": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ empty: # @schema type: [string, null]
replicas: 2 # @schema minimum: 1 ; maximum: 10 ; multipleOf: 2

# Strings
fullnameOverride: bar # @schema pattern: ^[a-z]$ ; minLength: 1 ; maxLength: 10
fullnameOverride: bar # @schema pattern: ^[a-z]$ ; title: My title

# Arrays
tolerations: # @schema minItems: 1 ; maxItems: 10 ; uniqueItems: true
Expand Down

0 comments on commit eed8a2c

Please sign in to comment.