Skip to content

Commit

Permalink
Merge pull request #40 from losisin/feature/add-default-filed
Browse files Browse the repository at this point in the history
add default keyword to schema annotations
  • Loading branch information
losisin authored Feb 26, 2024
2 parents e357b63 + 90e6bd1 commit 0c054b0
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
24 changes: 24 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The following annotations are supported:
* [required](#required)
* [Meta-Data Annotations](#meta-data-annotations)
* [title](#title)
* [default](#default)
* [readOnly](#readonly)

## Validation Keywords for Any Instance Type

Expand Down Expand Up @@ -384,6 +386,28 @@ fullnameOverride: bar # @schema title: My title
},
```

### default

Any JSON value. [section 9.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-9.2)

```yaml
tolerations: [] # @schema default: [{"key":"foo","operator":"Equal","value":"bar","effect":"NoSchedule"}]
```
```json
"tolerations": {
"default": [
{
"effect": "NoSchedule",
"key": "foo",
"operator": "Equal",
"value": "bar"
}
],
"type": "array"
}
```

### readOnly

Boolean. [section 9.4](https://json-schema.org/draft/2020-12/json-schema-validation#section-9.4)
Expand Down
6 changes: 6 additions & 0 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func mergeSchemas(dest, src *Schema) *Schema {
if src.ReadOnly {
dest.ReadOnly = src.ReadOnly
}
if src.Default != nil {
dest.Default = src.Default
}

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

// Arrays
if len(schema.Required) > 0 {
Expand Down
8 changes: 5 additions & 3 deletions pkg/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ func TestMergeSchemas(t *testing.T) {
},
{
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},
dest: &Schema{Type: "object", Title: "My Title", ReadOnly: true, Default: "default value"},
src: &Schema{Type: "object", Title: "My Title", ReadOnly: true, Default: "default value"},
want: &Schema{Type: "object", Title: "My Title", ReadOnly: true, Default: "default value"},
},
}

Expand Down Expand Up @@ -242,6 +242,7 @@ func TestConvertSchemaToMap(t *testing.T) {
Pattern: "^abc",
Title: "My Title",
Enum: []interface{}{1, 2, 3},
Default: "default",
},
want: map[string]interface{}{
"type": "integer",
Expand All @@ -251,6 +252,7 @@ func TestConvertSchemaToMap(t *testing.T) {
"pattern": "^abc",
"title": "My Title",
"enum": []interface{}{1, 2, 3},
"default": "default",
},
},
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"encoding/json"
"errors"
"strconv"
"strings"
Expand All @@ -27,6 +28,7 @@ type Schema struct {
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Default interface{} `json:"default,omitempty"`
}

func getKind(value string) string {
Expand Down Expand Up @@ -165,6 +167,11 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
if v, err := strconv.ParseBool(value); err == nil {
schema.ReadOnly = v
}
case "default":
var jsonObject interface{}
if err := json.Unmarshal([]byte(value), &jsonObject); err == nil {
schema.Default = jsonObject
}
}
}
}
Expand Down
15 changes: 11 additions & 4 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];readOnly:true",
expectedSchema: &Schema{Enum: []any{"one", "two", nil}, ReadOnly: true},
comment: "# @schema enum:[one, two, null]",
expectedSchema: &Schema{Enum: []any{"one", "two", nil}},
expectedRequired: false,
},
{
Expand All @@ -290,8 +290,8 @@ func TestProcessComment(t *testing.T) {
{
name: "Set string",
schema: &Schema{},
comment: "# @schema pattern:^abv$;title:My Title;minLength:2;maxLength:10",
expectedSchema: &Schema{Pattern: "^abv$", Title: "My Title", MinLength: uint64Ptr(2), MaxLength: uint64Ptr(10)},
comment: "# @schema pattern:^abv$;minLength:2;maxLength:10",
expectedSchema: &Schema{Pattern: "^abv$", MinLength: uint64Ptr(2), MaxLength: uint64Ptr(10)},
expectedRequired: false,
},
{
Expand All @@ -308,6 +308,13 @@ func TestProcessComment(t *testing.T) {
expectedSchema: &Schema{MinProperties: uint64Ptr(1), MaxProperties: uint64Ptr(10)},
expectedRequired: false,
},
{
name: "Set meta-data",
schema: &Schema{},
comment: "# @schema title:My Title;readOnly:false;default:\"foo\"",
expectedSchema: &Schema{Title: "My Title", ReadOnly: false, Default: "foo"},
expectedRequired: false,
},
}

for _, tt := range tests {
Expand Down
54 changes: 54 additions & 0 deletions testdata/meta.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"fullnameOverride": {
"title": "Full name override",
"type": "string"
},
"image": {
"properties": {
"repository": {
"default": "nginx",
"type": "string"
},
"tag": {
"readOnly": true,
"type": "string"
}
},
"type": "object"
},
"metrics": {
"properties": {
"enabled": {
"default": true,
"type": "boolean"
}
},
"type": "object"
},
"nodeSelector": {
"default": {
"cloud.google.com/gke-nodepool": "e2-standard-8-spot"
},
"properties": {},
"type": "object"
},
"replicas": {
"default": 2,
"type": "integer"
},
"tolerations": {
"default": [
{
"effect": "NoSchedule",
"key": "foo",
"operator": "Equal",
"value": "bar"
}
],
"type": "array"
}
},
"type": "object"
}
14 changes: 14 additions & 0 deletions testdata/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fullnameOverride: "" # @schema title: Full name override ; default: baz

image:
repository: "" # @schema default: "nginx"
tag: latest # @schema readOnly: true

metrics:
enabled: false # @schema default: true

replicas: 1 # @schema default: 2

tolerations: [] # @schema default: [{"key":"foo","operator":"Equal","value":"bar","effect":"NoSchedule"}]

nodeSelector: {} # @schema default: {"cloud.google.com/gke-nodepool": "e2-standard-8-spot"}

0 comments on commit 0c054b0

Please sign in to comment.