carvel-dev / carvel-dev/ytt

Provide error on duplicate YAML keys in strict mode?

Open
#881 3 comments 0 reactions 1 assignee Claimed by @prembhaskal View on GitHub
enhancement priority/unprioritized-backlog
Dominant language
Go
Stars
1.9k
Forks
167
PR merge metrics
No merged PRs in 30d

Description

**Describe the problem/challenge you have**

It's easy to introduce bugs into YAML due to duplicative keys. For example:

```yaml
key: value1
key: value2
```

Will evaluate to:

```yaml
key: value2
```

I realize YAML allows duplicate keys and this evaluation makes sense, but in large YAML documents, it can be easy to overlook duplicate keys that are more spread out in the document, which can lead to bugs (since it may not be immediately obvious that some value will get overwritten later on in the document).

**Describe the solution you'd like**

I was wondering if YTT could perhaps help guard against this type of situation, since it seems like duplicative keys are usually a mistake within a single YAML document. In particular, I was wondering if this might be a good fit for [ytt's "strict" mode](https://carvel.dev/ytt/docs/develop/strict/), since its stated goal is: "tries to remove any kind of ambiguity in user’s intent when parsing YAML." Duplicative keys seem like they fit this bill, so I was wondering if you all think this would be a useful addition to strict mode.

Currently strict mode doesn't care about duplicate keys, and just uses the last value:

```sh
printf "key: value1\nkey: value2" | ytt -f- --strict
key: value2
```

But I might envision strict mode behaving something like this:

```
printf "key: value1\nkey: value2" | ytt -f- --strict
ytt: Error: Unmarshaling YAML template 'stdin.yml': yaml:
Strict parsing:
Found duplicate key 'key'
```

But non-strict mode could continue to work as-is.

---
Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

👍 "I would like to see this addressed as soon as possible"
👎 "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.