-
Couldn't load subscription status.
- Fork 159
update items.validtion to allow single entry inserts to existing array #1507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eric-foster-angi
wants to merge
2
commits into
contentful:main
Choose a base branch
from
eric-foster-angi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+553
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] }]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] }]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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 } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: [] | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
field would only have a
itemsproperty if it is of typereferenceormedia. So we might want to add one more check here to make sure we don't unnecessarily add the items field to a field where it wouldn't belong. Something like: