# Redact PII

> Redacts Personally Identifiable Information (PII) from text or JSON objects to ensure data privacy and compliance.

Source: https://docs.sommatic.ai/en/nodes/pii.redact/

- Slug: `pii.redact`
- Family: Identity
- Version: 1.0.0
- Status: Available
- Execution: sync

Redacts Personally Identifiable Information (PII) from text or JSON objects to ensure data privacy and compliance.

## Configuration

- `input` (object, required): Text or JSON Object to redact
- `strategy` (string, optional): Default: `mask`. Allowed values: `mask`, `remove`.
- `rules` (array, optional): Regex or Predefined Types Default: `["email","credit_card"]`.

## Output ports

- `out` (default): Clean data
- `error`: Processing failed

## Input contract

- `input` (object, required)
- `strategy` (string, optional)
- `rules` (array, optional)

## Output contract

- `result` (object, optional): Redacted content

## In expressions

What a later node can read from this node's output with `$("Redact PII").item.json.<path>` (write your node's label instead of `Redact PII`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Redact PII").item.json.result` (object) = `{…}`: Redacted content
- `$("Redact PII").item.json.result.customer` (string) = `"Jane"` (Not suggested by the editor)
- `$("Redact PII").item.json.result.email` (string) = `"****************"` (Not suggested by the editor)
- `$("Redact PII").item.json.result.card` (string) = `"*******************"` (Not suggested by the editor)
- `$("Redact PII").item.json.result.note` (string) = `"call 555-123-4567"` (Not suggested by the editor)


## How it works

Masks or removes PII in the node input item with regular expressions: email addresses and card numbers by default, plus US-style SSN and phone patterns or your own patterns when listed in `rules`.

1. Takes the node input item (the previous node output) as the content to redact. The `input` field configured on the node is not used.
2. Serializes the item to a JSON string.
3. Applies each entry of `rules` in order: a preset name (`email`, `credit_card`, `ssn`, `phone`) or, otherwise, the text compiled as a regular expression with the `g` flag. Invalid patterns are skipped without notice.
4. Replaces every match with `*` repeated to the match length (`strategy: "mask"`, the default) or with nothing (`"remove"`).
5. Parses the string back to JSON and returns it under `result` on `out`.

## In a workflow

### Node

```json
{
  "node_id": "node-redact-ticket",
  "label": "Redact ticket PII",
  "operator_slug": "pii.redact",
  "operator_version": "1.0.0",
  "config": {
    "input": "{{ $json }}",
    "strategy": "mask",
    "rules": [
      "email",
      "credit_card",
      "phone"
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "result": {
      "customer": "Jane",
      "email": "****************",
      "card": "*******************",
      "note": "call ************"
    }
  }
}
```

## Output ports (behaviour in the code)

### `out`

The item was redacted and parsed back.

```json
{
  "result": {
    "customer": "Jane",
    "email": "****************",
    "card": "*******************",
    "note": "call 555-123-4567"
  }
}
```

### `error`

`rules` is not an array, or a replacement broke the serialized JSON.

```json
{
  "error": "rules.forEach is not a function"
}
```

## Behaviour in the code

- `input`: Ignored: the runtime replaces it with the node input item. To redact only part of the data, shape the item first with a previous node such as `data.transform`.
- `rules`: Default `["email", "credit_card"]`: `ssn` and `phone` apply only when listed. Presets: `email` (local@domain.tld), `credit_card` (16 digits in four groups of four, optionally separated by `-` or a space), `ssn` (`123-45-6789`), `phone` (10 digits as `555-123-4567`, `555.123.4567` or `5551234567`; no country code, parentheses or spaces). Patterns check format only, with no checksum. Custom patterns are case-sensitive and run over the serialized JSON, so they also match object keys.
- `strategy`: Any value other than `remove` masks. Masking keeps the original length; `remove` leaves an empty string in place of the match.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `rules.forEach is not a function` | `error` | `rules` is a string or an object. Pass an array, for example `["email", "phone"]`. |
| `Unexpected token '*', "<serialized item>" is not valid JSON` | `error` | A pattern matched a JSON number (for example a phone stored as `5551234567`) or JSON syntax (quotes, braces, colons), so the text no longer parses. Store such values as strings or anchor custom patterns to value content. The message includes a fragment of the serialized item. |

## Notes and limits

- Only the listed patterns are redacted: names, addresses, national ids in other formats and 15-digit card numbers pass through unchanged.
- Leaving through `error` does not fail the run: the engine follows the edges wired to `error`, and if there are none the branch simply ends.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "pii.redact",
  "name": "Redact PII",
  "category": "identity",
  "version": "1.0.0",
  "description": "Redacts Personally Identifiable Information (PII) from text or JSON objects to ensure data privacy and compliance.",
  "capabilities": [
    "privacy",
    "dlp"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "input": {
        "type": "object",
        "description": "Text or JSON Object to redact"
      },
      "strategy": {
        "type": "string",
        "enum": [
          "mask",
          "remove"
        ],
        "default": "mask"
      },
      "rules": {
        "type": "array",
        "default": [
          "email",
          "credit_card"
        ],
        "description": "Regex or Predefined Types"
      }
    },
    "required": [
      "input"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "input": {
        "type": "object"
      },
      "strategy": {
        "type": "string"
      },
      "rules": {
        "type": "array"
      }
    },
    "required": [
      "input"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "Redacted content"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Redacted",
      "description": "Clean data",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Processing failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard"
  ],
  "tags": [
    "pii",
    "compliance"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_veripass",
    "profile": {
      "name": "Veripass",
      "verified": true,
      "url": "https://me.veripass.com/veripass",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "input": {
      "type": "object",
      "description": "Text or JSON Object to redact"
    },
    "strategy": {
      "type": "string",
      "enum": [
        "mask",
        "remove"
      ],
      "default": "mask"
    },
    "rules": {
      "type": "array",
      "default": [
        "email",
        "credit_card"
      ],
      "description": "Regex or Predefined Types"
    }
  },
  "required": [
    "input"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "input": {
      "type": "object"
    },
    "strategy": {
      "type": "string"
    },
    "rules": {
      "type": "array"
    }
  },
  "required": [
    "input"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "Redacted content"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Redacted",
    "description": "Clean data",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Processing failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
