Skip to content

Commit

Permalink
fix: improve err msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Jan 13, 2020
1 parent 54f2a9d commit 6ff6e23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
Expand Up @@ -221,7 +221,7 @@ export function hasManyRelationAcceptance(
],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Customer" property "orders"). Please remove it or make sure the relation name is not the same as the property name in belongsTo relation.',
'Navigational properties are not allowed in model data (model "Customer" property "orders"), please remove it.',
);
});

Expand All @@ -238,7 +238,7 @@ export function hasManyRelationAcceptance(
},
]),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Customer" property "orders"). Please remove it or make sure the relation name is not the same as the property name in belongsTo relation.',
'Navigational properties are not allowed in model data (model "Customer" property "orders"), please remove it.',
);
});

Expand Down Expand Up @@ -269,9 +269,7 @@ export function hasManyRelationAcceptance(
name: 'Nintendo',
orders: [{description: 'Switch'}],
}),
).to.be.rejectedWith(
/Navigational properties are not allowed.*"orders".*/,
);
).to.be.rejectedWith(/Navigational properties are not allowed.*"orders"/);
});

it('throws when the instance contains navigational property when operates updateById()', async () => {
Expand All @@ -282,9 +280,7 @@ export function hasManyRelationAcceptance(
name: 'Luigi',
orders: [{description: 'Nintendo'}],
}),
).to.be.rejectedWith(
/Navigational properties are not allowed.*"orders".* /,
);
).to.be.rejectedWith(/Navigational properties are not allowed.*"orders"/);
});

it('throws when the instance contains navigational property when operates delete()', async () => {
Expand All @@ -300,7 +296,7 @@ export function hasManyRelationAcceptance(
});

await expect(customerRepo.delete(found)).to.be.rejectedWith(
`Navigational properties are not allowed in model data (model "Customer" property "orders"). Please remove it or make sure the relation name is not the same as the property name in belongsTo relation.`,
'Navigational properties are not allowed in model data (model "Customer" property "orders"), please remove it.',
);
});

Expand Down
Expand Up @@ -351,9 +351,20 @@ describe('DefaultCrudRepository', () => {
folderId: number;
}

@model()
class Account extends Entity {
@property({id: true})
id?: number;
@property()
name: string;
@belongsTo(() => Author, {name: 'author'})
author: number;
}

let folderRepo: DefaultCrudRepository<Folder, unknown, {}>;
let fileRepo: DefaultCrudRepository<File, unknown, {}>;
let authorRepo: DefaultCrudRepository<Author, unknown, {}>;
let accountRepo: DefaultCrudRepository<Account, unknown, {}>;

let folderFiles: HasManyRepositoryFactory<File, typeof Folder.prototype.id>;
let fileFolder: BelongsToAccessor<Folder, typeof File.prototype.id>;
Expand All @@ -370,6 +381,7 @@ describe('DefaultCrudRepository', () => {
folderRepo = new DefaultCrudRepository(Folder, ds);
fileRepo = new DefaultCrudRepository(File, ds);
authorRepo = new DefaultCrudRepository(Author, ds);
accountRepo = new DefaultCrudRepository(Account, ds);
});

before(() => {
Expand Down Expand Up @@ -461,18 +473,37 @@ describe('DefaultCrudRepository', () => {
});
});

it('throws if the target data passes to CRUD methods contains nav properties', async () => {
// a unit test for entityToData, which is invoked by create() method
// it would be the same of other CRUD methods.
await expect(
folderRepo.create({
name: 'f1',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files"). Please remove it or make sure the relation name is not the same as the property name in belongsTo relation.',
);
});
context(
'throws if the target data passes to CRUD methods contains nav properties',
() => {
it('error message warns about the nav properties', async () => {
// a unit test for entityToData, which is invoked by create() method
// it would be the same of other CRUD methods.
await expect(
folderRepo.create({
name: 'f1',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files"), please remove it.',
);
});

it('error msg also warns about property and relation names in belongsTo relation', async () => {
// the belongsTo relation has the same property name as the relation name.
await expect(
accountRepo.create({
name: 'acoount 1',
author: 1, // same as the relation name
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Account" property "author"), please remove it.' +
' The error might be invoked by belongsTo relations, please make sure the relation name is not the same as' +
' the property name.',
);
});
},
);

// stub resolvers
const hasManyResolver: InclusionResolver<
Expand Down
11 changes: 10 additions & 1 deletion packages/repository/src/repositories/legacy-juggler-bridge.ts
Expand Up @@ -579,11 +579,20 @@ export class DefaultCrudRepository<
const data: AnyObject = new this.entityClass(entity);

const def = this.entityClass.definition;
const props = def.properties;
for (const r in def.relations) {
const relName = def.relations[r].name;
if (relName in data) {
let invalidName = '';
if (relName in props) {
invalidName =
` The error might be invoked by belongsTo relations, please make sure the relation name is not the same as` +
` the property name.`;
}
throw new Error(
`Navigational properties are not allowed in model data (model "${this.entityClass.modelName}" property "${relName}"). Please remove it or make sure the relation name is not the same as the property name in belongsTo relation.`,
`Navigational properties are not allowed in model data (model "${this.entityClass.modelName}"` +
` property "${relName}"), please remove it.` +
invalidName,
);
}
}
Expand Down

0 comments on commit 6ff6e23

Please sign in to comment.