-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathvalidator.js
More file actions
124 lines (106 loc) · 3.13 KB
/
Copy pathvalidator.js
File metadata and controls
124 lines (106 loc) · 3.13 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* (c) Copyright IBM Corp. 2026
*/
'use strict';
const { validStackTraceModes } = require('../util/constants');
/**
* @param {any} value
* @returns {number|undefined}
*/
exports.numberValidator = function numberValidator(value) {
if (value == null) return undefined;
const num = typeof value === 'number' ? value : Number(value);
return Number.isNaN(num) ? undefined : num;
};
/**
* @param {any} value
* @returns {boolean|undefined}
*/
exports.booleanValidator = function booleanValidator(value) {
if (value == null) return undefined;
if (typeof value === 'boolean') return value;
if (typeof value === 'string') {
const normalized = value.toLowerCase();
if (normalized === 'true' || normalized === '1') return true;
if (normalized === 'false' || normalized === '0') return false;
}
return undefined;
};
/**
* @param {any} value
* @returns {string|undefined}
*/
exports.stringValidator = function stringValidator(value) {
if (value == null) return undefined;
return typeof value === 'string' ? value : undefined;
};
/**
* @param {any} value
* @returns {boolean|undefined}
*/
exports.validateTruthyBoolean = function validateTruthyBoolean(value) {
// Return true if value is truthy, undefined otherwise
return value ? true : undefined;
};
/**
* 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 };
};