fortify-schema
Version:
A modern TypeScript validation library designed around familiar interface syntax and powerful conditional validation. Experience schema validation that feels natural to TypeScript developers while unlocking advanced runtime validation capabilities.
63 lines (61 loc) • 1.83 kB
JavaScript
// Performance monitoring
class ValidationMetrics {
static record(operation, timeMs, hasError) {
const current = this.metrics.get(operation) || {
count: 0,
totalTime: 0,
errors: 0,
};
this.metrics.set(operation, {
count: current.count + 1,
totalTime: current.totalTime + timeMs,
errors: current.errors + (hasError ? 1 : 0),
});
}
static getMetrics() {
const result = {};
for (const [op, data] of this.metrics.entries()) {
result[op] = {
...data,
avgTime: data.totalTime / data.count,
errorRate: data.errors / data.count,
};
}
return result;
}
static reset() {
this.metrics.clear();
}
}
ValidationMetrics.metrics = new Map();
// Validation cache for expensive operations
class ValidationCache {
static get(key) {
const entry = this.cache.get(key);
if (!entry)
return null;
if (Date.now() - entry.timestamp > this.TTL) {
this.cache.delete(key);
return null;
}
return { ...entry.result }; // Deep copy to prevent mutation
}
static set(key, result) {
if (this.cache.size > 1000) {
// Prevent memory leaks
const oldestKey = this.cache.keys().next().value;
this.cache.delete(oldestKey || "");
}
this.cache.set(key, {
result: { ...result },
timestamp: Date.now(),
});
}
static clear() {
this.cache.clear();
}
}
ValidationCache.cache = new Map();
ValidationCache.TTL = 5 * 60 * 1000; // 5 minutes
export { ValidationCache, ValidationMetrics };
//# sourceMappingURL=securityValidatorHelpers.js.map