Skip to content

Commit

Permalink
add readOnly field to schema from comments
Browse files Browse the repository at this point in the history
closes [#32](#32)

Signed-off-by: Aleksandar Stojanov <[email protected]>
  • Loading branch information
losisin committed Feb 21, 2024
1 parent eed8a2c commit 5b70ab5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
19 changes: 19 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,22 @@ fullnameOverride: bar # @schema title: My title
"type": "string"
},
```

### readOnly

```yaml
image:
tag: latest # @schema readOnly: true
```
```json
"image": {
"properties": {
"tag": {
"readOnly": true,
"type": "string"
}
},
"type": "object"
}
```
6 changes: 6 additions & 0 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func mergeSchemas(dest, src *Schema) *Schema {
if src.Title != "" {
dest.Title = src.Title
}
if src.ReadOnly {
dest.ReadOnly = src.ReadOnly
}

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

// Arrays
if len(schema.Required) > 0 {
Expand Down
16 changes: 12 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 || a.Title != b.Title {
if a.Type != b.Type || a.Pattern != b.Pattern || a.UniqueItems != b.UniqueItems || a.Title != b.Title || a.ReadOnly != b.ReadOnly {
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", 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)},
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)},
},
{
name: "array properties",
Expand All @@ -154,6 +154,12 @@ func TestMergeSchemas(t *testing.T) {
src: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
want: &Schema{Type: "object", MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
},
{
name: "meta-data properties",
dest: &Schema{Type: "object", Title: "My Title", ReadOnly: true},
src: &Schema{Type: "object", Title: "My Title", ReadOnly: true},
want: &Schema{Type: "object", Title: "My Title", ReadOnly: true},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -211,6 +217,7 @@ func TestConvertSchemaToMap(t *testing.T) {
MinItems: uint64Ptr(1),
MaxItems: uint64Ptr(2),
UniqueItems: true,
ReadOnly: true,
},
want: map[string]interface{}{
"type": "array",
Expand All @@ -222,6 +229,7 @@ func TestConvertSchemaToMap(t *testing.T) {
"minItems": uint64(1),
"maxItems": uint64(2),
"uniqueItems": true,
"readOnly": true,
},
},
{
Expand Down
7 changes: 6 additions & 1 deletion pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 All @@ -26,6 +25,8 @@ type Schema struct {
Required []string `json:"required,omitempty"`
Items *Schema `json:"items,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
}

func getKind(value string) string {
Expand Down Expand Up @@ -160,6 +161,10 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
schema.Type = processList(value, true)
case "title":
schema.Title = value
case "readOnly":
if v, err := strconv.ParseBool(value); err == nil {
schema.ReadOnly = v
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ func TestProcessComment(t *testing.T) {
{
name: "Set enum",
schema: &Schema{},
comment: "# @schema enum:[one, two, null]",
expectedSchema: &Schema{Enum: []any{"one", "two", nil}},
comment: "# @schema enum:[one, two, null];readOnly:true",
expectedSchema: &Schema{Enum: []any{"one", "two", nil}, ReadOnly: true},
expectedRequired: false,
},
{
Expand Down
1 change: 1 addition & 0 deletions testdata/full.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"type": "string"
},
"tag": {
"readOnly": true,
"type": "string"
}
},
Expand Down
2 changes: 1 addition & 1 deletion testdata/full.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Required
image:
repository: nginx # @schema required: true
tag: latest
tag: latest # @schema readOnly: true
pullPolicy: Always
nameOverride: foo # @schema required: true

Expand Down

0 comments on commit 5b70ab5

Please sign in to comment.