diff --git a/src/schema/schema.ts b/src/schema/schema.ts index 1129034..75ffa7e 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -273,13 +273,17 @@ export class IndexSchema { * schema.removeField('old_field'); * ``` */ - removeField(fieldName: string): void { - if (!Object.hasOwn(this.fields, fieldName)) { - // TODO: log a warning - return; - } - delete this.fields[fieldName]; +removeField(fieldName: string): void { + if (!Object.hasOwn(this.fields, fieldName)) { + const knownFields = Object.keys(this.fields); + console.warn( + `Field '${fieldName}' not found in schema. ` + + `Available fields: ${knownFields.length > 0 ? knownFields.join(', ') : '(none)'}` + ); + return; } + delete this.fields[fieldName]; +} /** * Create an IndexSchema from a plain object. diff --git a/tests/unit/schema/schema.test.ts b/tests/unit/schema/schema.test.ts index f26416d..53cc667 100644 --- a/tests/unit/schema/schema.test.ts +++ b/tests/unit/schema/schema.test.ts @@ -217,6 +217,61 @@ describe('IndexSchema Tests', () => { schema.removeField('non-existent'); }).not.toThrow(); }); + + it('should log warning when removing non-existent field (with known fields)', () => { + const indexInfo = new IndexInfo({ name: 'redisvl-test-index' }); + const schema = new IndexSchema({ index: indexInfo }); + + // Add fields first so warning shows available fields + schema.addField({ name: 'title', type: 'text' }); + schema.addField({ name: 'category', type: 'tag' }); + + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + schema.removeField('non-existent'); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining("Field 'non-existent' not found in schema") + ); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('Available fields: title, category') + ); + + warnSpy.mockRestore(); + }); + + it('should log warning with (none) when removing field from empty schema', () => { + const indexInfo = new IndexInfo({ name: 'redisvl-test-index' }); + const schema = new IndexSchema({ index: indexInfo }); + + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + schema.removeField('any-field'); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining("Field 'any-field' not found in schema") + ); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('Available fields: (none)') + ); + + warnSpy.mockRestore(); + }); + + it('should NOT log warning when removing existing field', () => { + const indexInfo = new IndexInfo({ name: 'redisvl-test-index' }); + const schema = new IndexSchema({ index: indexInfo }); + schema.addField({ name: 'title', type: 'text' }); + + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + schema.removeField('title'); + + expect(warnSpy).not.toHaveBeenCalled(); + expect(schema.fields['title']).toBeUndefined(); + + warnSpy.mockRestore(); + }); }); describe('IndexSchema Field Path Validation', () => {