---
title: "Test a YAML value before patching it in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/compare-yaml-values-in-a-patch-test/
description: "Compare a YAML value before applying a patch in Go. See why hexadecimal 0x1F matches decimal 31, but 31.0 fails the same test."
version: "1.0.0"
---

# Test a YAML value before patching it in Go

**oneyaml-go** is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/compare-yaml-values-in-a-patch-test/compare-yaml-values-in-a-patch-test.zip)

Put `PatchTest` before `PatchReplace` in the same call: they’re the `test` and `replace` operations from JSON Patch (RFC 6902). A YAML integer written as `0x1F` matches decimal `31`; the test checks the value and type, not the spelling.

```shell
go get oneyaml.dev/go@v1.0.0
```

Go 1.24+.

```go
package main

import (
	"fmt"

	yaml "oneyaml.dev/go"
)

func main() {
	input := "level: 0x1F # current\n"
	doc, err := yaml.Parse([]byte(input))
	if err != nil {
		panic(err)
	}
	err = doc.Apply(
		yaml.PatchTest("/level", 31),
		yaml.PatchReplace("/level", "high"),
	)
	if err != nil {
		panic(err)
	}
	fmt.Print(doc)
}
```

Paste the code above into main.go, run `go mod init example` and `go get oneyaml.dev/go@v1.0.0`, then `go run .`; or unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/compare-yaml-values-in-a-patch-test/compare-yaml-values-in-a-patch-test.zip) and `go run .`.

`go run .` prints:

```text
level: high # current
```

If the value doesn’t match, `Apply` returns an error matching `ErrTestFailed` and leaves the document unchanged.

## Variations

**Check a float against the integer.** Add `"errors"` to the imports and replace the code from `doc.Apply` onward:

```go
	err = doc.Apply(
		yaml.PatchTest("/level", 31.0),
		yaml.PatchReplace("/level", "high"),
	)
	if errors.Is(err, yaml.ErrTestFailed) {
		fmt.Print("No match: ", doc)
		return
	}
	if err != nil {
		panic(err)
	}
	fmt.Print(doc)
```

It prints:

```text
No match: level: 0x1F # current
```

## Related recipes

- [Patch YAML in Go](https://recursiveai.org/oneyaml/go/cookbook/patch-yaml/)
- [Handle a failed YAML patch test](https://recursiveai.org/oneyaml/go/cookbook/conditionally-patch-yaml/)

## The short answer

Call `PatchTest` with the expected current value and `PatchReplace` with the new value in the same `Apply` call. The test can distinguish an integer, a float, and a string even when all three look like the same number. A failed test rejects the entire patch.

## How do I check if a YAML value is what I expect?

Give the test a [JSON Pointer](https://www.rfc-editor.org/rfc/rfc6901) to the value. Here `/level` selects the top-level `level` key. The test checks that one value; it doesn’t validate a schema or check the rest of the file. If you have several conditions, put their tests in the same call before the edits that depend on them. You can then tell whether the group applied by checking the error from that call.

Comparisons use YAML’s Core schema. Comparing the source line as text would reject `0x1F` when you expect `31`, even though the document holds the same integer. The patch test accepts that pair and ignores the `# current` comment. Quoting a plain number is different: `"31"` is a string, not another spelling of the integer.

## Why does a JSON Patch test fail for the same number?

In a patch applied to YAML, an integer and a float are different types. `31.0` is a Go floating-point constant when passed to `PatchTest` in the example. It doesn’t match the YAML integer. The rule also applies to numeric literals in patch text: YAML comparison doesn’t adopt [RFC 6902’s JSON number equality](https://www.rfc-editor.org/rfc/rfc6902#section-4.6) just because you wrote the operations as JSON. Match the number’s type as well as its magnitude.

## How do I set a YAML value only if the old value matches?

Place the test before the edit so it reads the old value, not the value you just wrote. If an operation fails, even operations earlier in that same call are rolled back. Separate `Apply` calls aren’t part of that rollback. The patch checks the parsed document in memory; it doesn’t check for a newer version on disk. If multiple writers can change the same file, coordinate the read and write outside the patch.
