probejs-core
Version:
A powerful tool for traversing and investigating nested objects
59 lines • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateOptions = validateOptions;
exports.validateKey = validateKey;
exports.validatePath = validatePath;
exports.detectCircular = detectCircular;
const errors_1 = require("./errors");
function validateOptions(options) {
if (typeof options.caseSensitive !== "boolean") {
throw new errors_1.ProbeError("caseSensitive option must be a boolean");
}
// Changed validation for maxDepth to handle Infinity correctly
if (typeof options.maxDepth !== "number" || options.maxDepth < 0) {
throw new errors_1.ProbeError("maxDepth must be a non-negative number");
}
if (typeof options.pathDelimiter !== "string" || !options.pathDelimiter) {
throw new errors_1.ProbeError("pathDelimiter must be a non-empty string");
}
if (typeof options.caching !== "boolean") {
throw new errors_1.ProbeError("caching option must be a boolean");
}
}
function validateKey(key) {
if (typeof key !== "string") {
throw new errors_1.KeyError(String(key), "Key must be a string");
}
if (!key.trim()) {
throw new errors_1.KeyError(key, "Key cannot be empty");
}
}
function validatePath(path) {
if (!path || path === "." || path === "..") {
throw new errors_1.PathError(path, "Invalid path");
}
if (typeof path !== "string") {
throw new errors_1.PathError(String(path), "Path must be a string");
}
if (!path.trim()) {
throw new errors_1.PathError(path, "Path cannot be empty");
}
}
function detectCircular(obj, path = [], seen = new WeakSet()) {
if (obj === null || typeof obj !== "object") {
return; // Skip primitives
}
if (seen.has(obj)) {
throw new errors_1.CircularReferenceError(path.join("."));
}
seen.add(obj);
for (const [key, value] of Object.entries(obj)) {
if (typeof value === "object" && value !== null) {
detectCircular(value, [...path, key], seen);
}
}
// Remove the object from the seen set after traversal
// This ensures that shared references don't trigger circular reference errors
seen.delete(obj);
}
//# sourceMappingURL=validation.js.map