form-sanitize 1.0.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 +21 -0
- package/README.md +72 -0
- package/dist/index.d.mts +75 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +268 -0
- package/dist/index.mjs +233 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tnikhil-24
|
|
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,72 @@
|
|
|
1
|
+
# form-sanitize
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
Schema-based form sanitization that works on your **React frontend and Express backend** — define rules once, run everywhere.
|
|
9
|
+
|
|
10
|
+
## Why form-sanitize?
|
|
11
|
+
|
|
12
|
+
| | DOMPurify | validator.js | form-sanitize |
|
|
13
|
+
|---|---|---|---|
|
|
14
|
+
| Works in Node.js | ❌ | ✅ | ✅ |
|
|
15
|
+
| Schema-based | ❌ | ❌ | ✅ |
|
|
16
|
+
| Chainable API | ❌ | ❌ | ✅ |
|
|
17
|
+
| Nested objects | ❌ | ❌ | ✅ |
|
|
18
|
+
| Zero dependencies | ✅ | ✅ | ✅ |
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install form-sanitize
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { createSchema, s } from 'form-sanitize'
|
|
30
|
+
|
|
31
|
+
const contactForm = createSchema({
|
|
32
|
+
name: s.string().trim().stripTags(),
|
|
33
|
+
email: s.string().trim().normalizeEmail(),
|
|
34
|
+
age: s.number().clamp(0, 120),
|
|
35
|
+
active: s.boolean(),
|
|
36
|
+
address: s.object({
|
|
37
|
+
city: s.string().trim(),
|
|
38
|
+
zip: s.string().truncate(10),
|
|
39
|
+
}),
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const clean = contactForm.sanitize(req.body)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### `s.string()`
|
|
48
|
+
| Method | Description |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `.trim()` | Remove leading/trailing whitespace |
|
|
51
|
+
| `.stripTags()` | Remove HTML and script tags including content |
|
|
52
|
+
| `.truncate(n)` | Limit to n characters |
|
|
53
|
+
| `.normalizeEmail()` | Lowercase, remove Gmail dots and aliases |
|
|
54
|
+
| `.escape()` | HTML-encode special characters |
|
|
55
|
+
| `.toSlug()` | Convert to URL-safe slug |
|
|
56
|
+
|
|
57
|
+
### `s.number()`
|
|
58
|
+
| Method | Description |
|
|
59
|
+
|---|---|
|
|
60
|
+
| `.clamp(min, max)` | Keep value within range |
|
|
61
|
+
| `.round(decimals)` | Round to decimal places |
|
|
62
|
+
| `.abs()` | Make value positive |
|
|
63
|
+
|
|
64
|
+
### `s.boolean()`
|
|
65
|
+
Coerces `"true"`, `"yes"`, `"1"`, `1` → `true` and `"false"`, `"no"`, `"0"`, `0` → `false`.
|
|
66
|
+
|
|
67
|
+
### `s.object(definition)`
|
|
68
|
+
Sanitize nested objects recursively.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
declare class StringSanitizer {
|
|
2
|
+
private val;
|
|
3
|
+
constructor(value: unknown);
|
|
4
|
+
trim(): this;
|
|
5
|
+
stripTags(): this;
|
|
6
|
+
truncate(length: number): this;
|
|
7
|
+
normalizeEmail(): this;
|
|
8
|
+
escape(): this;
|
|
9
|
+
toSlug(): this;
|
|
10
|
+
value(): string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare class NumberSanitizer {
|
|
14
|
+
private val;
|
|
15
|
+
constructor(value: unknown);
|
|
16
|
+
clamp(min: number, max: number): this;
|
|
17
|
+
round(decimals?: number): this;
|
|
18
|
+
abs(): this;
|
|
19
|
+
value(): number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare class BooleanSanitizer {
|
|
23
|
+
private val;
|
|
24
|
+
constructor(value: unknown);
|
|
25
|
+
value(): boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare class StringField {
|
|
29
|
+
private rules;
|
|
30
|
+
trim(): this;
|
|
31
|
+
stripTags(): this;
|
|
32
|
+
truncate(length: number): this;
|
|
33
|
+
normalizeEmail(): this;
|
|
34
|
+
escape(): this;
|
|
35
|
+
toSlug(): this;
|
|
36
|
+
sanitize(value: unknown): string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare class NumberField {
|
|
40
|
+
private rules;
|
|
41
|
+
clamp(min: number, max: number): this;
|
|
42
|
+
round(decimals?: number): this;
|
|
43
|
+
abs(): this;
|
|
44
|
+
sanitize(value: unknown): number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class BooleanField {
|
|
48
|
+
sanitize(value: unknown): boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type FieldType$1 = StringField | NumberField | BooleanField | ObjectField;
|
|
52
|
+
type ObjectSchemaDefinition = Record<string, FieldType$1>;
|
|
53
|
+
declare class ObjectField {
|
|
54
|
+
private definition;
|
|
55
|
+
constructor(definition: ObjectSchemaDefinition);
|
|
56
|
+
sanitize(value: unknown): Record<string, unknown>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type FieldType = StringField | NumberField | BooleanField | ObjectField;
|
|
60
|
+
type SchemaDefinition = Record<string, FieldType>;
|
|
61
|
+
type InferOutput<T extends SchemaDefinition> = {
|
|
62
|
+
[K in keyof T]: T[K] extends StringField ? string : T[K] extends NumberField ? number : T[K] extends BooleanField ? boolean : T[K] extends ObjectField ? Record<string, unknown> : never;
|
|
63
|
+
};
|
|
64
|
+
declare function createSchema<T extends SchemaDefinition>(definition: T): {
|
|
65
|
+
sanitize(data: Record<string, unknown>): InferOutput<T>;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
declare const s: {
|
|
69
|
+
string: (value?: unknown) => StringField | StringSanitizer;
|
|
70
|
+
number: (value?: unknown) => NumberField | NumberSanitizer;
|
|
71
|
+
boolean: (value?: unknown) => BooleanField | BooleanSanitizer;
|
|
72
|
+
object: (definition: ObjectSchemaDefinition) => ObjectField;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { BooleanField, BooleanSanitizer, NumberField, NumberSanitizer, ObjectField, StringField, StringSanitizer, createSchema, s };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
declare class StringSanitizer {
|
|
2
|
+
private val;
|
|
3
|
+
constructor(value: unknown);
|
|
4
|
+
trim(): this;
|
|
5
|
+
stripTags(): this;
|
|
6
|
+
truncate(length: number): this;
|
|
7
|
+
normalizeEmail(): this;
|
|
8
|
+
escape(): this;
|
|
9
|
+
toSlug(): this;
|
|
10
|
+
value(): string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare class NumberSanitizer {
|
|
14
|
+
private val;
|
|
15
|
+
constructor(value: unknown);
|
|
16
|
+
clamp(min: number, max: number): this;
|
|
17
|
+
round(decimals?: number): this;
|
|
18
|
+
abs(): this;
|
|
19
|
+
value(): number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare class BooleanSanitizer {
|
|
23
|
+
private val;
|
|
24
|
+
constructor(value: unknown);
|
|
25
|
+
value(): boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare class StringField {
|
|
29
|
+
private rules;
|
|
30
|
+
trim(): this;
|
|
31
|
+
stripTags(): this;
|
|
32
|
+
truncate(length: number): this;
|
|
33
|
+
normalizeEmail(): this;
|
|
34
|
+
escape(): this;
|
|
35
|
+
toSlug(): this;
|
|
36
|
+
sanitize(value: unknown): string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare class NumberField {
|
|
40
|
+
private rules;
|
|
41
|
+
clamp(min: number, max: number): this;
|
|
42
|
+
round(decimals?: number): this;
|
|
43
|
+
abs(): this;
|
|
44
|
+
sanitize(value: unknown): number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class BooleanField {
|
|
48
|
+
sanitize(value: unknown): boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type FieldType$1 = StringField | NumberField | BooleanField | ObjectField;
|
|
52
|
+
type ObjectSchemaDefinition = Record<string, FieldType$1>;
|
|
53
|
+
declare class ObjectField {
|
|
54
|
+
private definition;
|
|
55
|
+
constructor(definition: ObjectSchemaDefinition);
|
|
56
|
+
sanitize(value: unknown): Record<string, unknown>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type FieldType = StringField | NumberField | BooleanField | ObjectField;
|
|
60
|
+
type SchemaDefinition = Record<string, FieldType>;
|
|
61
|
+
type InferOutput<T extends SchemaDefinition> = {
|
|
62
|
+
[K in keyof T]: T[K] extends StringField ? string : T[K] extends NumberField ? number : T[K] extends BooleanField ? boolean : T[K] extends ObjectField ? Record<string, unknown> : never;
|
|
63
|
+
};
|
|
64
|
+
declare function createSchema<T extends SchemaDefinition>(definition: T): {
|
|
65
|
+
sanitize(data: Record<string, unknown>): InferOutput<T>;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
declare const s: {
|
|
69
|
+
string: (value?: unknown) => StringField | StringSanitizer;
|
|
70
|
+
number: (value?: unknown) => NumberField | NumberSanitizer;
|
|
71
|
+
boolean: (value?: unknown) => BooleanField | BooleanSanitizer;
|
|
72
|
+
object: (definition: ObjectSchemaDefinition) => ObjectField;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { BooleanField, BooleanSanitizer, NumberField, NumberSanitizer, ObjectField, StringField, StringSanitizer, createSchema, s };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
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
|
+
BooleanField: () => BooleanField,
|
|
24
|
+
BooleanSanitizer: () => BooleanSanitizer,
|
|
25
|
+
NumberField: () => NumberField,
|
|
26
|
+
NumberSanitizer: () => NumberSanitizer,
|
|
27
|
+
ObjectField: () => ObjectField,
|
|
28
|
+
StringField: () => StringField,
|
|
29
|
+
StringSanitizer: () => StringSanitizer,
|
|
30
|
+
createSchema: () => createSchema,
|
|
31
|
+
s: () => s
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/sanitizers/StringSanitizer.ts
|
|
36
|
+
var StringSanitizer = class {
|
|
37
|
+
constructor(value) {
|
|
38
|
+
this.val = value === null || value === void 0 ? "" : String(value);
|
|
39
|
+
}
|
|
40
|
+
trim() {
|
|
41
|
+
this.val = this.val.trim();
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
stripTags() {
|
|
45
|
+
this.val = this.val.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/gi, "");
|
|
46
|
+
this.val = this.val.replace(/<[^>]*>/g, "");
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
truncate(length) {
|
|
50
|
+
this.val = this.val.slice(0, length);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
normalizeEmail() {
|
|
54
|
+
let email = this.val.toLowerCase().trim();
|
|
55
|
+
const [local, domain] = email.split("@");
|
|
56
|
+
if (!domain) return this;
|
|
57
|
+
let normalLocal = local.split("+")[0];
|
|
58
|
+
if (domain === "gmail.com") {
|
|
59
|
+
normalLocal = normalLocal.replace(/\./g, "");
|
|
60
|
+
}
|
|
61
|
+
this.val = `${normalLocal}@${domain}`;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
escape() {
|
|
65
|
+
this.val = this.val.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
toSlug() {
|
|
69
|
+
this.val = this.val.toLowerCase().trim().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-");
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
value() {
|
|
73
|
+
return this.val;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/sanitizers/NumberSanitizer.ts
|
|
78
|
+
var NumberSanitizer = class {
|
|
79
|
+
constructor(value) {
|
|
80
|
+
const parsed = Number(value);
|
|
81
|
+
this.val = isNaN(parsed) ? 0 : parsed;
|
|
82
|
+
}
|
|
83
|
+
clamp(min, max) {
|
|
84
|
+
this.val = Math.min(Math.max(this.val, min), max);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
round(decimals = 0) {
|
|
88
|
+
const factor = Math.pow(10, decimals);
|
|
89
|
+
this.val = Math.round(this.val * factor) / factor;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
abs() {
|
|
93
|
+
this.val = Math.abs(this.val);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
value() {
|
|
97
|
+
return this.val;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// src/sanitizers/BooleanSanitizer.ts
|
|
102
|
+
var TRUTHY = /* @__PURE__ */ new Set(["true", "1", "yes", "on", "y"]);
|
|
103
|
+
var FALSY = /* @__PURE__ */ new Set(["false", "0", "no", "off", "n"]);
|
|
104
|
+
var BooleanSanitizer = class {
|
|
105
|
+
constructor(value) {
|
|
106
|
+
if (typeof value === "boolean") {
|
|
107
|
+
this.val = value;
|
|
108
|
+
} else {
|
|
109
|
+
const str = String(value).toLowerCase().trim();
|
|
110
|
+
if (TRUTHY.has(str)) {
|
|
111
|
+
this.val = true;
|
|
112
|
+
} else if (FALSY.has(str)) {
|
|
113
|
+
this.val = false;
|
|
114
|
+
} else {
|
|
115
|
+
this.val = Boolean(value);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
value() {
|
|
120
|
+
return this.val;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/schema/StringField.ts
|
|
125
|
+
var StringField = class {
|
|
126
|
+
constructor() {
|
|
127
|
+
this.rules = [];
|
|
128
|
+
}
|
|
129
|
+
trim() {
|
|
130
|
+
this.rules.push((val) => val.trim());
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
stripTags() {
|
|
134
|
+
this.rules.push((val) => {
|
|
135
|
+
val = val.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/gi, "");
|
|
136
|
+
return val.replace(/<[^>]*>/g, "");
|
|
137
|
+
});
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
truncate(length) {
|
|
141
|
+
this.rules.push((val) => val.slice(0, length));
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
normalizeEmail() {
|
|
145
|
+
this.rules.push((val) => {
|
|
146
|
+
let email = val.toLowerCase().trim();
|
|
147
|
+
const [local, domain] = email.split("@");
|
|
148
|
+
if (!domain) return email;
|
|
149
|
+
let normalLocal = local.split("+")[0];
|
|
150
|
+
if (domain === "gmail.com") {
|
|
151
|
+
normalLocal = normalLocal.replace(/\./g, "");
|
|
152
|
+
}
|
|
153
|
+
return `${normalLocal}@${domain}`;
|
|
154
|
+
});
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
escape() {
|
|
158
|
+
this.rules.push(
|
|
159
|
+
(val) => val.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'")
|
|
160
|
+
);
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
toSlug() {
|
|
164
|
+
this.rules.push(
|
|
165
|
+
(val) => val.toLowerCase().trim().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-")
|
|
166
|
+
);
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
sanitize(value) {
|
|
170
|
+
let val = value === null || value === void 0 ? "" : String(value);
|
|
171
|
+
for (const rule of this.rules) {
|
|
172
|
+
val = rule(val);
|
|
173
|
+
}
|
|
174
|
+
return val;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/schema/NumberField.ts
|
|
179
|
+
var NumberField = class {
|
|
180
|
+
constructor() {
|
|
181
|
+
this.rules = [];
|
|
182
|
+
}
|
|
183
|
+
clamp(min, max) {
|
|
184
|
+
this.rules.push((val) => Math.min(Math.max(val, min), max));
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
round(decimals = 0) {
|
|
188
|
+
this.rules.push((val) => {
|
|
189
|
+
const factor = Math.pow(10, decimals);
|
|
190
|
+
return Math.round(val * factor) / factor;
|
|
191
|
+
});
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
abs() {
|
|
195
|
+
this.rules.push((val) => Math.abs(val));
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
sanitize(value) {
|
|
199
|
+
const parsed = Number(value);
|
|
200
|
+
let val = isNaN(parsed) ? 0 : parsed;
|
|
201
|
+
for (const rule of this.rules) {
|
|
202
|
+
val = rule(val);
|
|
203
|
+
}
|
|
204
|
+
return val;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/schema/BooleanField.ts
|
|
209
|
+
var TRUTHY2 = /* @__PURE__ */ new Set(["true", "1", "yes", "on", "y"]);
|
|
210
|
+
var FALSY2 = /* @__PURE__ */ new Set(["false", "0", "no", "off", "n"]);
|
|
211
|
+
var BooleanField = class {
|
|
212
|
+
sanitize(value) {
|
|
213
|
+
if (typeof value === "boolean") return value;
|
|
214
|
+
const str = String(value).toLowerCase().trim();
|
|
215
|
+
if (TRUTHY2.has(str)) return true;
|
|
216
|
+
if (FALSY2.has(str)) return false;
|
|
217
|
+
return Boolean(value);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/schema/ObjectField.ts
|
|
222
|
+
var ObjectField = class {
|
|
223
|
+
constructor(definition) {
|
|
224
|
+
this.definition = definition;
|
|
225
|
+
}
|
|
226
|
+
sanitize(value) {
|
|
227
|
+
const data = typeof value === "object" && value !== null ? value : {};
|
|
228
|
+
const result = {};
|
|
229
|
+
for (const key in this.definition) {
|
|
230
|
+
result[key] = this.definition[key].sanitize(data[key]);
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// src/schema/createSchema.ts
|
|
237
|
+
function createSchema(definition) {
|
|
238
|
+
return {
|
|
239
|
+
sanitize(data) {
|
|
240
|
+
const result = {};
|
|
241
|
+
for (const key in definition) {
|
|
242
|
+
const field = definition[key];
|
|
243
|
+
result[key] = field.sanitize(data[key]);
|
|
244
|
+
}
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/index.ts
|
|
251
|
+
var s = {
|
|
252
|
+
string: (value) => value !== void 0 ? new StringSanitizer(value) : new StringField(),
|
|
253
|
+
number: (value) => value !== void 0 ? new NumberSanitizer(value) : new NumberField(),
|
|
254
|
+
boolean: (value) => value !== void 0 ? new BooleanSanitizer(value) : new BooleanField(),
|
|
255
|
+
object: (definition) => new ObjectField(definition)
|
|
256
|
+
};
|
|
257
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
258
|
+
0 && (module.exports = {
|
|
259
|
+
BooleanField,
|
|
260
|
+
BooleanSanitizer,
|
|
261
|
+
NumberField,
|
|
262
|
+
NumberSanitizer,
|
|
263
|
+
ObjectField,
|
|
264
|
+
StringField,
|
|
265
|
+
StringSanitizer,
|
|
266
|
+
createSchema,
|
|
267
|
+
s
|
|
268
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// src/sanitizers/StringSanitizer.ts
|
|
2
|
+
var StringSanitizer = class {
|
|
3
|
+
constructor(value) {
|
|
4
|
+
this.val = value === null || value === void 0 ? "" : String(value);
|
|
5
|
+
}
|
|
6
|
+
trim() {
|
|
7
|
+
this.val = this.val.trim();
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
stripTags() {
|
|
11
|
+
this.val = this.val.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/gi, "");
|
|
12
|
+
this.val = this.val.replace(/<[^>]*>/g, "");
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
truncate(length) {
|
|
16
|
+
this.val = this.val.slice(0, length);
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
normalizeEmail() {
|
|
20
|
+
let email = this.val.toLowerCase().trim();
|
|
21
|
+
const [local, domain] = email.split("@");
|
|
22
|
+
if (!domain) return this;
|
|
23
|
+
let normalLocal = local.split("+")[0];
|
|
24
|
+
if (domain === "gmail.com") {
|
|
25
|
+
normalLocal = normalLocal.replace(/\./g, "");
|
|
26
|
+
}
|
|
27
|
+
this.val = `${normalLocal}@${domain}`;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
escape() {
|
|
31
|
+
this.val = this.val.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
toSlug() {
|
|
35
|
+
this.val = this.val.toLowerCase().trim().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-");
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
value() {
|
|
39
|
+
return this.val;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/sanitizers/NumberSanitizer.ts
|
|
44
|
+
var NumberSanitizer = class {
|
|
45
|
+
constructor(value) {
|
|
46
|
+
const parsed = Number(value);
|
|
47
|
+
this.val = isNaN(parsed) ? 0 : parsed;
|
|
48
|
+
}
|
|
49
|
+
clamp(min, max) {
|
|
50
|
+
this.val = Math.min(Math.max(this.val, min), max);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
round(decimals = 0) {
|
|
54
|
+
const factor = Math.pow(10, decimals);
|
|
55
|
+
this.val = Math.round(this.val * factor) / factor;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
abs() {
|
|
59
|
+
this.val = Math.abs(this.val);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
value() {
|
|
63
|
+
return this.val;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/sanitizers/BooleanSanitizer.ts
|
|
68
|
+
var TRUTHY = /* @__PURE__ */ new Set(["true", "1", "yes", "on", "y"]);
|
|
69
|
+
var FALSY = /* @__PURE__ */ new Set(["false", "0", "no", "off", "n"]);
|
|
70
|
+
var BooleanSanitizer = class {
|
|
71
|
+
constructor(value) {
|
|
72
|
+
if (typeof value === "boolean") {
|
|
73
|
+
this.val = value;
|
|
74
|
+
} else {
|
|
75
|
+
const str = String(value).toLowerCase().trim();
|
|
76
|
+
if (TRUTHY.has(str)) {
|
|
77
|
+
this.val = true;
|
|
78
|
+
} else if (FALSY.has(str)) {
|
|
79
|
+
this.val = false;
|
|
80
|
+
} else {
|
|
81
|
+
this.val = Boolean(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
value() {
|
|
86
|
+
return this.val;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/schema/StringField.ts
|
|
91
|
+
var StringField = class {
|
|
92
|
+
constructor() {
|
|
93
|
+
this.rules = [];
|
|
94
|
+
}
|
|
95
|
+
trim() {
|
|
96
|
+
this.rules.push((val) => val.trim());
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
stripTags() {
|
|
100
|
+
this.rules.push((val) => {
|
|
101
|
+
val = val.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/gi, "");
|
|
102
|
+
return val.replace(/<[^>]*>/g, "");
|
|
103
|
+
});
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
truncate(length) {
|
|
107
|
+
this.rules.push((val) => val.slice(0, length));
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
normalizeEmail() {
|
|
111
|
+
this.rules.push((val) => {
|
|
112
|
+
let email = val.toLowerCase().trim();
|
|
113
|
+
const [local, domain] = email.split("@");
|
|
114
|
+
if (!domain) return email;
|
|
115
|
+
let normalLocal = local.split("+")[0];
|
|
116
|
+
if (domain === "gmail.com") {
|
|
117
|
+
normalLocal = normalLocal.replace(/\./g, "");
|
|
118
|
+
}
|
|
119
|
+
return `${normalLocal}@${domain}`;
|
|
120
|
+
});
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
escape() {
|
|
124
|
+
this.rules.push(
|
|
125
|
+
(val) => val.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'")
|
|
126
|
+
);
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
toSlug() {
|
|
130
|
+
this.rules.push(
|
|
131
|
+
(val) => val.toLowerCase().trim().replace(/[^a-z0-9\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-")
|
|
132
|
+
);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
sanitize(value) {
|
|
136
|
+
let val = value === null || value === void 0 ? "" : String(value);
|
|
137
|
+
for (const rule of this.rules) {
|
|
138
|
+
val = rule(val);
|
|
139
|
+
}
|
|
140
|
+
return val;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/schema/NumberField.ts
|
|
145
|
+
var NumberField = class {
|
|
146
|
+
constructor() {
|
|
147
|
+
this.rules = [];
|
|
148
|
+
}
|
|
149
|
+
clamp(min, max) {
|
|
150
|
+
this.rules.push((val) => Math.min(Math.max(val, min), max));
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
round(decimals = 0) {
|
|
154
|
+
this.rules.push((val) => {
|
|
155
|
+
const factor = Math.pow(10, decimals);
|
|
156
|
+
return Math.round(val * factor) / factor;
|
|
157
|
+
});
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
abs() {
|
|
161
|
+
this.rules.push((val) => Math.abs(val));
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
sanitize(value) {
|
|
165
|
+
const parsed = Number(value);
|
|
166
|
+
let val = isNaN(parsed) ? 0 : parsed;
|
|
167
|
+
for (const rule of this.rules) {
|
|
168
|
+
val = rule(val);
|
|
169
|
+
}
|
|
170
|
+
return val;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// src/schema/BooleanField.ts
|
|
175
|
+
var TRUTHY2 = /* @__PURE__ */ new Set(["true", "1", "yes", "on", "y"]);
|
|
176
|
+
var FALSY2 = /* @__PURE__ */ new Set(["false", "0", "no", "off", "n"]);
|
|
177
|
+
var BooleanField = class {
|
|
178
|
+
sanitize(value) {
|
|
179
|
+
if (typeof value === "boolean") return value;
|
|
180
|
+
const str = String(value).toLowerCase().trim();
|
|
181
|
+
if (TRUTHY2.has(str)) return true;
|
|
182
|
+
if (FALSY2.has(str)) return false;
|
|
183
|
+
return Boolean(value);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/schema/ObjectField.ts
|
|
188
|
+
var ObjectField = class {
|
|
189
|
+
constructor(definition) {
|
|
190
|
+
this.definition = definition;
|
|
191
|
+
}
|
|
192
|
+
sanitize(value) {
|
|
193
|
+
const data = typeof value === "object" && value !== null ? value : {};
|
|
194
|
+
const result = {};
|
|
195
|
+
for (const key in this.definition) {
|
|
196
|
+
result[key] = this.definition[key].sanitize(data[key]);
|
|
197
|
+
}
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// src/schema/createSchema.ts
|
|
203
|
+
function createSchema(definition) {
|
|
204
|
+
return {
|
|
205
|
+
sanitize(data) {
|
|
206
|
+
const result = {};
|
|
207
|
+
for (const key in definition) {
|
|
208
|
+
const field = definition[key];
|
|
209
|
+
result[key] = field.sanitize(data[key]);
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/index.ts
|
|
217
|
+
var s = {
|
|
218
|
+
string: (value) => value !== void 0 ? new StringSanitizer(value) : new StringField(),
|
|
219
|
+
number: (value) => value !== void 0 ? new NumberSanitizer(value) : new NumberField(),
|
|
220
|
+
boolean: (value) => value !== void 0 ? new BooleanSanitizer(value) : new BooleanField(),
|
|
221
|
+
object: (definition) => new ObjectField(definition)
|
|
222
|
+
};
|
|
223
|
+
export {
|
|
224
|
+
BooleanField,
|
|
225
|
+
BooleanSanitizer,
|
|
226
|
+
NumberField,
|
|
227
|
+
NumberSanitizer,
|
|
228
|
+
ObjectField,
|
|
229
|
+
StringField,
|
|
230
|
+
StringSanitizer,
|
|
231
|
+
createSchema,
|
|
232
|
+
s
|
|
233
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "form-sanitize",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Schema-based form sanitization that works on frontend and backend",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"form",
|
|
22
|
+
"sanitize",
|
|
23
|
+
"sanitizer",
|
|
24
|
+
"schema",
|
|
25
|
+
"xss",
|
|
26
|
+
"validation",
|
|
27
|
+
"typescript"
|
|
28
|
+
],
|
|
29
|
+
"author": "tnikhil-24",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/tnikhil-24/form-sanitize.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/tnikhil-24/form-sanitize#readme",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"test:watch": "vitest",
|
|
40
|
+
"prepublishOnly": "npm run build && npm test"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^25.6.0",
|
|
44
|
+
"eslint": "^10.2.0",
|
|
45
|
+
"prettier": "^3.8.2",
|
|
46
|
+
"tsup": "^8.5.1",
|
|
47
|
+
"typescript": "^6.0.2",
|
|
48
|
+
"vitest": "^4.1.4"
|
|
49
|
+
}
|
|
50
|
+
}
|