shelving 1.232.0 → 1.233.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/package.json +1 -1
- package/schema/EmailSchema.js +3 -1
- package/schema/URISchema.d.ts +1 -0
- package/schema/URISchema.js +5 -0
- package/schema/URLSchema.d.ts +1 -0
- package/schema/URLSchema.js +5 -0
- package/util/string.d.ts +9 -0
- package/util/string.js +13 -0
package/package.json
CHANGED
package/schema/EmailSchema.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sanitizeWord } from "../util/string.js";
|
|
1
2
|
import { NULLABLE } from "./NullableSchema.js";
|
|
2
3
|
import { StringSchema } from "./StringSchema.js";
|
|
3
4
|
const R_MATCH = /^[a-z0-9](?:[a-zA-Z0-9._+-]{0,62}[a-zA-Z0-9])?@(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.){1,3}(?:[a-z]{2,63}|xn--[a-z0-9-]{0,58}[a-z0-9])$/;
|
|
@@ -32,7 +33,8 @@ export class EmailSchema extends StringSchema {
|
|
|
32
33
|
});
|
|
33
34
|
}
|
|
34
35
|
sanitize(str) {
|
|
35
|
-
|
|
36
|
+
// Email addresses never contain whitespace, so strip it entirely, then lowercase (RFC says addresses should be case-insensitive).
|
|
37
|
+
return sanitizeWord(str).toLowerCase();
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
/** Valid email, e.g. `test@test.com` */
|
package/schema/URISchema.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export declare class URISchema extends StringSchema {
|
|
|
14
14
|
readonly schemes: URISchemes;
|
|
15
15
|
constructor({ one, title, schemes, ...options }: URISchemaOptions);
|
|
16
16
|
validate(unsafeValue: unknown): URIString;
|
|
17
|
+
sanitize(str: string): string;
|
|
17
18
|
format(value: string): string;
|
|
18
19
|
}
|
|
19
20
|
/** Valid URI string, e.g. `https://www.google.com` */
|
package/schema/URISchema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { formatURI } from "../util/format.js";
|
|
2
|
+
import { sanitizeWord } from "../util/string.js";
|
|
2
3
|
import { getURI, HTTP_SCHEMES } from "../util/uri.js";
|
|
3
4
|
import { NULLABLE } from "./NullableSchema.js";
|
|
4
5
|
import { StringSchema } from "./StringSchema.js";
|
|
@@ -31,6 +32,10 @@ export class URISchema extends StringSchema {
|
|
|
31
32
|
throw `Invalid ${this.one} scheme`;
|
|
32
33
|
return uri.href;
|
|
33
34
|
}
|
|
35
|
+
sanitize(str) {
|
|
36
|
+
// URIs never contain whitespace (a real space must be `%20`-encoded), so strip it entirely.
|
|
37
|
+
return sanitizeWord(str);
|
|
38
|
+
}
|
|
34
39
|
format(value) {
|
|
35
40
|
return formatURI(value, this.format);
|
|
36
41
|
}
|
package/schema/URLSchema.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export declare class URLSchema extends StringSchema {
|
|
|
17
17
|
readonly schemes: URISchemes;
|
|
18
18
|
constructor({ one, title, base, schemes, ...options }: URLSchemaOptions);
|
|
19
19
|
validate(unsafeValue: unknown): URLString;
|
|
20
|
+
sanitize(str: string): string;
|
|
20
21
|
format(value: string): string;
|
|
21
22
|
}
|
|
22
23
|
/** Valid URL string, e.g. `https://www.google.com` */
|
package/schema/URLSchema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { formatURL } from "../util/format.js";
|
|
2
|
+
import { sanitizeWord } from "../util/string.js";
|
|
2
3
|
import { HTTP_SCHEMES } from "../util/uri.js";
|
|
3
4
|
import { getURL } from "../util/url.js";
|
|
4
5
|
import { NULLABLE } from "./NullableSchema.js";
|
|
@@ -34,6 +35,10 @@ export class URLSchema extends StringSchema {
|
|
|
34
35
|
throw `Invalid ${this.one} scheme`;
|
|
35
36
|
return url.href;
|
|
36
37
|
}
|
|
38
|
+
sanitize(str) {
|
|
39
|
+
// URLs never contain whitespace (a real space must be `%20`-encoded), so strip it entirely.
|
|
40
|
+
return sanitizeWord(str);
|
|
41
|
+
}
|
|
37
42
|
format(value) {
|
|
38
43
|
return formatURL(value, this.base, this.format);
|
|
39
44
|
}
|
package/util/string.d.ts
CHANGED
|
@@ -42,6 +42,15 @@ export declare function joinStrings(strs: Iterable<string> & NotString, joiner?:
|
|
|
42
42
|
* @example santizeString("\x00Nice! "); // Returns `"Nice!"`
|
|
43
43
|
*/
|
|
44
44
|
export declare function sanitizeText(str: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* Sanitize a single word of text.
|
|
47
|
+
* - Used when you're sanitising a value that can never contain whitespace, e.g. an email address or token.
|
|
48
|
+
* - Remove all control characters (like `sanitizeText()`).
|
|
49
|
+
* - Strip all whitespace entirely (rather than collapsing runs to a single space like `sanitizeText()`).
|
|
50
|
+
*
|
|
51
|
+
* @example sanitizeWord("\x00 a b c "); // Returns `"abc"`
|
|
52
|
+
*/
|
|
53
|
+
export declare function sanitizeWord(str: string): string;
|
|
45
54
|
/**
|
|
46
55
|
* Sanitize multiple lines of text.
|
|
47
56
|
* - Used when you're sanitising a multi-line input, e.g. a description for something.
|
package/util/string.js
CHANGED
|
@@ -69,6 +69,19 @@ export function sanitizeText(str) {
|
|
|
69
69
|
.replace(/\s+/gu, " ") // Normalise runs of whitespace to one ` ` space.
|
|
70
70
|
.trim(); // Trim whitespace from the start and end of the string.
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Sanitize a single word of text.
|
|
74
|
+
* - Used when you're sanitising a value that can never contain whitespace, e.g. an email address or token.
|
|
75
|
+
* - Remove all control characters (like `sanitizeText()`).
|
|
76
|
+
* - Strip all whitespace entirely (rather than collapsing runs to a single space like `sanitizeText()`).
|
|
77
|
+
*
|
|
78
|
+
* @example sanitizeWord("\x00 a b c "); // Returns `"abc"`
|
|
79
|
+
*/
|
|
80
|
+
export function sanitizeWord(str) {
|
|
81
|
+
return str
|
|
82
|
+
.replace(/[^\P{C}\s]/gu, "") // Strip control characters (except whitespace).
|
|
83
|
+
.replace(/\s+/gu, ""); // Strip all whitespace entirely.
|
|
84
|
+
}
|
|
72
85
|
/**
|
|
73
86
|
* Sanitize multiple lines of text.
|
|
74
87
|
* - Used when you're sanitising a multi-line input, e.g. a description for something.
|