Skip to content

Commit 86ff0a4

Browse files
authored
test: field level validation errors (#10614)
Continuation of #10575. Field level validations error were incorrectly throwing uniqueness errors. This was fixed but lacking tests.
1 parent e80d679 commit 86ff0a4

File tree

7 files changed

+219
-91
lines changed

7 files changed

+219
-91
lines changed

packages/db-mongodb/src/utilities/handleError.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ export const handleError = ({
1919

2020
// Handle uniqueness error from MongoDB
2121
if ('code' in error && error.code === 11000 && 'keyValue' in error && error.keyValue) {
22-
const message = req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique'
2322
throw new ValidationError(
2423
{
2524
collection,
2625
errors: [
2726
{
28-
message,
27+
message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',
2928
path: Object.keys(error.keyValue)[0],
3029
},
3130
],

test/collections-rest/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const collectionWithName = (collectionSlug: string): CollectionConfig => {
3434
}
3535
}
3636

37-
export const slug = 'posts'
37+
export const postsSlug = 'posts'
3838
export const relationSlug = 'relation'
3939
export const pointSlug = 'point'
4040
export const customIdSlug = 'custom-id'
@@ -51,7 +51,7 @@ export default buildConfigWithDefaults({
5151
},
5252
collections: [
5353
{
54-
slug,
54+
slug: postsSlug,
5555
access: openAccess,
5656
fields: [
5757
{
@@ -346,14 +346,14 @@ export default buildConfigWithDefaults({
346346

347347
// Relation - hasMany
348348
await payload.create({
349-
collection: slug,
349+
collection: postsSlug,
350350
data: {
351351
relationHasManyField: rel1.id,
352352
title: 'rel to hasMany',
353353
},
354354
})
355355
await payload.create({
356-
collection: slug,
356+
collection: postsSlug,
357357
data: {
358358
relationHasManyField: rel2.id,
359359
title: 'rel to hasMany 2',
@@ -362,7 +362,7 @@ export default buildConfigWithDefaults({
362362

363363
// Relation - relationTo multi
364364
await payload.create({
365-
collection: slug,
365+
collection: postsSlug,
366366
data: {
367367
relationMultiRelationTo: {
368368
relationTo: relationSlug,
@@ -374,7 +374,7 @@ export default buildConfigWithDefaults({
374374

375375
// Relation - relationTo multi hasMany
376376
await payload.create({
377-
collection: slug,
377+
collection: postsSlug,
378378
data: {
379379
relationMultiRelationToHasMany: [
380380
{

0 commit comments

Comments
 (0)