-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[FIX] Make required respect dependsOn on Relationship fields too #4685
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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({ | ||
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. 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); | ||
|
||
}); | ||
}); | ||
|
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
true
orfalse
and the conditional (ternary) should already be setting these correctly.Can you clarify the motivation for changing these?
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