This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from parasg1999/master
Added isJWT and isBase64(with urlsafe)
Showing
5 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# validator.js | ||
|
||
 | ||
[](https://opensource.org/licenses/MIT) | ||
|
||
A library for validating and sanitizing strings, inspired by the popular [validator.js](https://github.com/validatorjs/validator.js) | ||
|
@@ -10,7 +11,7 @@ A library for validating and sanitizing strings, inspired by the popular [valida | |
## Usage | ||
|
||
```typescript | ||
import validator from "https://deno.land/x/validatorjs/mod.ts"; | ||
import validator from "https://deno.land/x/deno_validator/mod.ts"; | ||
|
||
validator.isEmail('[email protected]'); //=> true | ||
``` | ||
|
@@ -24,6 +25,7 @@ Here is a list of the validators currently available. | |
| **equals(str, comparison [, options])** | check if the string matches the comparison.<br/><br/>`options` is an object which defaults to `{ trim: false, ignore_case: false }`. | | ||
| **isAscii(str)** | check if the string contains ASCII chars only. | | ||
| **isBase32(str)** | check if a string is base32 encoded. | | ||
| **isBase64(str)** | check if a string is base64 encoded. | | ||
| **isEmpty(str [, options])** | check if the string has a length of zero. <br/><br/>`options` is an object which defaults to `{ ignore_whitespace:false }`. | | ||
| **isFullWidth(str)** | check if the string contains any full-width chars. | | ||
| **isHalfWidth(str)** | check if the string contains any half-width chars. | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
import assertString from './util/assertString.ts'; | ||
|
||
const notBase64: RegExp = /[^A-Z0-9+\/=]/i; | ||
const notUrlSafeBase64: RegExp = /[^A-Z0-9_\-=]/i; | ||
|
||
interface Options { | ||
urlsafe?: boolean; | ||
} | ||
|
||
const defaultOptions: Options = { | ||
urlsafe: false, | ||
} | ||
|
||
export default function isBase64(str: string, options: Options = defaultOptions): boolean { | ||
const len: number = str.length; | ||
|
||
if (options.urlsafe) { | ||
if (!len || notUrlSafeBase64.test(str)) { | ||
return false; | ||
} | ||
} else { | ||
if (!len || len % 4 !== 0 || notBase64.test(str)) { | ||
return false; | ||
} | ||
} | ||
|
||
export default function isBase64(str: string): boolean { | ||
assertString(str); | ||
const len = str.length; | ||
if (!len || len % 4 !== 0 || notBase64.test(str)) { | ||
return false; | ||
} | ||
const firstPaddingChar = str.indexOf('='); | ||
return firstPaddingChar === -1 || | ||
firstPaddingChar === len - 1 || | ||
(firstPaddingChar === len - 2 && str[len - 1] === '='); | ||
const firstPaddingChar = str.indexOf('='); | ||
return firstPaddingChar === -1 || | ||
firstPaddingChar === len - 1 || | ||
(firstPaddingChar === len - 2 && str[len - 1] === '='); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import isBase64 from "./isBase64.ts"; | ||
|
||
export default function isJWT(str: string): boolean { | ||
const dotSplit: Array<string> = str.split('.'); | ||
const len: number = dotSplit.length; | ||
|
||
if (len > 3 || len < 2) { | ||
return false; | ||
} | ||
|
||
return dotSplit.reduce((acc: boolean, currElem) => acc && isBase64(currElem, { urlsafe: true }), true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters