UNPKG

@syntropysoft/praetorian

Version:

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

141 lines 4.05 kB
"use strict"; /** * Range Validator - Functional Programming * * Single Responsibility: Validate numeric and string ranges only * Pure functions, no state, no side effects */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getStringLength = exports.isStringValueRange = exports.isNumber = exports.validateNumberRange = exports.validateStringLength = void 0; /** * Pure function to validate string length */ const validateStringLength = (value, schema, path) => { // Guard clause: invalid schema if (!schema) { return []; } // Guard clause: not a string if (typeof value !== 'string') { return []; } // Guard clause: no length constraints if (schema.minLength === undefined && schema.maxLength === undefined) { return []; } return [ ...validateMinLength(value, schema, path), ...validateMaxLength(value, schema, path) ]; }; exports.validateStringLength = validateStringLength; /** * Pure function to validate number range */ const validateNumberRange = (value, schema, path) => { // Guard clause: invalid schema if (!schema) { return []; } // Guard clause: not a number if (typeof value !== 'number') { return []; } // Guard clause: no range constraints if (schema.minimum === undefined && schema.maximum === undefined) { return []; } return [ ...validateMinimum(value, schema, path), ...validateMaximum(value, schema, path) ]; }; exports.validateNumberRange = validateNumberRange; /** * Pure function to validate minimum string length */ const validateMinLength = (value, schema, path) => { return schema.minLength !== undefined && value.length < schema.minLength ? [createMinLengthError(path, value.length, schema.minLength)] : []; }; /** * Pure function to validate maximum string length */ const validateMaxLength = (value, schema, path) => { return schema.maxLength !== undefined && value.length > schema.maxLength ? [createMaxLengthError(path, value.length, schema.maxLength)] : []; }; /** * Pure function to validate minimum number value */ const validateMinimum = (value, schema, path) => { return schema.minimum !== undefined && value < schema.minimum ? [createMinimumError(path, value, schema.minimum)] : []; }; /** * Pure function to validate maximum number value */ const validateMaximum = (value, schema, path) => { return schema.maximum !== undefined && value > schema.maximum ? [createMaximumError(path, value, schema.maximum)] : []; }; /** * Pure function to create min length error */ const createMinLengthError = (path, actual, expected) => ({ path, message: `String length must be at least ${expected}`, code: 'MIN_LENGTH_ERROR', actual, expected }); /** * Pure function to create max length error */ const createMaxLengthError = (path, actual, expected) => ({ path, message: `String length must be at most ${expected}`, code: 'MAX_LENGTH_ERROR', actual, expected }); /** * Pure function to create minimum error */ const createMinimumError = (path, actual, expected) => ({ path, message: `Value must be at least ${expected}`, code: 'MINIMUM_ERROR', actual, expected }); /** * Pure function to create maximum error */ const createMaximumError = (path, actual, expected) => ({ path, message: `Value must be at most ${expected}`, code: 'MAXIMUM_ERROR', actual, expected }); /** * Pure function to check if value is number */ const isNumber = (value) => typeof value === 'number'; exports.isNumber = isNumber; /** * Pure function to check if value is string */ const isStringValueRange = (value) => typeof value === 'string'; exports.isStringValueRange = isStringValueRange; /** * Pure function to get string length */ const getStringLength = (value) => value.length; exports.getStringLength = getStringLength; //# sourceMappingURL=RangeValidator.js.map