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

Check for title/slug field on config load. #1203

Merged
merged 7 commits into from
May 25, 2018
Merged

Conversation

tech4him1
Copy link
Contributor

@tech4him1 tech4him1 commented Mar 27, 2018

This PR fixes #690 by verifying that folder-type collections have a valid title ("slug"-able) field at CMS load time, instead of when the user tries to save an entry. It also explains how to fix the error.

@verythorough
Copy link
Contributor

verythorough commented Mar 27, 2018

Deploy preview for netlify-cms-www ready!

Built with commit 4044766

https://deploy-preview-1203--netlify-cms-www.netlify.com

@verythorough
Copy link
Contributor

verythorough commented Mar 27, 2018

Deploy preview for cms-demo ready!

Built with commit 4044766

https://deploy-preview-1203--cms-demo.netlify.com

@tech4him1 tech4him1 changed the title Clarify "Invalid entry identifier" message WIP: Clarify "Invalid entry identifier" message Mar 27, 2018
@tech4him1 tech4him1 force-pushed the clarify-title-necessary branch 2 times, most recently from d0c907d to f054994 Compare March 27, 2018 20:49
@erquhart
Copy link
Contributor

Sweet! Is the WIP label still accurate?

@tech4him1
Copy link
Contributor Author

@erquhart I haven't tested it much yet. Also, is this, in your opinion, too big of a change to the backend, or is it good to merge?

@erquhart
Copy link
Contributor

It's mostly a refactor, just porting the identifier logic out to a selector, which makes sense, and improving the error message. I haven't given it a thorough review, but it seems merge ready to me.

@tech4him1 tech4him1 changed the title WIP: Clarify "Invalid entry identifier" message Clarify "Invalid entry identifier" message Mar 28, 2018
@tech4him1 tech4him1 force-pushed the clarify-title-necessary branch 2 times, most recently from fec276b to 09f4d73 Compare March 28, 2018 18:31
@tech4him1 tech4him1 changed the title Clarify "Invalid entry identifier" message WIP: Clarify "Invalid entry identifier" message Mar 28, 2018
@tech4him1
Copy link
Contributor Author

This is going to need some more work before merge.

@tech4him1 tech4him1 changed the title WIP: Clarify "Invalid entry identifier" message Clarify "Invalid entry identifier" message Mar 28, 2018
@tech4him1
Copy link
Contributor Author

@erquhart I think I've fixed all the issues, but it probably deserves a pretty thorough review before merge -- the logic gets changed up quite a bit, it's not just a copy/paste.

@tech4him1 tech4him1 changed the title Clarify "Invalid entry identifier" message Check for title/slug field on config load. Mar 29, 2018
@tech4him1
Copy link
Contributor Author

Ready for review reminder 😆

@erquhart
Copy link
Contributor

Alright I've got it queued.

Copy link
Contributor

@erquhart erquhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic looks solid, just a few notes to simplify the implementation.

}

const validIdentifierFields = ["title", "path"];
export const selectIdentifier = (entryData) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fieldNames would be more self documenting than entryData, which I would expect to be a map of the actual entry data.

@@ -34,7 +34,8 @@ function validateCollection(configCollection) {
files,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validateCollection function is just validating configuration - I'd say move it to Actions/config with the rest of the config validation (all of which may be worth porting elsewhere in the future), which allows you to put the new selector in with the rest of the selectors under the [FOLDER] key.

@@ -40,6 +43,38 @@ export function applyDefaults(config) {
});
}

function validateCollection(collection) {
Copy link
Contributor

@erquhart erquhart May 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a new function, relocated from the collections reducer (because it validates configuration), with the exception of the last conditional.

if (!!folder && !selectIdentifier(collection)) {
// Verify that folder-type collections have an identifier field for slug creation.
throw new Error(`Collection "${name}" must have a field that is a valid entry identifier. Supported fields are ${IDENTIFIER_FIELDS.join(', ')}.`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last conditional was added for this PR.

@erquhart
Copy link
Contributor

erquhart commented May 15, 2018

@tech4him1 mind reviewing with this latest commit? I think it's good to merge if it looks good to you.

@tech4him1
Copy link
Contributor Author

@erquhart We'll throw every time here, instead of just when the {{slug}} placeholder is used. For example, we had people using only the date to save the file, without a title at all. Even though it is intended to always check, like this PR is doing, do you think it is a breaking change and we should merge into 2.0 instead?

@erquhart
Copy link
Contributor

Yeah that makes sense - let's not backport this one if we can get away with it.

Copy link
Contributor

@Benaiah Benaiah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - I have a couple nitpicks, but nothing important. Tested the functionality manually and it works as designed.

// Cannot set custom delimiter without explicit and proper frontmatter format declaration
throw new Error(`Please set a proper frontmatter format for collection "${name}" to use a custom delimiter. Supported frontmatter formats are yaml-frontmatter, toml-frontmatter, and json-frontmatter.`);
}
if (!!folder && !selectIdentifier(collection)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the !! bool coercion here is necessary. Any value which would turn into false due to the coercion would also be ignored due to the && which follows immediately after.

return identifier;
};
const identifier = entryData.get(selectIdentifier(collection));
if (identifier === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (identifier === undefined) seems overly specific when if (identifier) seems to express the same semantics (none of false, null, and 0 are valid identifier field names either). If we'd rather avoid falsiness semantics, a better solution would be to use the second arguments to Immutable's .get and .find to explicitly return false when nothing is found.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just copying existing code, but if we're going for 2.0 this should be fine.

@erquhart erquhart merged commit 0022df5 into master May 25, 2018
@erquhart erquhart deleted the clarify-title-necessary branch May 25, 2018 16:15
brianlmacdonald pushed a commit to brianlmacdonald/netlify-cms that referenced this pull request May 29, 2018
brianlmacdonald pushed a commit to brianlmacdonald/netlify-cms that referenced this pull request May 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

save fails silently if no title field is configured
4 participants