-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstackTraceValidation.js
More file actions
78 lines (67 loc) · 1.98 KB
/
Copy pathstackTraceValidation.js
File metadata and controls
78 lines (67 loc) · 1.98 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* (c) Copyright IBM Corp. 2025
*/
'use strict';
const { validStackTraceModes } = require('../../util/constants');
/**
* Validates the stack trace mode value.
*
* @param {*} value - The value to validate
* @returns {{ isValid: boolean, error: string | null }} - Validation result
*/
exports.validateStackTraceMode = function validateStackTraceMode(value) {
if (value === null) {
return { isValid: false, error: `The value cannot be null. Valid values are: ${validStackTraceModes.join(', ')}.` };
}
if (typeof value !== 'string') {
return {
isValid: false,
error: `The value has the non-supported type ${typeof value}. Valid values are: ${validStackTraceModes.join(
', '
)}.`
};
}
const normalizedValue = value.toLowerCase();
if (validStackTraceModes.includes(normalizedValue)) {
return { isValid: true, error: null };
}
return {
isValid: false,
error: `Invalid value: "${value}". Valid values are: ${validStackTraceModes.join(', ')}.`
};
};
/**
* Validates the stack trace length value.
*
* @param {*} value - The value to validate
* @returns {{ isValid: boolean, error: string | null }} - Validation result
*/
exports.validateStackTraceLength = function validateStackTraceLength(value) {
if (value == null) {
return { isValid: false, error: 'The value cannot be null' };
}
let parsedValue;
if (typeof value === 'number') {
parsedValue = value;
} else if (typeof value === 'string') {
parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
return {
isValid: false,
error: `The value ("${value}") cannot be parsed to a numerical value.`
};
}
} else {
return {
isValid: false,
error: `The value has the non-supported type ${typeof value}.`
};
}
if (!Number.isFinite(parsedValue)) {
return {
isValid: false,
error: `Invalid value: ${value}. Expected a number or numeric string.`
};
}
return { isValid: true, error: null };
};