Skip to content

Commit 94064a6

Browse files
tweak implementation, add test classes and validate they work as planned in test space/env
1 parent edd8bdb commit 94064a6

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module.exports = function (migration) {
2+
migration.createContentType('contentModelA', {
3+
name: 'Content Model A',
4+
description: 'A content model for addItemsValidation'
5+
})
6+
7+
migration.createContentType('contentModelB', {
8+
name: 'Content Model B',
9+
description: 'B content model for addItemsValidation'
10+
})
11+
12+
migration.createContentType('contentModelC', {
13+
name: 'Content Model C',
14+
description: 'C content model for addItemsValidation'
15+
})
16+
17+
const testModel = migration.createContentType('testModel', {
18+
name: 'Test Model',
19+
description: 'A test model for addItemsValidation'
20+
})
21+
22+
testModel.createField('name').name('Name').type('Symbol').required(true)
23+
24+
testModel
25+
.createField('tags')
26+
.name('Tags')
27+
.type('Array')
28+
.items({
29+
type: 'Symbol'
30+
})
31+
.addItemsValidation([{ unique: true }, { size: { min: 1, max: 5 } }])
32+
33+
testModel
34+
.createField('skills')
35+
.name('Skills')
36+
.type('Array')
37+
.items({
38+
type: 'Symbol'
39+
})
40+
.addItemsValidation([{ size: { min: 1 } }])
41+
42+
testModel
43+
.createField('relatedEntries')
44+
.name('Related Entries')
45+
.type('Array')
46+
.items({
47+
type: 'Link',
48+
linkType: 'Entry'
49+
})
50+
.addItemsValidation([{ linkContentType: ['contentModelA', 'contentModelB'] }])
51+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function (migration) {
2+
const testModel = migration.editContentType('testModel')
3+
4+
// Update the relatedEntries's items validations using addItemsValidation to add contentModelC
5+
testModel.editField('relatedEntries').addItemsValidation([{ linkContentType: ['contentModelC'] }])
6+
}

src/lib/action/field-add-items-validation.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,39 @@ class FieldAddItemsValidationAction extends FieldAction {
2222
}
2323

2424
// Merge new validations with existing ones
25-
field.items.validations = [...field.items.validations, ...this.validations]
25+
const existingValidations = field.items.validations || []
26+
const newValidations = this.validations || []
27+
28+
// Handle linkContentType validations specially
29+
const existingLinkContentType = existingValidations.find((v) => v.linkContentType)
30+
const newLinkContentType = newValidations.find((v) => v.linkContentType)
31+
32+
if (existingLinkContentType && newLinkContentType) {
33+
// Merge linkContentType arrays
34+
const mergedContentTypes = [
35+
...new Set([
36+
...(existingLinkContentType.linkContentType || []),
37+
...(newLinkContentType.linkContentType || [])
38+
])
39+
]
40+
41+
// Remove both old validations
42+
const filteredExisting = existingValidations.filter((v) => !v.linkContentType)
43+
const filteredNew = newValidations.filter((v) => !v.linkContentType)
44+
45+
// Add merged validation
46+
field.items.validations = [
47+
...filteredExisting,
48+
...filteredNew,
49+
{ linkContentType: mergedContentTypes }
50+
]
51+
} else {
52+
// If no linkContentType to merge, just append
53+
field.items.validations = [...existingValidations, ...newValidations]
54+
}
2655

2756
fields.setField(this.getFieldId(), field)
2857
}
2958
}
3059

31-
export { FieldAddItemsValidationAction }
60+
export { FieldAddItemsValidationAction }

0 commit comments

Comments
 (0)