/** * Insertr Validation Module * Client-side validation for user experience (not security) */ class InsertrValidation { constructor(config, domPurify = null) { this.config = config; this.DOMPurify = domPurify; this.limits = config.getValidationLimits(); } /** * Validate input based on field type * @param {string} input - Input to validate * @param {string} fieldType - Type of field * @returns {Object} Validation result */ validateInput(input, fieldType) { if (!input || typeof input !== 'string') { return { valid: false, message: 'Content cannot be empty' }; } // Basic length validation if (input.length > this.limits.maxContentLength) { return { valid: false, message: `Content is too long (max ${this.limits.maxContentLength.toLocaleString()} characters)` }; } // Field-specific validation switch (fieldType) { case 'text': return this.validateTextInput(input); case 'textarea': return this.validateTextInput(input); case 'link': return this.validateLinkInput(input); case 'markdown': return this.validateMarkdownInput(input); default: return { valid: true }; } } /** * Validate plain text input * @param {string} input - Text to validate * @returns {Object} Validation result */ validateTextInput(input) { // Check for obvious HTML that users might accidentally include if (input.includes('')) { return { valid: false, message: 'Script tags are not allowed for security reasons' }; } if (input.includes('<') && input.includes('>')) { return { valid: false, message: 'HTML tags are not allowed in text fields. Use markdown collections for formatted content.' }; } return { valid: true }; } /** * Validate link/URL input * @param {string} input - URL to validate * @returns {Object} Validation result */ validateLinkInput(input) { // Basic URL validation for user feedback const urlPattern = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/; if (input.startsWith('http') && !urlPattern.test(input)) { return { valid: false, message: 'Please enter a valid URL (e.g., https://example.com)' }; } return { valid: true }; } /** * Validate markdown input * @param {string} input - Markdown to validate * @returns {Object} Validation result */ validateMarkdownInput(input) { // Check for potentially problematic content if (input.includes('