Yaml files can be surprisingly lenient in what can be parsed as a yaml file. A reasonable way of validation a yaml file is to ensure the top level is a map or array (although it is valid yaml to have scalars at the top level, but often this is not what you want). This can be done by:
There are a couple of tricks to getting the right string representation, take a look at [string operators](https://mikefarah.gitbook.io/yq/operators/string-operators#string-blocks-bash-and-newlines) for more details:
Powershell has its [own](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about\_quoting\_rules?view=powershell-7.1) way of handling quotes:
To merge all given yaml files into one, use the `reduce` operator with the `*` (multiply) operator. Note the use of `ea` or `eval-all` to load all files into memory so that they can be merged.
```
yq ea '. as $item ireduce ({}; . * $item )' file1.yml file2.yml ...
To see the original source file and line number of your merged result, you can pre-process the files and add that information in as line comments, then perform the merge.
The best way to run a diff is to use `yq` to normalise the yaml files and then just use diff. Here is a simple example of using pretty print `-P` to normalise the styling and running diff:
The most important thing to remember to do is to have brackets around the LHS expression - otherwise what `yq` will do is first filter by the selection, and then, separately, update the filtered result and return that subset.
`yq` has not yet added `if` expressions - however you should be able to use `with` and `select` to achieve the same outcome. Lets use an example:
```yaml
- animal: cat
- animal: dog
- animal: frog
```
Now, if you were using good ol' jq - you may have a script with `if`s like so:
```bash
jq ' .[] |=
if (.animal == "cat") then
.noise = "meow" |
.whiskers = true
elif (.animal == "dog") then
.noise = "woof" |
.happy = true
else
.noise = "??"
end
' <file.yaml
```
Using `yq` - you can get the same result by:
```bash
yq '.[] |= (
with(select(.animal == "cat");
.noise = "meow" |
.whiskers = true
) |
with(select(.animal == "dog");
.noise = "woof" |
.happy = true
) |
with(select(.noise == null);
.noise = "???"
)
)' <file.yml
```
Note that the logic isn't quite the same, as there is no concept of 'else'. So you may need to put additional logic in the expressions, as this has for the 'else' logic.
The merge functionality from yaml v1.1 (e.g. `<<:`has actually been removed in the 1.2 spec. Thankfully, `yq` underlying yaml parser still supports that tag - and it's extra nice in that it explicitly puts the `!!merge` tag on key of the map entry. This tag tells other yaml parsers that this entry is a merge entry, as opposed to a regular string key that happens to have a value of `<<:`. This is backwards compatible with the 1.1 spec of yaml, it's simply an explicit way of specifying the type (for instance, you can use a `!!str` tag to enforce a particular value to be a string.
Although this does affect the readability of the yaml to humans, it still works and processes fine with various yaml processors.