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

feat: support yaml extension #2217

Merged
merged 11 commits into from
Jan 10, 2024
20 changes: 0 additions & 20 deletions docs/3-create-a-zarf-package/4-zarf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,26 +441,6 @@ Must be one of:
| **Type** | `object` |
| **Additional properties** | [![Any type: allowed](https://img.shields.io/badge/Any%20type-allowed-green)](# "Additional Properties of any type are allowed.") |

<details>
<summary>
<strong> <a name="build_registryOverrides_pattern1"></a>Pattern Property .*</strong>
</summary>
&nbsp;
<blockquote>

:::note
All properties whose name matches the regular expression
```.*``` ([Test](https://regex101.com/?regex=.%2A))
must respect the following conditions
:::

| | |
| -------- | -------- |
| **Type** | `string` |

</blockquote>
</details>

</blockquote>
</details>

Expand Down
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% for sub_property in schema.iterate_properties %}
{%- if sub_property.is_additional_properties and not sub_property.is_additional_properties_schema -%}
{%- if sub_property.is_additional_properties and not sub_property.is_additional_properties_schema or sub_property.is_pattern_property -%}
{% continue %}
{% endif %}

Expand All @@ -19,7 +19,7 @@
<summary>
{% filter md_heading(1, html_id, True) -%}
{%- filter replace('\n', '') -%}
{%- if sub_property.is_pattern_property %} Pattern Property{% endif %} {% with schema=sub_property %}{%- include "breadcrumbs.md" %} {% endwith %}
{% with schema=sub_property %}{%- include "breadcrumbs.md" %} {% endwith %}
{%- if not skip_required and sub_property.property_name -%}
{{ "*" if sub_property.is_required_property else "" -}}
{%- endif -%}
Expand All @@ -40,13 +40,6 @@
{% endif %}

{% with schema=sub_property, skip_headers=False %}
{% if sub_property.is_pattern_property %}
:::note
All properties whose name matches the regular expression
```{{ sub_property.property_name }}``` ([Test](https://regex101.com/?regex={{ sub_property.property_name | urlencode }}))
must respect the following conditions
:::
{% endif %}
{%- if not skip_required and sub_property.property_name -%}
{{ md_badge("Required", "red", show_text=False) if sub_property.is_required_property else "" -}}
{%- endif -%}
Expand Down
2 changes: 1 addition & 1 deletion hack/.templates/jsonschemaforhumans/tabbed_section.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<blockquote>

{% filter md_heading(2, node.html_id) -%}
{% if node.is_pattern_property %}Pattern{% endif %} Property `{% with schema=node %}{%- include "breadcrumbs.md" %}{% endwith %}`
Property `{% with schema=node %}{%- include "breadcrumbs.md" %}{% endwith %}`
{%- endfilter %}

{% with schema=node, skip_headers=False %}
Expand Down
4 changes: 4 additions & 0 deletions hack/create-zarf-schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
# Create the json schema for the zarf.yaml
go run main.go internal gen-config-schema > zarf.schema.json

# Adds pattern properties to all definitions to allow for yaml extensions
jq '.definitions |= map_values(. + {"patternProperties": {"^x-": {}}})' zarf.schema.json > temp_zarf.schema.json
mv temp_zarf.schema.json zarf.schema.json

# Create docs from the zarf.yaml JSON schema
docker run -v $(pwd):/app -w /app --rm python:3.8-alpine /bin/sh -c "pip install json-schema-for-humans && generate-schema-doc --config-file hack/.templates/jsfh-config.json zarf.schema.json docs/3-create-a-zarf-package/4-zarf-schema.md"
28 changes: 28 additions & 0 deletions src/pkg/packager/lint/lint.go
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,35 @@ func makeFieldPathYqCompat(field string) string {
return fmt.Sprintf(".%s", wrappedField)
}

// removeYamlExtensionFields removes any field in a yaml object that starts with x-
// This is consistent with how docker compose ignores yaml extension fields
// https://docs.docker.com/compose/compose-file/11-extension/
func removeYamlExtensionFields(yamlObj map[string]interface{}) {
for key, value := range yamlObj {
if strings.HasPrefix(key, "x-") {
delete(yamlObj, key)
continue
}

if nestedMap, ok := value.(map[string]interface{}); ok {
removeYamlExtensionFields(nestedMap)
}

if slice, ok := value.([]interface{}); ok {
for _, item := range slice {
if nestedMap, ok := item.(map[string]interface{}); ok {
removeYamlExtensionFields(nestedMap)
}
}
}
}
}

AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
func validateSchema(validator *Validator) error {
if zarfPackageMap, ok := validator.untypedZarfPackage.(map[string]interface{}); ok {
removeYamlExtensionFields(zarfPackageMap)
}

AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
schemaLoader := gojsonschema.NewBytesLoader(validator.jsonSchema)
documentLoader := gojsonschema.NewGoLoader(validator.untypedZarfPackage)

Expand Down
7 changes: 6 additions & 1 deletion src/pkg/packager/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ components:
`

const goodZarfPackage = `
x-name: &name good-zarf-package

kind: ZarfPackageConfig
metadata:
name: good-zarf-package
name: *name
x-description: Testing good yaml with yaml extension

components:
- name: baseline
required: true
x-foo: bar

`

func readAndUnmarshalYaml[T interface{}](t *testing.T, yamlString string) T {
Expand Down
Loading
Loading