# Redactions (/docs/redactions)



Overview [#overview]

Redactions **replace** node values (rather than removing them) after filters have been applied.
They are useful for masking volatile or sensitive data — dates, tokens, IDs — with stable
placeholder strings.

```ts
import { ReplacedRedaction, SortedRedaction } from "vitest-snap";

await expect(data).toJsonSnapshot({
  redactions: [
    new ReplacedRedaction(".apiKey", "[REDACTED]"),
    new SortedRedaction(".tags"),
  ],
});
```

***

ValueRedaction [#valueredaction]

Replaces any node where a predicate function returns `true`.

<Callout>
  **Applied by default** for `Date` instances: every `Date` is replaced with
  `[Date_1]`, `[Date_2]`, … Pass a custom `redactions` array to override.
</Callout>

Constructor [#constructor]

```ts
new ValueRedaction(
  predicate: (path: string, value: unknown) => boolean,
  replaced:  string,
  counting?: boolean,
)
```

| Parameter   | Type                       | Default | Description                                                       |
| ----------- | -------------------------- | ------- | ----------------------------------------------------------------- |
| `predicate` | `(path, value) => boolean` | —       | Returns `true` for nodes to redact                                |
| `replaced`  | `string`                   | —       | Replacement string, e.g. `"[Secret]"`                             |
| `counting`  | `boolean`                  | `true`  | When `true`, appends `_1`, `_2`, … to make each occurrence unique |

Example — redact all strings longer than 100 characters [#example--redact-all-strings-longer-than-100-characters]

```ts
import { ValueRedaction } from "vitest-snap";

new ValueRedaction(
  (_, v) => typeof v === "string" && v.length > 100,
  "[LONG_STRING]",
);
```

Example — redact specific primitive value [#example--redact-specific-primitive-value]

```ts
new ValueRedaction((_, v) => v === "supersecret", "[REDACTED]", false);
```

Default Date redaction [#default-date-redaction]

The built-in default is equivalent to:

```ts
new ValueRedaction((_, v) => v instanceof Date, "[Date]");
// produces [Date_1], [Date_2], … for each Date encountered
```

***

ReplacedRedaction [#replacedredaction]

Replaces all nodes matched by a [selector](/docs/selectors) with a
fixed placeholder string.

Constructor [#constructor-1]

```ts
new ReplacedRedaction(
  selector: Selector<T>,
  replaced: string,
  counting?: boolean,
)
```

| Parameter  | Type          | Default | Description                                      |
| ---------- | ------------- | ------- | ------------------------------------------------ |
| `selector` | `Selector<T>` | —       | Path expression targeting nodes to redact        |
| `replaced` | `string`      | —       | Replacement string                               |
| `counting` | `boolean`     | `true`  | Appends `_1`, `_2`, … per occurrence when `true` |

Example — mask a top-level field [#example--mask-a-top-level-field]

```ts
import { ReplacedRedaction } from "vitest-snap";

new ReplacedRedaction(".password", "[REDACTED]");
```

Example — mask every token in an array [#example--mask-every-token-in-an-array]

```ts
new ReplacedRedaction(".sessions[].token", "[TOKEN]");
```

Example — fixed placeholder without counter [#example--fixed-placeholder-without-counter]

```ts
new ReplacedRedaction(".id", "[ID]", false);
// all .id values become "[ID]" (no _1, _2 suffix)
```

***

SortedRedaction [#sortedredaction]

Sorts the array at the matched path in-place (ascending, using default JS comparison).
Useful for stabilizing snapshot output when the order of array elements is non-deterministic.

Constructor [#constructor-2]

```ts
new SortedRedaction(selector: Selector<T>)
```

| Parameter  | Type          | Description                                |
| ---------- | ------------- | ------------------------------------------ |
| `selector` | `Selector<T>` | Path expression targeting an array to sort |

Example — sort a top-level array of strings [#example--sort-a-top-level-array-of-strings]

```ts
import { SortedRedaction } from "vitest-snap";

const redaction = new SortedRedaction(".tags");

redaction.apply({ tags: ["banana", "apple", "cherry"] });
// → { tags: ["apple", "banana", "cherry"] }
```

Example — sort nested arrays [#example--sort-nested-arrays]

```ts
new SortedRedaction(".users[].roles");
```

Usage in snapshot [#usage-in-snapshot]

```ts
await expect(result).toJsonSnapshot({
  redactions: [new SortedRedaction(".items")],
});
```
