Skip to main content
Glama
ValidationService-fields-and-emails.test.tsβ€’4.88 kB
/** * Split: ValidationService fields and email validations */ import { describe, it, expect, vi, beforeEach } from 'vitest'; vi.mock('../../src/utils/validation/email-validation.js', () => ({ isValidEmail: vi.fn(), })); vi.mock('../../src/utils/validation.js', () => ({ isValidId: vi.fn() })); vi.mock('../../src/utils/validation/uuid-validation.js', () => ({ isValidUUID: vi.fn(), createInvalidUUIDError: vi.fn(), })); vi.mock('../../src/middleware/performance-enhanced.js', () => ({ enhancedPerformanceTracker: { markTiming: vi.fn(), endOperation: vi.fn() }, })); vi.mock('../../src/handlers/tool-configs/universal/field-mapper.js', () => ({ validateFields: vi.fn(), FIELD_MAPPINGS: { companies: { validFields: ['name', 'domain', 'industry'] }, people: { validFields: ['first_name', 'last_name', 'email_addresses'] }, }, })); import { ValidationService } from '../../src/services/ValidationService.js'; import { UniversalResourceType } from '../../src/handlers/tool-configs/universal/types.js'; import { validateFields } from '../../src/handlers/tool-configs/universal/field-mapper.js'; import { isValidEmail } from '../../src/utils/validation/email-validation.js'; describe('ValidationService', () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(validateFields).mockClear(); }); describe('validateFieldsWithErrorHandling', () => { it('should pass for valid field validation', () => { vi.mocked(validateFields).mockReturnValue({ valid: true, errors: [], warnings: [], suggestions: [], } as any); const result = ValidationService.validateFieldsWithErrorHandling( UniversalResourceType.COMPANIES, { name: 'Test Company' }, false ); expect(result.valid).toBe(true); expect(result.errorMessage).toBeUndefined(); }); it('should return error message for invalid validation when not throwing', () => { vi.mocked(validateFields).mockReturnValue({ valid: false, errors: ['Name is required'], warnings: ['Domain should be provided'], suggestions: ['Try using a valid company name'], } as any); const result = ValidationService.validateFieldsWithErrorHandling( UniversalResourceType.COMPANIES, {}, false ); expect(result.valid).toBe(false); expect(result.errorMessage).toContain( 'Field validation failed for companies' ); expect(result.errorMessage).toContain('❌ Name is required'); expect(result.errorMessage).toContain('πŸ’‘ Suggestions:'); expect(result.errorMessage).toContain('Available fields for companies:'); }); it('should throw UniversalValidationError for invalid validation when throwing enabled', () => { vi.mocked(validateFields).mockReturnValue({ valid: false, errors: ['Required field missing'], warnings: [], suggestions: ['Add required field'], } as any); expect(() => { ValidationService.validateFieldsWithErrorHandling( UniversalResourceType.PEOPLE, {}, true ); }).toThrow(); }); it('should truncate suggestions in error message', () => { const manySuggestions = Array.from( { length: 10 }, (_, i) => `Suggestion ${i + 1}` ); vi.mocked(validateFields).mockReturnValue({ valid: false, errors: ['Error'], warnings: [], suggestions: manySuggestions, } as any); const result = ValidationService.validateFieldsWithErrorHandling( UniversalResourceType.COMPANIES, {}, false ); expect(result.errorMessage).toContain('... and 7 more suggestions'); }); }); describe('validateEmailAddresses', () => { it('should validate complex email structures and throw on invalid formats', () => { const recordData = { email_addresses: [ { value: 'valid@email.com' }, { value: 'invalid-email' }, null, { nested: { value: 'another@email.com' } }, ], } as any; vi.mocked(isValidEmail).mockImplementation((v: string) => /@/.test(v)); expect(() => { ValidationService.validateEmailAddresses(recordData); }).toThrow(/Invalid email format/i); // Called on both valid and invalid string emails expect(isValidEmail).toHaveBeenCalledWith('valid@email.com'); expect(isValidEmail).toHaveBeenCalledWith('invalid-email'); }); it('should handle null or undefined record data', () => { expect(() => { ValidationService.validateEmailAddresses(null as any); ValidationService.validateEmailAddresses(undefined as any); ValidationService.validateEmailAddresses('not-an-object' as any); }).not.toThrow(); }); }); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kesslerio/attio-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server