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

myAchievements: achievement link validation(fixes #8114) #8115

Open
wants to merge 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class UsersAchievementsUpdateComponent implements OnInit, OnDestroy {
...achievement,
title: [ achievement.title, CustomValidators.required ],
description: [ achievement.description ],
link: [ achievement.link ],
link: [ achievement.link, [], CustomValidators.validLink ],
date: [ achievement.date, null, ac => this.validatorService.notDateInFuture$(ac) ]
}),
{ onSubmit: (formValue, formGroup) => {
Expand Down
22 changes: 18 additions & 4 deletions src/app/validators/custom-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,25 @@ export class CustomValidators {

static validLink(ac: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> {
return new Promise((resolve, reject) => {
try {
const url = new URL(ac.value);
if (!ac.value) {
resolve(null);
} catch (_) {
resolve({ 'invalidLink': true });
} else {
const trimmedValue = ac.value.trim();
if (/\s/.test(trimmedValue)) {
resolve({ 'invalidLink': true });
} else {
const domainRegex = /^(https?:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\.[a-zA-Z]{2,})*(\/.*)?$/;
if (!domainRegex.test(trimmedValue)) {
resolve({ 'invalidLink': true });
} else {
try {
const url = new URL(trimmedValue);
resolve(null);
} catch (_) {
resolve({ 'invalidLink': true });
}
}
}
}
});
}
Expand Down
Loading