@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
130 lines • 3.85 kB
JavaScript
;
/**
* Format Validator - Functional Programming
*
* Single Responsibility: Validate string formats only
* Pure functions, no state, no side effects
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormatValidator = exports.isStringValue = exports.validatePattern = exports.validateFormat = void 0;
/**
* Pure function registry for format validators
*/
const formatValidators = {
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
uri: /^https?:\/\/.+/,
'date-time': /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/,
date: /^\d{4}-\d{2}-\d{2}$/,
time: /^\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/,
uuid: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
hostname: /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
ipv6: /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
};
/**
* Pure function to validate string format
*/
const validateFormat = (value, schema, path) => {
// Guard clause: invalid schema
if (!schema) {
return [];
}
// Guard clause: not a string
if (typeof value !== 'string') {
return [];
}
// Guard clause: no format specified
if (!schema.format) {
return [];
}
const validator = formatValidators[schema.format];
if (!validator) {
return [createUnsupportedFormatError(path, schema.format)];
}
return !validator.test(value)
? [createInvalidFormatError(path, value, schema.format)]
: [];
};
exports.validateFormat = validateFormat;
/**
* Pure function to validate string pattern
*/
const validatePattern = (value, schema, path) => {
// Guard clause: invalid schema
if (!schema) {
return [];
}
// Guard clause: not a string
if (typeof value !== 'string') {
return [];
}
// Guard clause: no pattern specified
if (!schema.pattern) {
return [];
}
return validatePatternWithRegex(value, schema.pattern, path);
};
exports.validatePattern = validatePattern;
/**
* Pure function to validate pattern with regex
*/
const validatePatternWithRegex = (value, pattern, path) => {
try {
const regex = new RegExp(pattern);
return !regex.test(value)
? [createPatternMismatchError(path, value, pattern)]
: [];
}
catch (error) {
return [createInvalidPatternError(path, pattern)];
}
};
/**
* Pure function to create unsupported format error
*/
const createUnsupportedFormatError = (path, format) => ({
path,
message: `Unsupported format: ${format}`,
code: 'UNSUPPORTED_FORMAT',
actual: format
});
/**
* Pure function to create invalid format error
*/
const createInvalidFormatError = (path, value, format) => ({
path,
message: `Value must be a valid ${format}`,
code: 'INVALID_FORMAT',
actual: value,
expected: format
});
/**
* Pure function to create pattern mismatch error
*/
const createPatternMismatchError = (path, value, pattern) => ({
path,
message: `Value must match pattern: ${pattern}`,
code: 'PATTERN_MISMATCH',
actual: value,
expected: pattern
});
/**
* Pure function to create invalid pattern error
*/
const createInvalidPatternError = (path, pattern) => ({
path,
message: `Invalid pattern: ${pattern}`,
code: 'INVALID_PATTERN',
actual: pattern
});
/**
* Pure function to check if value is string
*/
const isStringValue = (value) => typeof value === 'string';
exports.isStringValue = isStringValue;
/**
* Pure function to get format validator
*/
const getFormatValidator = (format) => formatValidators[format];
exports.getFormatValidator = getFormatValidator;
//# sourceMappingURL=FormatValidator.js.map