-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitize.ts
38 lines (35 loc) · 967 Bytes
/
sanitize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { CollectionAccess } from "./types.ts";
type Schema = Record<string, unknown>;
export const sanitizeCollectionAccess = (
data: CollectionAccess,
): CollectionAccess => {
const booleanKeys = ["get", "post", "patch", "put", "delete"];
const stringKeys = ["key", "description"];
// @ts-ignore ?
return Object.entries(data).reduce((r, [key, value]) => {
if (booleanKeys.includes(key)) {
// @ts-ignore ?
r[key] = value === true;
}
if (stringKeys.includes(key)) {
// @ts-ignore ?
r[key] = String(value);
}
return r;
}, {});
};
export const sanitizeBySchema = <T = Record<string, unknown>>(
schema: Schema,
data: T,
): T => {
// @ts-ignore ?
const allowedKeys: string = Object.keys(schema.properties || {});
// @ts-ignore ?
return Object.entries(data).reduce((r, [key, value]) => {
if (allowedKeys.includes(key)) {
// @ts-ignore ?
r[key] = value;
}
return r;
}, {});
};