Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from parasg1999/master
Browse files Browse the repository at this point in the history
Added isJWT and isBase64(with urlsafe)
rubiin authored May 26, 2020
2 parents 9bed442 + 0b3c9b9 commit f860f3b
Showing 5 changed files with 98 additions and 20 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# validator.js

![CI](https://github.com/validatorjs/validator-deno/workflows/CI/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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. |
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ import isHSL from "./src/lib/isHSL.ts";
import isFullWidth from "./src/lib/isFullWidth.ts";
import isHalfWidth from "./src/lib/isHalfWidth.ts";
import isInt from "./src/lib/isInt.ts";
import isJWT from "./src/lib/isJWT.ts";
import isLowerCase from "./src/lib/isLowerCase.ts";
import isUpperCase from "./src/lib/isUpperCase.ts";

@@ -56,6 +57,7 @@ const validator : ValidatorMap = {
isHexColor,
isHSL,
isInt,
isJWT,
isLowerCase,
isUpperCase,
version
38 changes: 26 additions & 12 deletions src/lib/isBase64.ts
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] === '=');
}
12 changes: 12 additions & 0 deletions src/lib/isJWT.ts
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)
}
62 changes: 55 additions & 7 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -5,21 +5,21 @@ test({
args: ['abc'],
valid: ['abc'],
invalid: ['Abc', '123']
})
});

test({
validator: 'equals',
args: ['abc ', { trim: true }],
valid: [' abc '],
invalid: ['Abc', '123']
})
});

test({
validator: 'equals',
args: ['abc', { ignore_case: true }],
valid: ['abc', 'AbC'],
invalid: ['@bc', '123']
})
});

test({
validator: 'isAscii',
@@ -35,7 +35,7 @@ test({
'123456',
'カタカナ',
],
})
});

test({
validator: 'isBase32',
@@ -59,6 +59,38 @@ test({
'=m9vYg==',
'Zm9vYm/y====',
],
});

test({
validator: 'isBase64',
valid: [
'Zg==',
'Zm8=',
'Zm9v',
'Zm9vYg==',
'Zm9vYmE=',
'Zm9vYmFy',
'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4=',
'Vml2YW11cyBmZXJtZW50dW0gc2VtcGVyIHBvcnRhLg==',
'U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw==',
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMPNS1Ufof9EW/M98FNw' +
'UAKrwflsqVxaxQjBQnHQmiI7Vac40t8x7pIb8gLGV6wL7sBTJiPovJ0V7y7oc0Ye' +
'rhKh0Rm4skP2z/jHwwZICgGzBvA0rH8xlhUiTvcwDCJ0kc+fh35hNt8srZQM4619' +
'FTgB66Xmp4EtVyhpQV+t02g6NzK72oZI0vnAvqhpkxLeLiMCyrI416wHm5Tkukhx' +
'QmcL2a6hNOyu0ixX/x2kSFXApEnVrJ+/IxGyfyw8kf4N2IZpW5nEP847lpfj0SZZ' +
'Fwrd1mnfnDbYohX2zRptLy2ZUn06Qo9pkG5ntvFEPo9bfZeULtjYzIl6K8gJ2uGZ' +
'HQIDAQAB',
],
invalid: [
'12345',
'',
'Vml2YW11cyBmZXJtZtesting123',
'Zg=',
'Z===',
'Zm=8',
'=m9vYg==',
'Zm9vYmFy====',
]
})

test({
@@ -299,7 +331,7 @@ test({
'abc123',
'!"#$%&()<>/+=-_? ~^|.,@`{}[]',
],
})
});


// test({
@@ -338,7 +370,7 @@ test({
'あいうえお',
'0011',
]
})
});


// test({
@@ -550,6 +582,22 @@ test({
],
});

test({
validator: 'isJWT',
valid: [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ', // No signature
],
invalid: [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
'$Zs.ewu.su84',
'ks64$S/9.dy$§kz.3sd73b',
],
});

test({
validator: 'isLowerCase',
valid: [
@@ -562,7 +610,7 @@ test({
'fooBar',
'123A',
],
})
});

test({
validator: 'isUpperCase',

0 comments on commit f860f3b

Please sign in to comment.