UNPKG

promptly-ai

Version:

A universal template-based prompt management system for LLM applications

62 lines 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateContext = validateContext; exports.validateRequiredFields = validateRequiredFields; /** * Validate context against validation rules */ function validateContext(context, rules) { const errors = []; for (const rule of rules) { const value = context[rule.field]; // Check if required field is missing if (rule.required && (value === undefined || value === null)) { errors.push(`Required field "${rule.field}" is missing`); continue; } // Skip validation if field is not required and not present if (!rule.required && (value === undefined || value === null)) { continue; } // Type validation if (rule.type && !validateType(value, rule.type)) { errors.push(`Field "${rule.field}" must be of type ${rule.type}, got ${typeof value}`); } // Custom validator if (rule.validator && !rule.validator(value)) { errors.push(`Field "${rule.field}" failed custom validation`); } } if (errors.length > 0) { throw new Error(`Validation failed:\n${errors.join('\n')}`); } } /** * Validate if value matches expected type */ function validateType(value, expectedType) { switch (expectedType) { case 'string': return typeof value === 'string'; case 'number': return typeof value === 'number' && !isNaN(value); case 'boolean': return typeof value === 'boolean'; case 'array': return Array.isArray(value); case 'object': return (typeof value === 'object' && value !== null && !Array.isArray(value)); default: return true; } } /** * Validate required fields are present in context */ function validateRequiredFields(context, requiredFields) { const missing = requiredFields.filter((field) => !(field in context) || context[field] === undefined); if (missing.length > 0) { throw new Error(`Missing required context fields: ${missing.join(', ')}`); } } //# sourceMappingURL=validation.js.map