Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/schema/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down