UNPKG

@syntropysoft/praetorian

Version:

Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.

145 lines 4.23 kB
"use strict"; /** * Pattern Validator - Functional Programming * * Single Responsibility: Validate string patterns only * Pure functions, no state, no side effects */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getStringValue = exports.isEmpty = exports.isString = exports.validatePatterns = void 0; /** * Pure function to validate patterns against data */ const validatePatterns = (data, rules, context) => { // Guard clause: no rules provided if (!rules || rules.length === 0) { return createEmptyResult(); } // Guard clause: no data provided if (data === null || data === undefined) { return createEmptyResult(); } const results = rules.map(rule => validateSinglePattern(data, rule, context)); const errors = results.flatMap(r => r.error ? [r.error] : []); const warnings = results.flatMap(r => r.warning ? [r.warning] : []); const valid = errors.length === 0; return { valid, errors, warnings, results, summary: createSummary(results) }; }; exports.validatePatterns = validatePatterns; /** * Pure function to validate a single pattern */ const validateSinglePattern = (data, rule, context) => { // Guard clause: invalid rule if (!rule || !rule.pattern) { return createFailedResult(rule, 'Invalid pattern rule'); } const value = extractValueFromPath(data, rule.targetPath); const regex = createRegexFromPattern(rule); if (!regex) { return createFailedResult(rule, 'Invalid regex pattern'); } const matched = regex.test(value); if (matched) { return createSuccessResult(rule, value, rule.targetPath); } return createFailedResult(rule, rule.message || `Value does not match pattern: ${rule.pattern}`, value, rule.targetPath); }; /** * Pure function to extract value from nested path */ const extractValueFromPath = (data, path) => { // Guard clause: no path specified if (!path) { return String(data); } // Guard clause: invalid data if (data === null || data === undefined) { return ''; } return path.split('.').reduce((obj, key) => { if (obj && typeof obj === 'object' && key in obj) { return obj[key]; } return undefined; }, data) || ''; }; /** * Pure function to create regex from pattern */ const createRegexFromPattern = (rule) => { try { const flags = rule.flags || ''; return new RegExp(rule.pattern, flags); } catch (error) { return null; } }; /** * Pure function to create success result */ const createSuccessResult = (rule, value, path) => ({ rule, matched: true, testedValue: value, path }); /** * Pure function to create failed result */ const createFailedResult = (rule, message, value, path) => ({ rule, matched: false, error: { code: `PATTERN_MISMATCH_${rule.id.toUpperCase()}`, message, severity: rule.severity, path: path || '', context: { pattern: rule.pattern, value } }, testedValue: value, path }); /** * Pure function to create empty result */ const createEmptyResult = () => ({ valid: true, errors: [], warnings: [], results: [], summary: { total: 0, passed: 0, failed: 0, warnings: 0 } }); /** * Pure function to create summary */ const createSummary = (results) => { const total = results.length; const passed = results.filter(r => r.matched).length; const failed = results.filter(r => !r.matched).length; const warnings = results.filter(r => r.warning).length; return { total, passed, failed, warnings }; }; /** * Pure function to check if value is string */ const isString = (value) => typeof value === 'string'; exports.isString = isString; /** * Pure function to check if value is empty */ const isEmpty = (value) => value === null || value === undefined || value === ''; exports.isEmpty = isEmpty; /** * Pure function to get string value safely */ const getStringValue = (value) => (0, exports.isString)(value) ? value : String(value || ''); exports.getStringValue = getStringValue; //# sourceMappingURL=PatternValidator.js.map