Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions fields/types/relationship/RelationshipType.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ relationship.prototype.addToSchema = function (schema) {
var def = {
type: this._nativeType,
ref: this.options.ref,
index: (this.options.index ? true : false),
required: (this.options.required ? true : false),
unique: (this.options.unique ? true : false),
index: (this.options.index ? this.options.index : false),
Copy link
Contributor

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 or false and the conditional (ternary) should already be setting these correctly.

Can you clarify the motivation for changing these?

Copy link
Author

@nicolas-guichard nicolas-guichard Jun 27, 2018

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

required: (this.options.required ? this.options.required : false),
unique: (this.options.unique ? this.options.unique : false),
};
this.paths = {
refList: this.options.refListPath || this.path + 'RefList',
Expand Down
1 change: 1 addition & 0 deletions test/models/DependsOn.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DependsOn.add({
title: { type: String, required: true, default: '' },
state: { type: Types.Select, options: 'draft, published, archived', default: 'draft' },
publishedDate: { type: Types.Date, dependsOn: {state: 'published'}, required: true, initial: false },
relativeDependsOn: { type: Types.Relationship, ref: 'DependsOn', dependsOn: {state: 'published'}, required: true, initial: false },
});

DependsOn.register();
57 changes: 44 additions & 13 deletions test/unit/lib/list/dependsOn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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);
Expand All @@ -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;
Expand All @@ -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);

});
});
Expand Down