tech-conferences/conference-data

Add Year in URL Validation in tests

Closed this issue · 2 comments

I have seen some cases where the URL has a year, but that year does not match the conference details. Here are some examples:

image

To avoid those incorrect URLs, it will be a good idea to add an additional test to the checkUrl function

  • If a 4-digit number starting with "20" is found in the URL
  • Then check if the yearURL matches the event start or end year

Here is a possible solution for this problem:

 function checkUrl(conference, property) {
        const value = conference[property];
        assertField(httpRegex.test(value), property, 'should start with http', value);
        assertField(!httpNoQuestionmarkRegex.test(value), property, 'should not contain a "?"', value);
        assertField(!urlShortener.test(value), property, 'should not use url shorteners', value);

        const yearInUrl = value.match(/20\d{2}/);

        // If a 4-digit number starting with "20" is found in the URL
        if (yearInUrl) {
            const year = yearInUrl[0];
            const eventStartYear = new Date(conference.startDate).getFullYear();
            const eventEndYear = new Date(conference.endDate).getFullYear();

            // Check if the year in the URL matches the event start or end year
            assertField(
                year === eventStartYear.toString() || year === eventEndYear.toString(),
                property,
                `If a year is present in the URL, it should match the event start or end year`,
                value
            );
        }
    }

Output

image

I notice that the code has some issues and could be improved. but it is a good starting point.

Sounds good. Thanks a lot.

Fix with: #6886