diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b64d278..51daba5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: diff --git a/LICENSE b/LICENSE index 5e942ec..cb9be93 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 793c1fc..37350c8 100644 --- a/README.md +++ b/README.md @@ -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"] diff --git a/docs/README.md b/docs/README.md index 2717e18..777a00e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) diff --git a/pkg/schema.go b/pkg/schema.go index 813e931..c177145 100644 --- a/pkg/schema.go +++ b/pkg/schema.go @@ -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{} { @@ -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": @@ -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) } } } @@ -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 } diff --git a/pkg/schema_test.go b/pkg/schema_test.go index e608a2c..26e7a5c 100644 --- a/pkg/schema_test.go +++ b/pkg/schema_test.go @@ -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", diff --git a/plugin.yaml b/plugin.yaml index e0d8384..0908b87 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -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 diff --git a/testdata/anchors.schema.json b/testdata/anchors.schema.json new file mode 100644 index 0000000..f5313be --- /dev/null +++ b/testdata/anchors.schema.json @@ -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" +} \ No newline at end of file diff --git a/testdata/anchors.yaml b/testdata/anchors.yaml new file mode 100644 index 0000000..f88c39c --- /dev/null +++ b/testdata/anchors.yaml @@ -0,0 +1,4 @@ +app: &app + settings: + namespace: + - *app # @schema type:[string]