update items.validtion to allow single entry inserts to existing array
Summary
Implements fix for issue: https://github.com/contentful/contentful-migration/issues/1166
When updating the valid Content Types for a reference Content Model field, you cannot just insert or add a single entry as validations will completely write over the existing entries. This requires copying and pasting the list which introduces the risk that someone might do it wrong and wipe out needed values.
Instead, we should be able to just add/insert a new value to the array, reducing risk and headache.
Description
implements a new method addItemsValidation that allows you to update existing validations on items without completely writing over the existing values.
Motivation and Context
Given an existing model defined as:
testModel
.createField('relatedEntries')
.name('Related Entries')
.type('Array')
.items({
type: 'Link',
linkType: 'Entry',
validations: [{ linkContentType: ['contentModelA', 'contentModelB']}]
})
Today if we wanted to add contentModelC to this list of validations, we must redefine the entire array:
testModel
.editField('relatedEntries')
.items({
type: 'Link',
linkType: 'Entry',
validations: [{ linkContentType: ['contentModelA', 'contentModelB', 'contentModelC']}]
})
Instead it would be desirable to be able to add a single entry:
const testModel = migration.editContentType('testModel')
testModel
.editField('relatedEntries')
.addItemsValidation([{ linkContentType: ['contentModelC'] }])
Todos
None
Screenshots
SUPPORT. This is great.
Hey. Thank you for making this commit. Our team is taking a look at it and will let you know if we have any questions.
Just a quick update: still taking a look at this, will have more concrete feedback/questions/approvals in the next 2 weeks, hopefully sooner.
In the meantime I'll run the circle CI tests.
Thanks for the patience.
Looks like there might be some failing tests?
// npm run test:unit
337 passing (538ms)
1 pending
3 failing
1) FieldAddItemsValidationIntent
when adding validations to array field items
adds validations to existing items validations:
TypeError: callsite.getFileName is not a function
at Object.addItemsValidation (src/lib/migration-steps/action-creators.ts:739:29)
at Context.<anonymous> (test/unit/lib/intent/field-add-items-validation.spec.ts:12:74)
at processImmediate (node:internal/timers:476:21)
2) FieldAddItemsValidationIntent
when adding validations to array field items
creates items validations array if it does not exist:
TypeError: callsite.getFileName is not a function
at Object.addItemsValidation (src/lib/migration-steps/action-creators.ts:739:29)
at Context.<anonymous> (test/unit/lib/intent/field-add-items-validation.spec.ts:51:74)
at processImmediate (node:internal/timers:476:21)
3) FieldAddItemsValidationIntent
when adding validations to array field items
creates items object if it does not exist:
TypeError: callsite.getFileName is not a function
at Object.addItemsValidation (src/lib/migration-steps/action-creators.ts:739:29)
at Context.<anonymous> (test/unit/lib/intent/field-add-items-validation.spec.ts:88:74)
at processImmediate (node:internal/timers:476:21)
Exited with code exit status 3
// npm run lint
> [email protected] lint
> eslint 'examples/**/*.js' 'test/**/*.js' 'src/**/*.js' && tslint --project tsconfig-dev.json --config tslint.json -e '**/*.js'
strict-type-predicates does not work without --strictNullChecks
/home/circleci/project/test/unit/lib/actions/field-add-items-validation.spec.ts:89:7
ERROR: 89:7 no-unused-expression unused expression, expected an assignment or function call
/home/circleci/project/test/unit/lib/intent/field-add-items-validation.spec.ts:117:7
ERROR: 117:7 no-unused-expression unused expression, expected an assignment or function call
// npm run test:integration
47 passing (57s)
1 failing
1) the migration
updates items validation for existing field:
AssertionError: expected [ { linkContentType: [ …(3) ] } ] to deep include { linkContentType: [ 'contentModelC' ] }
at Context.<anonymous> (test/integration/migration.spec.js:1466:59)
at processTicksAndRejections (node:internal/process/task_queues:95:5)