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
51 changes: 51 additions & 0 deletions examples/56-add-items-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = function (migration) {
migration.createContentType('contentModelA', {
name: 'Content Model A',
description: 'A content model for addItemsValidation'
})

migration.createContentType('contentModelB', {
name: 'Content Model B',
description: 'B content model for addItemsValidation'
})

migration.createContentType('contentModelC', {
name: 'Content Model C',
description: 'C content model for addItemsValidation'
})

const testModel = migration.createContentType('testModel', {
name: 'Test Model',
description: 'A test model for addItemsValidation'
})

testModel.createField('name').name('Name').type('Symbol').required(true)

testModel
.createField('tags')
.name('Tags')
.type('Array')
.items({
type: 'Symbol'
})
.addItemsValidation([{ unique: true }, { size: { min: 1, max: 5 } }])

testModel
.createField('skills')
.name('Skills')
.type('Array')
.items({
type: 'Symbol'
})
.addItemsValidation([{ size: { min: 1 } }])

testModel
.createField('relatedEntries')
.name('Related Entries')
.type('Array')
.items({
type: 'Link',
linkType: 'Entry'
})
.addItemsValidation([{ linkContentType: ['contentModelA', 'contentModelB'] }])
}
6 changes: 6 additions & 0 deletions examples/57-add-items-validation-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (migration) {
const testModel = migration.editContentType('testModel')

// Update the relatedEntries's items validations using addItemsValidation to add contentModelC
testModel.editField('relatedEntries').addItemsValidation([{ linkContentType: ['contentModelC'] }])
}
46 changes: 15 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,6 @@ const createRun = ({ shouldThrow }) =>
console.warn(chalk`⚠️ {bold.yellow Migration aborted}`)
}
}

export const runMigration = createRun({ shouldThrow: true })
export default createRun({ shouldThrow: false })

60 changes: 60 additions & 0 deletions src/lib/action/field-add-items-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { FieldAction } from './field-action'
import ContentType from '../entities/content-type'

class FieldAddItemsValidationAction extends FieldAction {
protected validations: any[]

constructor(contentTypeId: string, fieldId: string, validations: any[]) {
super(contentTypeId, fieldId)
this.validations = validations
}

async applyTo(ct: ContentType) {
const fields = ct.fields
const field = fields.getField(this.getFieldId())

if (!field.items && ['Array', 'Link'].includes(field.type)) {
field.items = { type: 'Symbol' } // Default type for array items
}

if (!field.items.validations) {
field.items.validations = []
}

// Merge new validations with existing ones
const existingValidations = field.items.validations || []
const newValidations = this.validations || []

// Handle linkContentType validations specially
const existingLinkContentType = existingValidations.find((v) => v.linkContentType)
const newLinkContentType = newValidations.find((v) => v.linkContentType)

if (existingLinkContentType && newLinkContentType) {
// Merge linkContentType arrays
const mergedContentTypes = [
...new Set([
...(existingLinkContentType.linkContentType || []),
...(newLinkContentType.linkContentType || [])
])
]

// Remove both old validations
const filteredExisting = existingValidations.filter((v) => !v.linkContentType)
const filteredNew = newValidations.filter((v) => !v.linkContentType)

// Add merged validation
field.items.validations = [
...filteredExisting,
...filteredNew,
{ linkContentType: mergedContentTypes }
]
} else {
// If no linkContentType to merge, just append
field.items.validations = [...existingValidations, ...newValidations]
}

fields.setField(this.getFieldId(), field)
}
}

export { FieldAddItemsValidationAction }
4 changes: 4 additions & 0 deletions src/lib/intent/base-intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export default abstract class Intent implements IntentInterface {
return false
}

isFieldAddItemsValidation() {
return false
}

isContentTransform() {
return false
}
Expand Down
56 changes: 56 additions & 0 deletions src/lib/intent/field-add-items-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Intent from './base-intent'
import { FieldAddItemsValidationAction } from '../action/field-add-items-validation'
import { PlanMessage } from '../interfaces/plan-message'
import chalk from 'chalk'
import { RawStep } from '../interfaces/raw-step'

export default class FieldAddItemsValidationIntent extends Intent {
constructor(rawStep: RawStep) {
super(rawStep)
}

isFieldAddItemsValidation() {
return true
}

groupsWith(other: Intent): boolean {
const sameContentType = other.getContentTypeId() === this.getContentTypeId()
return (
(other.isContentTypeUpdate() ||
other.isContentTypeCreate() ||
other.isContentTypeAnnotate() ||
other.isFieldCreate() ||
other.isFieldUpdate() ||
other.isFieldMove() ||
other.isFieldAddItemsValidation()) &&
sameContentType
)
}

endsGroup(): boolean {
return false
}

toActions() {
return [
new FieldAddItemsValidationAction(
this.getContentTypeId(),
this.getFieldId(),
this.payload.props.validations
)
]
}

toPlanMessage(): PlanMessage {
return {
heading: chalk`Update Content Type {bold.yellow ${this.getContentTypeId()}}`,
sections: [
{
heading: chalk`Add items validations to array field {yellow ${this.getFieldId()}}`,
details: [`Validations: ${JSON.stringify(this.payload.props.validations)}`]
}
],
details: []
}
}
}
4 changes: 3 additions & 1 deletion src/lib/intent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import EntrySetTagsIntent from './entry-set-tags'
import EditorLayoutChangeFieldGroupIdIntent from './editor-layout/editor-layout-change-field-group-id'
import EditorLayoutDeleteIntent from './editor-layout/editor-layout-delete'
import FieldAnnotateIntent from './field-annotate'
import FieldAddItemsValidationIntent from './field-add-items-validation'

export {
Intent as default,
Expand Down Expand Up @@ -76,5 +77,6 @@ export {
EntrySetTagsIntent as EntrySetTags,
EditorLayoutMoveFieldIntent as EditorLayoutMoveField,
EditorLayoutChangeFieldGroupIdIntent as EditorLayoutChangeFieldGroupId,
EditorLayoutDeleteIntent as EditorLayoutDelete
EditorLayoutDeleteIntent as EditorLayoutDelete,
FieldAddItemsValidationIntent as FieldAddItemsValidation
}
Loading