# Redact PII

> 对文本或 JSON 对象中的个人身份信息（PII）进行脱敏，保障数据隐私与合规。

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

- 标识: `pii.redact`
- 类别: 身份
- 版本: 1.0.0
- 状态: 可用
- 执行方式: sync

对文本或 JSON 对象中的个人身份信息（PII）进行脱敏，保障数据隐私与合规。

## 配置

- `input` (object, 必填): 要脱敏的文本或 JSON 对象
- `strategy` (string, 可选): 默认值: `mask`. 可选值: `mask`, `remove`.
- `rules` (array, 可选): 正则表达式或预定义类型 默认值: `["email","credit_card"]`.

## 输出端口

- `out` (默认): 已清洗的数据
- `error`: 处理失败

## 输入契约

- `input` (object, 必填)
- `strategy` (string, 可选)
- `rules` (array, 可选)

## 输出契约

- `result` (object, 可选): 脱敏后的内容

## 在表达式中

后续节点可通过 `$("Redact PII").item.json.<路径>` 读取此节点输出中的内容（请把 `Redact PII` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Redact PII").item.json.result` (object) = `{…}`: 脱敏后的内容
- `$("Redact PII").item.json.result.customer` (string) = `"Jane"` (编辑器不提示)
- `$("Redact PII").item.json.result.email` (string) = `"****************"` (编辑器不提示)
- `$("Redact PII").item.json.result.card` (string) = `"*******************"` (编辑器不提示)
- `$("Redact PII").item.json.result.note` (string) = `"call 555-123-4567"` (编辑器不提示)


## 工作原理

用正则表达式遮盖或删除节点输入项中的个人信息：默认处理电子邮件和银行卡号；在 `rules` 中列出时，还可处理美式 SSN、电话号码格式或自定义模式。

1. 以节点输入项（上一个节点的输出）作为脱敏内容。节点上配置的 `input` 字段不会被使用。
2. 将输入项序列化为 JSON 字符串。
3. 按顺序应用 `rules` 中的每一项：预设名称（`email`、`credit_card`、`ssn`、`phone`），否则把文本编译为带 `g` 标志的正则表达式。无效模式会被静默跳过。
4. 将每个匹配替换为与其等长的 `*`（`strategy: "mask"`，默认），或替换为空（`"remove"`）。
5. 将字符串解析回 JSON，并在 `result` 中从 `out` 返回。

## 在工作流中

### 节点

```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 ************"
    }
  }
}
```

## 输出端口 (代码中的行为)

### `out`

输入项已脱敏并解析回 JSON。

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

### `error`

`rules` 不是数组，或替换破坏了序列化后的 JSON。

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

## 代码中的行为

- `input`: 会被忽略：运行时用节点输入项替换它。若只需脱敏部分数据，请先用前置节点（如 `data.transform`）整理输入项。
- `rules`: 默认值为 `["email", "credit_card"]`：`ssn` 和 `phone` 只有列出时才会应用。预设：`email`（local@domain.tld）、`credit_card`（16 位数字，分为四组，每组四位，可用 `-` 或空格分隔）、`ssn`（`123-45-6789`）、`phone`（10 位数字，如 `555-123-4567`、`555.123.4567` 或 `5551234567`；不含国家代码、括号或空格）。模式只检查格式，不做校验位验证。自定义模式区分大小写，并作用于序列化后的 JSON，因此也会匹配对象的键名。
- `strategy`: 除 `remove` 外的任何值都按遮盖处理。遮盖保留原始长度；`remove` 会在匹配处留下空字符串。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `rules.forEach is not a function` | `error` | `rules` 是字符串或对象。请传入数组，例如 `["email", "phone"]`。 |
| `Unexpected token '*', "<serialized item>" is not valid JSON` | `error` | 模式匹配到了 JSON 数字（例如存为 `5551234567` 的电话号码）或 JSON 语法（引号、花括号、冒号），导致文本无法解析。请把此类值存为字符串，或让自定义模式只匹配值的内容。错误信息中会包含序列化输入项的片段。 |

## 说明与限制

- 只会处理列出的模式：姓名、地址、其他格式的身份证件号以及 15 位卡号会原样通过。
- 从 `error` 端口输出不会使运行失败：引擎沿 `error` 上的连线继续；若没有连线，该分支直接结束。


## 契约（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
  }
]
```
