Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 1x | import { PromptTemplate, ValidationRule, ChainContext } from '../types';
interface ValidationResult {
templateId: string;
passed: boolean;
checks: ValidationCheck[];
}
interface ValidationCheck {
rule: ValidationRule;
passed: boolean;
message: string;
autoFixed: boolean;
}
export class PromptValidator {
validateTemplate(template: PromptTemplate, context: ChainContext): ValidationResult {
const checks: ValidationCheck[] = [];
for (const rule of template.validationRules) {
const check = this.evaluateRule(rule, template, context);
checks.push(check);
}
return {
templateId: template.id,
passed: checks.every((c) => c.passed),
checks,
};
}
private evaluateRule(
rule: ValidationRule,
template: PromptTemplate,
context: ChainContext
): ValidationCheck {
switch (rule.type) {
case 'syntax':
return this.validateSyntax(template);
case 'security':
return this.validateSecurity(template, context);
case 'accessibility':
return this.validateAccessibility(template);
case 'performance':
return this.validatePerformance(template, context);
case 'style':
return this.validateStyle(template);
default:
return { rule, passed: true, message: 'Unknown rule type', autoFixed: false };
}
}
private validateSyntax(template: PromptTemplate): ValidationCheck {
const rule = template.validationRules.find((r) => r.type === 'syntax')!;
const hasPlaceholders = /\{\{.*?\}\}/g.test(template.template);
const hasValidSlots = template.slots.every(
(s) => !s.required || template.template.includes(`{{${s.name}}}`)
);
return {
rule,
passed: hasValidSlots,
message: hasValidSlots
? 'Syntax valid: all required slots present'
: 'Syntax error: missing required slot placeholders',
autoFixed: false,
};
}
private validateSecurity(
template: PromptTemplate,
context: ChainContext
): ValidationCheck {
const rule = template.validationRules.find((r) => r.type === 'security')!;
const templateText = template.template.toLowerCase();
const hasSecurePatterns = [
'sanitiz',
'hash',
'encrypt',
'https',
'rate limit',
'csrf',
'cors',
'csp',
'helmet',
'input validation',
].some((p) => templateText.includes(p));
return {
rule,
passed: hasSecurePatterns,
message: hasSecurePatterns
? 'Security patterns detected in template'
: 'Warning: No explicit security patterns found',
autoFixed: !hasSecurePatterns,
};
}
private validateAccessibility(template: PromptTemplate): ValidationCheck {
const rule = template.validationRules.find((r) => r.type === 'accessibility')!;
const templateText = template.template.toLowerCase();
const hasA11y = [
'aria',
'keyboard',
'focus',
'sr-only',
'alt',
'role=',
'wcag',
'contrast',
].some((p) => templateText.includes(p));
return {
rule,
passed: hasA11y,
message: hasA11y
? 'Accessibility requirements present'
: 'Warning: Accessibility requirements may be missing',
autoFixed: false,
};
}
private validatePerformance(
template: PromptTemplate,
context: ChainContext
): ValidationCheck {
const rule = template.validationRules.find((r) => r.type === 'performance')!;
const templateText = template.template.toLowerCase();
const hasPerf = [
'lazy',
'cache',
'bundle',
'optimize',
'compress',
'minify',
'tree shake',
'cdn',
].some((p) => templateText.includes(p));
return {
rule,
passed: hasPerf,
message: hasPerf ? 'Performance optimizations referenced' : 'Performance considerations may be absent',
autoFixed: false,
};
}
private validateStyle(template: PromptTemplate): ValidationCheck {
const rule = template.validationRules.find((r) => r.type === 'style')!;
return {
rule,
passed: true,
message: 'Style validation passed',
autoFixed: false,
};
}
validateApiContract(apiSpec: string, frontendCalls: string[]): { match: boolean; gaps: string[] } {
const gaps: string[] = [];
for (const call of frontendCalls) {
Iif (!apiSpec.includes(call)) {
gaps.push(`No matching endpoint for frontend call: ${call}`);
}
}
return { match: gaps.length === 0, gaps };
}
}
|