Skip to content
This repository has been archived by the owner on Nov 30, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2 from justapagamentos/feature/url-validator
Browse files Browse the repository at this point in the history
Feature/url validator
  • Loading branch information
Raí Siqueira authored May 24, 2019
2 parents 0d84aaa + 096946c commit d3d8175
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions projects/justa/br-validators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class AppComponent {
- validateCNPJ (format: xx.xxx.xxx/xxxx-xx | xxxxxxxxxxxxxx)
- validateCPF (format: xxx.xxx.xxx-xx | xxxxxxxxxxx)
- validatePhone (format: (xx)xxxxx-xxxx | (xx)xxxx-xxxx | xxxxxxxxxx)
- validateURL (format: http[s]://lol.com)

## Build (dev only)

Expand Down
2 changes: 1 addition & 1 deletion projects/justa/br-validators/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@justa/ng-validators",
"version": "0.2.0",
"version": "1.0.0",
"peerDependencies": {
"@angular/common": "^5.0.0 || ^6.0.0 || ^7.0.0",
"@angular/core": "^5.0.0 || ^6.0.0 || ^7.0.0",
Expand Down
17 changes: 16 additions & 1 deletion projects/justa/br-validators/src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import * as Moment from "moment";

const moment = Moment;

const urlPattern = new RegExp(
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
);

/**
* @description remove all non digits caracters
*/
Expand Down Expand Up @@ -60,9 +64,20 @@ function validatePhone(control: AbstractControl): ValidationErrors | null {
: null;
}

/**
* @description validateURL for FormBuilder
*/
function validateURL(control: AbstractControl): ValidationErrors | null {
const controlValue = control.value;
return !urlPattern.test(controlValue)
? { isInvalid: { expected: true, message: "URL Inválida." } }
: null;
}

export const jstValidators = {
validateDate,
validateCNPJ,
validateCPF,
validatePhone
validatePhone,
validateURL
};

0 comments on commit d3d8175

Please sign in to comment.