-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For both entity and static pages, this pull request creates a function to check whether getPath() returns a valid or invalid (undefined or containing unsafe characters) URL slug value. If it's invalid, the page settings modal does not allow editing of the URL slug. If it's valid, then the modal does allow editing. However, if the user tries to save an invalid URL slug in the modal, then an error message is displayed and the Save button is disabled until new user input. For an entity page, a valid URL slug also includes any string that contains ${document.field} where "field" is variable. J=SLAP-2826 TEST=auto, manual Checked that URL behavior works as expected in the test site. --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Oliver Shi <[email protected]> Co-authored-by: Jacob <[email protected]> Co-authored-by: Jacob Wartofsky <[email protected]>
- Loading branch information
1 parent
bbf7d09
commit 55aa516
Showing
13 changed files
with
299 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export interface ValidationResult { | ||
valid: boolean; | ||
errorMessages: string[]; | ||
} | ||
|
||
/** | ||
* PageDataValidator contains various static utility methods | ||
* for validation of user-inputted page data. | ||
*/ | ||
export default class PageDataValidator { | ||
private isEntityPage = false; | ||
|
||
constructor(isEntityPage?: boolean) { | ||
if (isEntityPage) { | ||
this.isEntityPage = isEntityPage; | ||
} | ||
} | ||
|
||
checkIsURLEditable(url?: string) { | ||
if (!url) { | ||
return false; | ||
} | ||
const result: ValidationResult = this.validate({ url }); | ||
return result.valid; | ||
} | ||
/** | ||
* Throws an error if the user-inputted page data is invalid. | ||
*/ | ||
validate(pageData: { pageName?: string; url?: string }): ValidationResult { | ||
const errorMessages: string[] = []; | ||
if (pageData.pageName !== undefined) | ||
errorMessages.push(...this.validatePageName(pageData.pageName)); | ||
if (pageData.url !== undefined) | ||
errorMessages.push( | ||
...this.validateURLSlug(pageData.url, this.isEntityPage) | ||
); | ||
return { | ||
valid: errorMessages.length === 0, | ||
errorMessages: errorMessages, | ||
} as ValidationResult; | ||
} | ||
|
||
/** | ||
* Throws an error if the page name is invalid. | ||
*/ | ||
private validatePageName(pageName: string) { | ||
const errorMessages: string[] = []; | ||
if (!pageName) { | ||
errorMessages.push("A page name is required."); | ||
} | ||
const errorChars = pageName.match(/[\\/?%*:|"<>]/g); | ||
if (errorChars) { | ||
errorMessages.push( | ||
`Page name cannot contain the characters: ${[ | ||
...new Set(errorChars), | ||
].join("")}` | ||
); | ||
} | ||
if (pageName.endsWith(".")) { | ||
errorMessages.push(`Page name cannot end with a period.`); | ||
} | ||
if (pageName.length > 255) { | ||
errorMessages.push("Page name must be 255 characters or less."); | ||
} | ||
return errorMessages; | ||
} | ||
|
||
/** | ||
* Throws an error if the URL Slug is invalid. | ||
*/ | ||
private validateURLSlug(input: string, isEntityPage?: boolean) { | ||
const cleanInput = isEntityPage | ||
? input.replace(/\${document\..*?}/g, "") | ||
: input; | ||
const blackListURLChars = new RegExp(/[ <>""''|\\{}[\]]/g); | ||
const errorChars = cleanInput.match(blackListURLChars); | ||
const errorMessages: string[] = []; | ||
if (errorChars) { | ||
errorMessages.push( | ||
`URL slug contains invalid characters: ${[...new Set(errorChars)].join( | ||
"" | ||
)}` | ||
); | ||
} | ||
return errorMessages; | ||
} | ||
} |
Oops, something went wrong.