---
title: "Test a YAML value before replacing it in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/conditionally-patch-yaml/
description: "Use a JSON Patch test before replace to update YAML in Go only if the existing value matches. Handle a failed test without changing the document."
version: "1.0.0"
---

# Test a YAML value before replacing 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/conditionally-patch-yaml/conditionally-patch-yaml.zip)

Use `PatchTest` before `PatchReplace` in the same call.

```shell
go mod init demo
go get oneyaml.dev/go
```

Requires Go 1.24 or later.

```go
package main
import (
  "fmt"
  yaml "oneyaml.dev/go"
)
func main() {
  src := []byte("replicas: 2 # scale\n")
  doc, err := yaml.Parse(src)
  if err != nil { panic(err) }
  err = doc.Apply(
    yaml.PatchTest("/replicas", 2),
    yaml.PatchReplace("/replicas", 3),
  )
  if err != nil { panic(err) }
  fmt.Print(doc)
}
```

`go run .` prints:

```text
replicas: 3 # scale
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/conditionally-patch-yaml/conditionally-patch-yaml.zip), then `go run .`.

The test matches the current count of `2`, so the replacement runs.

If the scalar has changed, the call returns an error matching `ErrTestFailed` and leaves the document unchanged.

## Variations

**Apply a JSON patch supplied as text.** Replace the `doc.Apply` call with:

```go
err = doc.ApplyPatch([]byte(`[
  {"op":"test","path":"/replicas","value":2},
  {"op":"replace","path":"/replicas","value":3}
]`))
```

It prints the same updated YAML.

**Handle a failed test.** In the program above, add `"errors"` to the imports and change `replicas: 2` to `replicas: 4` in `src`. Leave the expected value at `2` and replace the error check after `doc.Apply` with:

```go
if errors.Is(err, yaml.ErrTestFailed) {
  fmt.Print("Test failed; unchanged: ", doc)
  return
}
if err != nil { panic(err) }
```

It prints:

```text
Test failed; unchanged: replicas: 4 # scale
```

## Related recipes

- [Patch YAML in Go](https://recursiveai.org/oneyaml/go/cookbook/patch-yaml/)
- [Edit YAML and keep comments](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/)

## The short answer

Pass `PatchTest` and `PatchReplace` to the same `Apply` call, in that order. The test names the value you expect; the replacement names the value you want. Each call is all-or-nothing. A failing call doesn’t undo edits from an earlier call.

## What does the JSON Patch test operation compare?

A `test` reads the value at a path without changing it. The path is a [JSON Pointer](https://www.rfc-editor.org/rfc/rfc6901), so `/replicas` points to the `replicas` key, and `/deployment/replicas` points to a key inside `deployment`. Comments and whitespace aren’t part of the compared value.

When the document is YAML, the comparison uses YAML’s data types, not the printed text or JSON’s rules for comparing numbers. For example, the YAML integer `0x1F` matches decimal `31`, but a YAML integer and a float with the same magnitude are different, as are the number `2` and the string `"2"`. Match the type in the existing YAML, not just the number or text you see on the screen.

## When should I use a JSON Patch test before replace?

Use it when your update is valid only from a particular starting value, such as a release script that should scale from two replicas to three but must leave a count of four alone. A missing path is an error too; a test isn’t a way to supply a default for that path. Keep all edits that depend on the test in the same patch so they either all apply or none do.

The test checks the document you parsed in memory. It doesn’t reread a file from disk before you save it. If another process can edit the same file between your read and write, handle that coordination separately.
