Skip to content

Commit

Permalink
Merge pull request #885 from bgrgndzz/bgrgndzz/isJWT
Browse files Browse the repository at this point in the history
Add isJWT
  • Loading branch information
chriso authored Aug 24, 2018
2 parents 10d3cdd + 0b89e56 commit 11b79bd
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Validator | Description
**isIn(str, values)** | check if the string is in a array of allowed values.
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
**isJSON(str)** | check if the string is valid JSON (note: uses JSON.parse).
**isJWT(str)** | check if the string is valid JWT token.
**isLatLong(str)**                     | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
**isLowercase(str)** | check if the string is lowercase.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ var _isHash = require('./lib/isHash');

var _isHash2 = _interopRequireDefault(_isHash);

var _isJWT = require('./lib/isJWT');

var _isJWT2 = _interopRequireDefault(_isJWT);

var _isJSON = require('./lib/isJSON');

var _isJSON2 = _interopRequireDefault(_isJSON);
Expand Down Expand Up @@ -329,6 +333,7 @@ var validator = {
isISRC: _isISRC2.default,
isMD5: _isMD2.default,
isHash: _isHash2.default,
isJWT: _isJWT2.default,
isJSON: _isJSON2.default,
isEmpty: _isEmpty2.default,
isLength: _isLength2.default,
Expand Down
20 changes: 20 additions & 0 deletions lib/isJWT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isJWT;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var jwt = /^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/;

function isJWT(str) {
(0, _assertString2.default)(str);
return jwt.test(str);
}
module.exports = exports['default'];
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import isISRC from './lib/isISRC';

import isMD5 from './lib/isMD5';
import isHash from './lib/isHash';
import isJWT from './lib/isJWT';

import isJSON from './lib/isJSON';
import isEmpty from './lib/isEmpty';
Expand Down Expand Up @@ -133,6 +134,7 @@ const validator = {
isISRC,
isMD5,
isHash,
isJWT,
isJSON,
isEmpty,
isLength,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/isJWT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assertString from './util/assertString';

const jwt = /^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/;

export default function isJWT(str) {
assertString(str);
return jwt.test(str);
}
17 changes: 17 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,23 @@ describe('Validators', () => {
],
});
});
it('should validate JWT tokens', () => {
test({
validator: 'isJWT',
valid: [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E',
],
invalid: [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSw',
'$Zs.ewu.su84',
'ks64$S/9.dy$§kz.3sd73b',
],
});
});

it('should validate null strings', () => {
test({
Expand Down
8 changes: 8 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,13 @@ function isHash(str, algorithm) {
return hash.test(str);
}

var jwt = /^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/;

function isJWT(str) {
assertString(str);
return jwt.test(str);
}

function isJSON(str) {
assertString(str);
try {
Expand Down Expand Up @@ -1681,6 +1688,7 @@ var validator = {
isISRC: isISRC,
isMD5: isMD5,
isHash: isHash,
isJWT: isJWT,
isJSON: isJSON,
isEmpty: isEmpty,
isLength: isLength,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 11b79bd

Please sign in to comment.