@form-engine-ts/privacy 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 form-engine-ts contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # @form-engine-ts/privacy
2
+
3
+ Framework-independent sensitive-data candidate detection for form-engine-ts. The standard detector scans only text and
4
+ textarea answers for email addresses, phone numbers, HTTP(S) URLs, and postal codes. Rules and custom detectors are
5
+ injectable, and findings are advisory so applications can choose confirm, block, or audit behavior.
6
+
7
+ ```ts
8
+ import { createStandardPrivacyDetector } from "@form-engine-ts/privacy";
9
+
10
+ const detector = createStandardPrivacyDetector();
11
+ const findings = detector.detect(schema, values);
12
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createStandardPrivacyDetector: () => createStandardPrivacyDetector
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var STANDARD_RULES = [
27
+ { type: "email", pattern: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/giu },
28
+ { type: "url", pattern: /\bhttps?:\/\/[^\s<>"']+[^\s<>"'.,;:!?)]/giu },
29
+ { type: "phone", pattern: /(?:\+?\d[\d\s().-]{6,}\d)/gu },
30
+ { type: "postal_code", pattern: /(?:〒\s*)?\b\d{3}-?\d{4}\b/gu }
31
+ ];
32
+ function configuredRules(config) {
33
+ const rules = new Map(STANDARD_RULES.map((rule) => [rule.type, rule]));
34
+ for (const rule of config.rules ?? []) {
35
+ if (rule.type.trim().length === 0) throw new TypeError("Privacy detector rule type must not be empty.");
36
+ if (!(rule.pattern instanceof RegExp)) throw new TypeError(`Privacy detector rule ${rule.type} requires a RegExp.`);
37
+ if (rule.enabled === false) rules.delete(rule.type);
38
+ else rules.set(rule.type, rule);
39
+ }
40
+ return [...rules.values()];
41
+ }
42
+ function detectRule(fieldId, text, rule) {
43
+ const flags = rule.pattern.flags.includes("g") ? rule.pattern.flags : `${rule.pattern.flags}g`;
44
+ const pattern = new RegExp(rule.pattern.source, flags);
45
+ const findings = [];
46
+ for (const match of text.matchAll(pattern)) {
47
+ const matchedText = match[0];
48
+ if (matchedText.length === 0 || match.index === void 0) continue;
49
+ if (rule.type === "phone" && matchedText.replaceAll(/\D/gu, "").length < 8) continue;
50
+ findings.push({
51
+ fieldId,
52
+ type: rule.type,
53
+ start: match.index,
54
+ end: match.index + matchedText.length,
55
+ matchedText
56
+ });
57
+ }
58
+ return findings;
59
+ }
60
+ function createStandardPrivacyDetector(config = {}) {
61
+ const rules = configuredRules(config);
62
+ const customDetectors = [...config.customDetectors ?? []];
63
+ if (customDetectors.some((detector) => typeof detector !== "function")) {
64
+ throw new TypeError("customDetectors must contain only functions.");
65
+ }
66
+ return {
67
+ detect(schema, values) {
68
+ const findings = [];
69
+ for (const field of schema.fields) {
70
+ if (field.type !== "text" && field.type !== "textarea") continue;
71
+ const value = values[field.id];
72
+ if (typeof value !== "string" || value.length === 0) continue;
73
+ for (const rule of rules) findings.push(...detectRule(field.id, value, rule));
74
+ for (const detector of customDetectors) findings.push(...detector(field.id, value));
75
+ }
76
+ return findings;
77
+ }
78
+ };
79
+ }
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ createStandardPrivacyDetector
83
+ });
@@ -0,0 +1,24 @@
1
+ import { FormSchema } from '@form-engine-ts/core';
2
+
3
+ interface SensitiveDataFinding {
4
+ readonly fieldId: string;
5
+ readonly type: "email" | "phone" | "url" | "postal_code" | string;
6
+ readonly start?: number;
7
+ readonly end?: number;
8
+ readonly matchedText?: string;
9
+ }
10
+ interface SensitiveDataDetectorRule {
11
+ readonly type: string;
12
+ readonly pattern: RegExp;
13
+ readonly enabled?: boolean;
14
+ }
15
+ interface PrivacyDetectorConfig {
16
+ readonly rules?: readonly SensitiveDataDetectorRule[];
17
+ readonly customDetectors?: readonly ((fieldId: string, text: string) => readonly SensitiveDataFinding[])[];
18
+ }
19
+ interface SensitiveDataDetector {
20
+ detect(schema: FormSchema, values: Record<string, unknown>): readonly SensitiveDataFinding[];
21
+ }
22
+ declare function createStandardPrivacyDetector(config?: PrivacyDetectorConfig): SensitiveDataDetector;
23
+
24
+ export { type PrivacyDetectorConfig, type SensitiveDataDetector, type SensitiveDataDetectorRule, type SensitiveDataFinding, createStandardPrivacyDetector };
@@ -0,0 +1,24 @@
1
+ import { FormSchema } from '@form-engine-ts/core';
2
+
3
+ interface SensitiveDataFinding {
4
+ readonly fieldId: string;
5
+ readonly type: "email" | "phone" | "url" | "postal_code" | string;
6
+ readonly start?: number;
7
+ readonly end?: number;
8
+ readonly matchedText?: string;
9
+ }
10
+ interface SensitiveDataDetectorRule {
11
+ readonly type: string;
12
+ readonly pattern: RegExp;
13
+ readonly enabled?: boolean;
14
+ }
15
+ interface PrivacyDetectorConfig {
16
+ readonly rules?: readonly SensitiveDataDetectorRule[];
17
+ readonly customDetectors?: readonly ((fieldId: string, text: string) => readonly SensitiveDataFinding[])[];
18
+ }
19
+ interface SensitiveDataDetector {
20
+ detect(schema: FormSchema, values: Record<string, unknown>): readonly SensitiveDataFinding[];
21
+ }
22
+ declare function createStandardPrivacyDetector(config?: PrivacyDetectorConfig): SensitiveDataDetector;
23
+
24
+ export { type PrivacyDetectorConfig, type SensitiveDataDetector, type SensitiveDataDetectorRule, type SensitiveDataFinding, createStandardPrivacyDetector };
package/dist/index.js ADDED
@@ -0,0 +1,58 @@
1
+ // src/index.ts
2
+ var STANDARD_RULES = [
3
+ { type: "email", pattern: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/giu },
4
+ { type: "url", pattern: /\bhttps?:\/\/[^\s<>"']+[^\s<>"'.,;:!?)]/giu },
5
+ { type: "phone", pattern: /(?:\+?\d[\d\s().-]{6,}\d)/gu },
6
+ { type: "postal_code", pattern: /(?:〒\s*)?\b\d{3}-?\d{4}\b/gu }
7
+ ];
8
+ function configuredRules(config) {
9
+ const rules = new Map(STANDARD_RULES.map((rule) => [rule.type, rule]));
10
+ for (const rule of config.rules ?? []) {
11
+ if (rule.type.trim().length === 0) throw new TypeError("Privacy detector rule type must not be empty.");
12
+ if (!(rule.pattern instanceof RegExp)) throw new TypeError(`Privacy detector rule ${rule.type} requires a RegExp.`);
13
+ if (rule.enabled === false) rules.delete(rule.type);
14
+ else rules.set(rule.type, rule);
15
+ }
16
+ return [...rules.values()];
17
+ }
18
+ function detectRule(fieldId, text, rule) {
19
+ const flags = rule.pattern.flags.includes("g") ? rule.pattern.flags : `${rule.pattern.flags}g`;
20
+ const pattern = new RegExp(rule.pattern.source, flags);
21
+ const findings = [];
22
+ for (const match of text.matchAll(pattern)) {
23
+ const matchedText = match[0];
24
+ if (matchedText.length === 0 || match.index === void 0) continue;
25
+ if (rule.type === "phone" && matchedText.replaceAll(/\D/gu, "").length < 8) continue;
26
+ findings.push({
27
+ fieldId,
28
+ type: rule.type,
29
+ start: match.index,
30
+ end: match.index + matchedText.length,
31
+ matchedText
32
+ });
33
+ }
34
+ return findings;
35
+ }
36
+ function createStandardPrivacyDetector(config = {}) {
37
+ const rules = configuredRules(config);
38
+ const customDetectors = [...config.customDetectors ?? []];
39
+ if (customDetectors.some((detector) => typeof detector !== "function")) {
40
+ throw new TypeError("customDetectors must contain only functions.");
41
+ }
42
+ return {
43
+ detect(schema, values) {
44
+ const findings = [];
45
+ for (const field of schema.fields) {
46
+ if (field.type !== "text" && field.type !== "textarea") continue;
47
+ const value = values[field.id];
48
+ if (typeof value !== "string" || value.length === 0) continue;
49
+ for (const rule of rules) findings.push(...detectRule(field.id, value, rule));
50
+ for (const detector of customDetectors) findings.push(...detector(field.id, value));
51
+ }
52
+ return findings;
53
+ }
54
+ };
55
+ }
56
+ export {
57
+ createStandardPrivacyDetector
58
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@form-engine-ts/privacy",
3
+ "version": "2.7.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "sideEffects": false,
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs"
22
+ }
23
+ },
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/nitta-a/form-engine-ts.git",
28
+ "directory": "packages/privacy"
29
+ },
30
+ "bugs": {
31
+ "url": "https://github.com/nitta-a/form-engine-ts/issues"
32
+ },
33
+ "homepage": "https://github.com/nitta-a/form-engine-ts#readme",
34
+ "keywords": [
35
+ "form",
36
+ "privacy",
37
+ "pii",
38
+ "typescript"
39
+ ],
40
+ "dependencies": {
41
+ "@form-engine-ts/core": "2.7.0"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core",
45
+ "check": "biome check . && tsc --noEmit",
46
+ "test": "vitest run --globals",
47
+ "typecheck": "tsc --noEmit"
48
+ }
49
+ }