UNPKG

@diullei/codeguardian

Version:

Open-source developer tool to validate and enforce architectural rules, especially for AI-generated code

72 lines 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AssertPropertyRule = void 0; const core_1 = require("../core"); class AssertPropertyRule extends core_1.AssertionRule { propertyPath; expectedValue; operator; extractPattern; constructor(id, propertyPath, expectedValue, operator = '==', extractPattern) { super(id); this.propertyPath = propertyPath; this.expectedValue = expectedValue; this.operator = operator; this.extractPattern = extractPattern; } async assert(item, _context) { let actualValue = this.getNestedProperty(item, this.propertyPath); if (this.extractPattern && typeof actualValue === 'string') { const match = this.extractPattern.exec(actualValue); if (match && match[1] !== undefined) { const extracted = match[1].replace(/,/g, ''); actualValue = extracted; } else { return false; } } return this.compare(actualValue, this.expectedValue, this.operator); } getNestedProperty(obj, path) { return path.split('.').reduce((curr, prop) => { if (curr && typeof curr === 'object' && prop in curr) { return curr[prop]; } return undefined; }, obj); } compare(actual, expected, operator) { switch (operator) { case '==': return actual == expected; case '!=': return actual != expected; case '>': return Number(actual) > Number(expected); case '<': return Number(actual) < Number(expected); case '>=': return Number(actual) >= Number(expected); case '<=': return Number(actual) <= Number(expected); case 'includes': if (typeof actual === 'string' && typeof expected === 'string') { return actual.includes(expected); } if (Array.isArray(actual)) { return actual.includes(expected); } return false; case 'matches': if (typeof actual === 'string' && typeof expected === 'string') { return new RegExp(expected).test(actual); } return false; default: throw new Error(`Unknown operator: ${operator}`); } } } exports.AssertPropertyRule = AssertPropertyRule; //# sourceMappingURL=AssertPropertyRule.js.map