Skip to content

Commit

Permalink
Feat/validate url (#191)
Browse files Browse the repository at this point in the history
* feat(valid-instagram): adding isValidInstagram

* feat(valid-instagram): renaming to `isValidInstagramUserName`

* chore(testings): updating testings and fully coverage

* merging

* feat(validate-url): adding `isValidUrl`

* feat(validate-url): changing author email

* feat(validate-url): making value optional
  • Loading branch information
ammichael authored Jun 26, 2023
1 parent 422697f commit 400c8d2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
20 changes: 20 additions & 0 deletions docs/isValidURL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `isValidURL`

Validates if a string is a valid URL.

## Arguments

- `value: string`: value to be validated

## Usage

```jsx
import { isValidURL } from '@platformbuilders/validations';

isValidURL('https://platformbuilders.io'); // return true
isValidURL('http://platformbuilders.io'); // return true
isValidURL('ftp://platformbuilders.io'); // return true
isValidURL('www.platformbuilders.io'); // return true
isValidURL('platformbuilders.io'); // return false
isValidURL('platformbuilders'); // return false
```
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@platformbuilders/validations",
"version": "0.1.4",
"version": "0.1.5",
"description": "Validações de comuns de formulários",
"author": "Platform Builders <digitals@platformbuilders.io>",
"author": "Platform Builders <tecnologia@platformbuilders.io>",
"repository": {
"type": "git",
"url": "git+https://github.com/platformbuilders/validations.git"
Expand Down Expand Up @@ -31,13 +31,13 @@
"uglify": "for f in dist/*.js; do short=${f%.js}; uglifyjs $f > $short.min.js; rm -rf $f; mv $short.min.js $short.js; done",
"lint": "tsc && eslint '*/**/*.{ts, tsx}' --fix"
},
"lint-staged": {
"*.{ts,tsx}": [
"yarn pretty",
"yarn lint",
"yarn test"
]
},
"lint-staged": {
"*.{ts,tsx}": [
"yarn pretty",
"yarn lint",
"yarn test"
]
},
"peerDependencies": {
"@fnando/cnpj": "1.0.2",
"@fnando/cpf": "1.0.2",
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/isValidURL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isValidURL } from '../isValidURL';

describe('isValidURL', () => {
const tests = [
{ param: 'https://platformbuilders.io', expectedResult: true },
{ param: 'http://platformbuilders.io', expectedResult: true },
{ param: 'ftp://platformbuilders.io', expectedResult: true },
{ param: 'www.platformbuilders.io', expectedResult: true },
{ param: 'platformbuilders.io', expectedResult: false },
{ param: ' ', expectedResult: false },
{ param: '', expectedResult: false },
];
tests.forEach(({ expectedResult, param }) => {
test(`when param is "${param}", then return ${expectedResult}`, () => {
// when
const result = isValidURL(param);

// then
expect(result).toBe(expectedResult);
});
});
});
9 changes: 9 additions & 0 deletions src/isValidURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Validates if string is a valid URL
* it should contain http, https, ftp or www
* * Empty not allowed
* @param string
* @returns boolean
*/
export const isValidURL = (value?: string): boolean =>
value ? /(?:https?|ftp):\/\/|www\.[^\s]+/.test(value) : false;

0 comments on commit 400c8d2

Please sign in to comment.