@awesomeness-js/utils 1.1.8 → 1.1.20
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/index.js +6 -0
- package/package.json +1 -1
- package/src/clean/custom.js +39 -0
- package/src/clean/file.js +62 -0
- package/src/validateSchema.js +3 -1
- package/types/clean/array.d.ts +5 -1
- package/types/clean/custom.d.ts +4 -0
- package/types/clean/file.d.ts +11 -0
- package/types/clean/object.d.ts +5 -1
- package/types/index.d.ts +6 -0
- package/types/utils/clean.d.ts +10 -2
package/index.js
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
import _build from './src/build.js';
|
|
7
7
|
import _clean_array from './src/clean/array.js';
|
|
8
8
|
import _clean_boolean from './src/clean/boolean.js';
|
|
9
|
+
import _clean_custom from './src/clean/custom.js';
|
|
10
|
+
import _clean_file from './src/clean/file.js';
|
|
9
11
|
import _clean_integer from './src/clean/integer.js';
|
|
10
12
|
import _clean_number from './src/clean/number.js';
|
|
11
13
|
import _clean_object from './src/clean/object.js';
|
|
@@ -64,6 +66,8 @@ export { _wait as wait };
|
|
|
64
66
|
export const clean = {
|
|
65
67
|
array: _clean_array,
|
|
66
68
|
boolean: _clean_boolean,
|
|
69
|
+
custom: _clean_custom,
|
|
70
|
+
file: _clean_file,
|
|
67
71
|
integer: _clean_integer,
|
|
68
72
|
number: _clean_number,
|
|
69
73
|
object: _clean_object,
|
|
@@ -141,6 +145,8 @@ export default {
|
|
|
141
145
|
clean: {
|
|
142
146
|
array: _clean_array,
|
|
143
147
|
boolean: _clean_boolean,
|
|
148
|
+
custom: _clean_custom,
|
|
149
|
+
file: _clean_file,
|
|
144
150
|
integer: _clean_integer,
|
|
145
151
|
number: _clean_number,
|
|
146
152
|
object: _clean_object,
|
package/package.json
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export default function cleanCustom(value, {
|
|
2
|
+
required = false,
|
|
3
|
+
validate = null, // callback that returns true or cleaned value, throws or returns false if invalid
|
|
4
|
+
} = {}) {
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
if (value === undefined || value === null) {
|
|
8
|
+
throw {
|
|
9
|
+
message: 'Value is missing',
|
|
10
|
+
value
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (typeof validate === 'function') {
|
|
15
|
+
const result = validate(value);
|
|
16
|
+
|
|
17
|
+
// if validator returns false, treat as invalid
|
|
18
|
+
if (result === false) {
|
|
19
|
+
throw {
|
|
20
|
+
message: 'Validation failed',
|
|
21
|
+
value
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// if validator returns something else, assume cleaned value
|
|
26
|
+
return result === true ? value : result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// no validation callback, just return the value
|
|
30
|
+
return value;
|
|
31
|
+
|
|
32
|
+
} catch (e) {
|
|
33
|
+
if (required) {
|
|
34
|
+
throw e;
|
|
35
|
+
} else {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export default function cleanFile(file, {
|
|
2
|
+
required = false,
|
|
3
|
+
validTypes = [],
|
|
4
|
+
minSize = false, // bytes
|
|
5
|
+
maxSize = false, // bytes
|
|
6
|
+
} = {}) {
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
if (!file || typeof file !== 'object') {
|
|
10
|
+
throw {
|
|
11
|
+
message: 'File must be an object (like req.file or File API object)',
|
|
12
|
+
value: file
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof file.size !== 'number' || typeof file.type !== 'string') {
|
|
17
|
+
throw {
|
|
18
|
+
message: 'File object missing required fields (size, type)',
|
|
19
|
+
value: file
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// type validation
|
|
24
|
+
if (Array.isArray(validTypes) && validTypes.length > 0) {
|
|
25
|
+
const isValidType = validTypes.some(t => file.type === t || file.name?.endsWith(t));
|
|
26
|
+
if (!isValidType) {
|
|
27
|
+
throw {
|
|
28
|
+
message: `Invalid file type: ${file.type}`,
|
|
29
|
+
allowed: validTypes,
|
|
30
|
+
value: file.name || null
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// size validation
|
|
36
|
+
if (minSize !== false && file.size < minSize) {
|
|
37
|
+
throw {
|
|
38
|
+
message: `File is smaller than minimum size (${minSize} bytes)`,
|
|
39
|
+
value: file.size
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (maxSize !== false && file.size > maxSize) {
|
|
44
|
+
throw {
|
|
45
|
+
message: `File is larger than maximum size (${maxSize} bytes)`,
|
|
46
|
+
value: file.size
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// return a normalized safe file info object
|
|
51
|
+
return {
|
|
52
|
+
name: file.name || null,
|
|
53
|
+
type: file.type,
|
|
54
|
+
size: file.size,
|
|
55
|
+
lastModified: file.lastModified ? new Date(file.lastModified).toISOString() : null,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
} catch (e) {
|
|
59
|
+
if (required) throw e;
|
|
60
|
+
else return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/validateSchema.js
CHANGED
|
@@ -40,12 +40,14 @@ function validateSchema(schema){
|
|
|
40
40
|
const validTypes = [
|
|
41
41
|
'array',
|
|
42
42
|
'boolean',
|
|
43
|
+
'custom',
|
|
44
|
+
'file',
|
|
43
45
|
'integer',
|
|
44
46
|
'number',
|
|
45
47
|
'object',
|
|
46
48
|
'string',
|
|
47
49
|
'timestamp',
|
|
48
|
-
'uuid'
|
|
50
|
+
'uuid',
|
|
49
51
|
];
|
|
50
52
|
|
|
51
53
|
if(!schema.type || !validTypes.includes(schema.type)) {
|
package/types/clean/array.d.ts
CHANGED
package/types/clean/object.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
import type _build from './build';
|
|
7
7
|
import type _clean_array from './clean/array';
|
|
8
8
|
import type _clean_boolean from './clean/boolean';
|
|
9
|
+
import type _clean_custom from './clean/custom';
|
|
10
|
+
import type _clean_file from './clean/file';
|
|
9
11
|
import type _clean_integer from './clean/integer';
|
|
10
12
|
import type _clean_number from './clean/number';
|
|
11
13
|
import type _clean_object from './clean/object';
|
|
@@ -64,6 +66,8 @@ export declare const wait: typeof _wait;
|
|
|
64
66
|
export declare const clean: {
|
|
65
67
|
array: typeof _clean_array;
|
|
66
68
|
boolean: typeof _clean_boolean;
|
|
69
|
+
custom: typeof _clean_custom;
|
|
70
|
+
file: typeof _clean_file;
|
|
67
71
|
integer: typeof _clean_integer;
|
|
68
72
|
number: typeof _clean_number;
|
|
69
73
|
object: typeof _clean_object;
|
|
@@ -141,6 +145,8 @@ declare const _default: {
|
|
|
141
145
|
clean: {
|
|
142
146
|
array: typeof _clean_array,
|
|
143
147
|
boolean: typeof _clean_boolean,
|
|
148
|
+
custom: typeof _clean_custom,
|
|
149
|
+
file: typeof _clean_file,
|
|
144
150
|
integer: typeof _clean_integer,
|
|
145
151
|
number: typeof _clean_number,
|
|
146
152
|
object: typeof _clean_object,
|
package/types/utils/clean.d.ts
CHANGED
|
@@ -9,8 +9,16 @@ declare namespace _default {
|
|
|
9
9
|
export { cleanUUID as uuid };
|
|
10
10
|
}
|
|
11
11
|
export default _default;
|
|
12
|
-
declare function cleanArray(arr: any, schema?: {}
|
|
13
|
-
|
|
12
|
+
declare function cleanArray(arr: any, schema?: {}, { testMode, allOrNothing, path }?: {
|
|
13
|
+
testMode?: boolean;
|
|
14
|
+
allOrNothing?: boolean;
|
|
15
|
+
path?: string;
|
|
16
|
+
}): any[];
|
|
17
|
+
declare function cleanObject(obj: any, schema: any, { testMode, allOrNothing, path }?: {
|
|
18
|
+
testMode?: boolean;
|
|
19
|
+
allOrNothing?: boolean;
|
|
20
|
+
path?: string;
|
|
21
|
+
}): {};
|
|
14
22
|
import cleanBoolean from '../clean/boolean.js';
|
|
15
23
|
import cleanInteger from '../clean/integer.js';
|
|
16
24
|
import cleanNumber from '../clean/number.js';
|