Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where keyNode is nil #29

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/losisin/helm-values-schema-json
rev: v1.0.0
rev: v1.1.0
hooks:
- id: helm-schema
args:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2023 Aleksandar Stojanov
Copyright (c) 2023 - 2024 Aleksandar Stojanov

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ First [install pre-commit](https://pre-commit.com/#install) and then create or u
```yaml
repos:
- repo: https://github.com/losisin/helm-values-schema-json
rev: v1.0.0
rev: v1.1.0
hooks:
- id: helm-schema
args: ["-input", "values.yaml"]
Expand Down
36 changes: 36 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,42 @@ replicaCount: # @schema type:[integer, null]
}
```

Another way to use this is to define type when using anchors and aliases in yaml. See discussion [#28](https://github.com/losisin/helm-values-schema-json/issues/28) for more details.

```yaml
app: &app
settings:
namespace:
- *app # @schema type:[string]
```

```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"app": {
"properties": {
"settings": {
"properties": {
"namespace": {
"items": {
"type": [
"string"
]
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
```

### Enum

Always returns array of strings. Special case is `null` where instead of string, it is treated as valid inpput type. [section 6.1.2](https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1.2)
Expand Down
16 changes: 8 additions & 8 deletions pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ func getSchemaURL(draft int) (string, error) {
}

func getComment(keyNode *yaml.Node, valNode *yaml.Node) string {
if len(valNode.Value) > 0 {
if valNode.LineComment != "" {
return valNode.LineComment
}
return keyNode.LineComment
if keyNode != nil {
return keyNode.LineComment
}
return ""
}

func processList(comment string, stringsOnly bool) []interface{} {
Expand Down Expand Up @@ -102,8 +105,6 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
value := strings.TrimSpace(keyValue[1])

switch key {
case "type":
schema.Type = processList(value, true)
case "enum":
schema.Enum = processList(value, false)
case "multipleOf":
Expand Down Expand Up @@ -154,6 +155,8 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
if strings.TrimSpace(value) == "true" {
isRequired = strings.TrimSpace(value) == "true"
}
case "type":
schema.Type = processList(value, true)
}
}
}
Expand Down Expand Up @@ -200,10 +203,7 @@ func parseNode(keyNode *yaml.Node, valNode *yaml.Node) (*Schema, bool) {
}
}

propIsRequired := false
if keyNode != nil {
propIsRequired = processComment(schema, getComment(keyNode, valNode))
}
propIsRequired := processComment(schema, getComment(keyNode, valNode))

return schema, propIsRequired
}
2 changes: 1 addition & 1 deletion pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestGetComment(t *testing.T) {
Value: "some value",
LineComment: "",
},
expectedComment: "",
expectedComment: "# Key comment",
},
{
name: "empty value node, key node with comment",
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "schema"
version: "1.0.0"
version: "1.1.0"
usage: "generate values.schema.json from values yaml"
description: "Helm plugin for generating values.schema.json from multiple values files."
ignoreFlags: false
Expand Down
24 changes: 24 additions & 0 deletions testdata/anchors.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"app": {
"properties": {
"settings": {
"properties": {
"namespace": {
"items": {
"type": [
"string"
]
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
},
"type": "object"
}
4 changes: 4 additions & 0 deletions testdata/anchors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
app: &app
settings:
namespace:
- *app # @schema type:[string]