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
103 changes: 103 additions & 0 deletions spec/MongoSchemaCollectionAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,107 @@ describe('MongoSchemaCollection', () => {
});
done();
});

describe('mongoFieldToParseSchemaField function', () => {
// Test successful type conversions
it('should convert valid mongo field types to parse schema fields', () => {
const testCases = [
{ input: 'string', expected: { type: 'String' } },
{ input: 'number', expected: { type: 'Number' } },
{ input: 'boolean', expected: { type: 'Boolean' } },
{ input: 'date', expected: { type: 'Date' } },
{ input: 'map', expected: { type: 'Object' } },
{ input: 'object', expected: { type: 'Object' } },
{ input: 'array', expected: { type: 'Array' } },
{ input: 'geopoint', expected: { type: 'GeoPoint' } },
{ input: 'file', expected: { type: 'File' } },
{ input: 'bytes', expected: { type: 'Bytes' } },
{ input: 'polygon', expected: { type: 'Polygon' } },
{ input: '*_User', expected: { type: 'Pointer', targetClass: '_User' } },
{ input: '*Post', expected: { type: 'Pointer', targetClass: 'Post' } },
{ input: 'relation<_User>', expected: { type: 'Relation', targetClass: '_User' } },
{ input: 'relation<Post>', expected: { type: 'Relation', targetClass: 'Post' } },
];

testCases.forEach(({ input, expected }) => {
const result = MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'TestClass',
testField: input,
});

expect(result.fields.testField).toEqual(expected);
});
});

// Test error handling for invalid types (non-string values)
it('should throw error for invalid field types', () => {
const invalidInputs = [
null,
undefined,
123,
true,
false,
{},
[],
'',
];

invalidInputs.forEach(invalidInput => {
expect(() => {
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'TestClass',
testField: invalidInput,
});
}).toThrow();
});
});

it('should throw error with correct message for null input', () => {
try {
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'TestClass',
testField: null,
});
} catch (error) {
expect(error.code).toBe(255);
expect(error.message).toContain('Invalid field type');
}
});

it('should throw error with correct message for undefined input', () => {
try {
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'TestClass',
testField: undefined,
});
} catch (error) {
expect(error.code).toBe(255);
expect(error.message).toContain('Invalid field type');
}
});

it('should throw error with correct message for non-string input', () => {
try {
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'TestClass',
testField: 123,
});
} catch (error) {
expect(error.code).toBe(255);
expect(error.message).toContain('Invalid field type');
}
});

it('should throw error with correct message for empty string input', () => {
try {
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'TestClass',
testField: '',
});
} catch (error) {
expect(error.code).toBe(255);
expect(error.message).toContain('Invalid field type');
}
});
});
});
8 changes: 8 additions & 0 deletions src/Adapters/Storage/Mongo/MongoSchemaCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import MongoCollection from './MongoCollection';
import Parse from 'parse/node';

function mongoFieldToParseSchemaField(type) {
// Add type validation to prevent TypeError
if (!type || typeof type !== 'string') {
throw new Parse.Error(
Parse.Error.INVALID_SCHEMA_OPERATION,
`Invalid field type: ${type}. Expected a string. Fix the type mismatch in your schema configuration.`
Comment on lines +8 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: to help developer to identity the field, any chance to mention the impacted field and current class name ?

);
}

if (type[0] === '*') {
return {
type: 'Pointer',
Expand Down