Skip to content

Commit

Permalink
Merge pull request #180 from gfredtech/Develop
Browse files Browse the repository at this point in the history
Fixes Restricting custom URLs to alpha-numeric characters is undesirable. #176
  • Loading branch information
seyiogunjuyigbe authored Oct 25, 2019
2 parents cc00ab8 + 2208785 commit 96dc736
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage/
npm-debug.log
.idea/
.jshintrc
.log/
10 changes: 5 additions & 5 deletions src/middlewares/validateUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { respondWithWarning } from '../helpers/responseHandler';
*/
export const stripUrl = async (req, res, next) => {
const { long_url, expiry_date, custom_url } = req.body;

const schema = Joi.object({
url: Joi.string().regex(
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
).error(new Error('Enter a valid URL')),
expiry: Joi.date().iso().greater(new Date()).allow('').error(new Error('Expiry date must be in the future')),
custom_url: Joi.string().alphanum().allow(''),
custom_url: Joi.string().regex(/^[A-Za-z0-9_.\-~]{0,}$/).error(new Error('custom URL must contain only alphanumeric, period(.), hyphen(-), underscore(_) and tilde(~) characters')),
});
const validationOptions = {
allowUnknown: true, // allow unknown keys that will be ignored
Expand Down Expand Up @@ -76,11 +76,11 @@ export const urlAlreadyTrimmedByUser = (req, res, next) => {

export const customUrlExists = async(req, res, next) => {
const customUrl = req.body.custom_url;

if(customUrl) {
//Search the db for this custom url.
let retrievedClip = await UrlShorten.findOne({urlCode: customUrl});

//If an existing clip already has the same custom url code....
if (retrievedClip) {
//If the existing clip has expired...
Expand All @@ -93,6 +93,6 @@ export const customUrlExists = async(req, res, next) => {
return respondWithWarning(res, 409, "Custom URL already in use. Please try another");
}
};

next()
};

0 comments on commit 96dc736

Please sign in to comment.