This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[FIX] Make required respect dependsOn on Relationship fields too #4685
Open
nicolas-guichard
wants to merge
2
commits into
keystonejs:master
Choose a base branch
from
nicolas-guichard:master
base: master
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -10,26 +10,24 @@ var DependsOn = keystone.list('DependsOn'); | |
| describe('Test dependsOn and required', function () { | ||
|
|
||
| it('Ignore required if evalDependsOn is not `true` by setting `state` to `draft`', function (done) { | ||
| // remove any Post documents | ||
| // remove any DependsOn documents | ||
| DependsOn.model.find({}).remove(function (error) { | ||
| if (error) { | ||
| done(error); | ||
| } | ||
|
|
||
| var newPost = new DependsOn.model({ | ||
| var newDependsOn = new DependsOn.model({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be an arbitrary rename without changing any functionality for relationships.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I should have done a separate commit for the renaming, which is there to clarify that this test does not use the Post model. The new test case is lines 58-84: it fails without commit 4e27b7b and passes with it. |
||
| title: 'new post', | ||
| state: 'draft' | ||
| }); | ||
|
|
||
| newPost.save(done); | ||
| newDependsOn.save(done); | ||
|
|
||
| }); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| it('Save will fail if `state` set to `published` and `publishedDate` is not defined', function (done) { | ||
| // remove any Post documents | ||
| // remove any DependsOn documents | ||
| DependsOn.model.find({}).remove(function (error) { | ||
| if (error) { | ||
| done(error); | ||
|
|
@@ -39,13 +37,43 @@ describe('Test dependsOn and required', function () { | |
| const backupLog = console.error; | ||
| console.error = () => null; | ||
|
|
||
| var newPost = new DependsOn.model({ | ||
| var newDependsOn = new DependsOn.model({ | ||
| title: 'new post', | ||
| state: 'published', | ||
| publishedDate: undefined, | ||
| }); | ||
|
|
||
| newDependsOn.relativeDependsOn = newDependsOn._id; | ||
|
|
||
| newDependsOn.save(function (err) { | ||
| demand(err).be.a.object(); | ||
|
|
||
| console.error = backupLog; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| it('Save will fail if `state` set to `published` and `relativeDependsOn` is not defined', function (done) { | ||
| // remove any DependsOn documents | ||
| DependsOn.model.find({}).remove(function (error) { | ||
| if (error) { | ||
| done(error); | ||
| } | ||
|
|
||
| // suppressing console log output | ||
| const backupLog = console.error; | ||
| console.error = () => null; | ||
|
|
||
| var newDependsOn = new DependsOn.model({ | ||
| title: 'new post', | ||
| state: 'published', | ||
| publishedDate: new Date(), | ||
| relativeDependsOn: undefined, | ||
| }); | ||
|
|
||
| newPost.save(function (err) { | ||
| newDependsOn.save(function (err) { | ||
| demand(err).be.a.object(); | ||
|
|
||
| console.error = backupLog; | ||
|
|
@@ -55,20 +83,23 @@ describe('Test dependsOn and required', function () { | |
|
|
||
| }); | ||
|
|
||
| it('Save will succeed if `state` set to `published` and `publishedDate` is defined', function (done) { | ||
| it('Save will succeed if `state` set to `published` and `publishedDate` and `relativeDependsOn` are defined', function (done) { | ||
|
|
||
| // remove any Post documents | ||
| // remove any DependsOn documents | ||
| DependsOn.model.find({}).remove(function (error) { | ||
| if (error) { | ||
| done(error); | ||
| } | ||
|
|
||
| var newPost = new DependsOn.model({ | ||
| var newDependsOn = new DependsOn.model({ | ||
| title: 'new post', | ||
| state: 'published', | ||
| publishedDate: new Date() | ||
| publishedDate: new Date(), | ||
| }); | ||
| newPost.save(done); | ||
|
|
||
| newDependsOn.relativeDependsOn = newDependsOn._id; | ||
|
|
||
| newDependsOn.save(done); | ||
|
|
||
| }); | ||
| }); | ||
|
|
||
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.
The values for these 3 options (index, required, and unique) are either
trueorfalseand the conditional (ternary) should already be setting these correctly.Can you clarify the motivation for changing these?
Uh oh!
There was an error while loading. Please reload this page.
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.
The desired values are not necessarily true or false, in the case described by keystonejs/keystone#2200, if dependsOn is set, this.option.required may actually be a function (see below), which is then used by Mongoose. I changed the three of them for consistency.
https://github.com/keystonejs/keystone/blob/d34f45662eb359e2cb18b397f2ffea21f9883141/fields/types/Type.js#L74