Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
added url validation to 'base url' field
Browse files Browse the repository at this point in the history
  • Loading branch information
banders committed Jul 25, 2018
1 parent c85022f commit 5c089c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/app/register-api/register-api.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<input type="text" class="form-control" id="base-url" placeholder="URL" formControlName="baseUrl">
</div>

<div class="alert alert-danger" role="alert" *ngIf="form2.get('baseUrl').errors && form2.get('baseUrl').errors.pattern">
Invalid URL
</div>

<div class="form-group">
<label>Does the API support HTTPS?</label>&nbsp;&nbsp;

Expand Down
6 changes: 4 additions & 2 deletions src/app/register-api/register-api.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Component, OnInit } from '@angular/core';
import { ArggService } from '../services/argg.service';
import { BcdcService } from '../services/bcdc.service';
import { OpenApiService } from '../services/openapi.service';
import { UrlService } from '../services/url.service';
import { FormControl, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { debounceTime, mergeMap, tap, catchError, filter, reduce } from 'rxjs/operators';
import { of, from, combineLatest } from 'rxjs';
Expand Down Expand Up @@ -38,7 +39,8 @@ export class RegisterApiComponent implements OnInit {
constructor(private fb: FormBuilder,
private arggService: ArggService,
public bcdcService: BcdcService,
private openApiService: OpenApiService) {
private openApiService: OpenApiService,
private urlService: UrlService) {

this.form1 = this.fb.group({
//OpenApi
Expand All @@ -49,7 +51,7 @@ export class RegisterApiComponent implements OnInit {
//General
title: [null, Validators.required],
description: [null, Validators.required],
baseUrl: [null, Validators.required],
baseUrl: [null, Validators.compose([Validators.required, Validators.pattern(urlService.validUrlPattern)])],
supportsHttps: [null],
supportsCors: [null],
//owner
Expand Down
27 changes: 17 additions & 10 deletions src/app/services/url.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@ import { Injectable } from '@angular/core';
})
export class UrlService {

constructor() { }

/**
Checks if the given url is "valid".
Implementation Note: It is actually quite difficult to find a regular expression that
It is actually quite difficult to find a regular expression that
can correctly identify valid URLs and reject invalid URLs. Most have limitations in
which they reject certain valid URLs (such as IP addresses, "localhost", and others).
We really don't want to reject valid URLs, so the implementation here is more basic.
It only disallows a small set of invalid URLs, but at least it doesn't disallow valid
ones.
Regex explanation:
- url must start with http(s)://
- next character must be a number or letter
- any additional characters may follow
*/
validUrlPattern = /^https?\:\/\/[\w]{1,}[^\s]*/;

constructor() { }



/**
Checks if the given url is "valid".
*/
isValidUrl(url) {
//regex explanation:
// - url must start with http(s)://
// - next character must be a number or letter
// - any additional characters may follow
var query = /^https?\:\/\/[\w]{1,}[^\s]*/
var regex = new RegExp(query);

//var query = /^https?\:\/\/[\w]{1,}[^\s]*/
var regex = new RegExp(this.validUrlPattern);
var result = regex.test(url);
return result;
}
Expand Down

0 comments on commit 5c089c1

Please sign in to comment.