Skip to content

Commit 874861b

Browse files
Kiliandecavlapo
authored andcommitted
feat: add isJWT validator (#444)
1 parent 7e21409 commit 874861b

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 o
837837
validator.isISIN(str); // Checks if the string is an ISIN (stock/security identifier).
838838
validator.isISO8601(str); // Checks if the string is a valid ISO 8601 date.
839839
validator.isJSON(str); // Checks if the string is valid JSON (note: uses JSON.parse).
840+
validator.isJWT(str) // Checks if the string is valid JWT.
840841
validator.isObject(object); // Checks if the object is valid Object (null, functions, arrays will return false)
841842
validator.isNotEmptyObject(object); // Checks if the object is not empty
842843
validator.isLowercase(str); // Checks if the string is lowercase.
@@ -929,6 +930,7 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
929930
| `@IsISIN()` | Checks if the string is an ISIN (stock/security identifier). |
930931
| `@IsISO8601()` | Checks if the string is a valid ISO 8601 date. |
931932
| `@IsJSON()` | Checks if the string is valid JSON. |
933+
| `@IsJWT()` | Checks if the string is valid JWT. |
932934
| `@IsObject()` | Checks if the object is valid Object (null, functions, arrays will return false). |
933935
| `@IsNotEmptyObject()` | Checks if the object is not empty. |
934936
| `@IsLowercase()` | Checks if the string is lowercase. |

src/decorator/decorators.ts

+15
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,21 @@ export function IsJSON(validationOptions?: ValidationOptions) {
923923
};
924924
}
925925

926+
/**
927+
* Checks if the string is valid JWT.
928+
*/
929+
export function IsJWT(validationOptions?: ValidationOptions) {
930+
return function (object: Object, propertyName: string) {
931+
const args: ValidationMetadataArgs = {
932+
type: ValidationTypes.IS_JWT,
933+
target: object.constructor,
934+
propertyName: propertyName,
935+
validationOptions: validationOptions
936+
};
937+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
938+
};
939+
}
940+
926941
/**
927942
* Checks if the value is a valid object.
928943
*/

src/validation/ValidationTypes.ts

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class ValidationTypes {
7373
static IS_ISIN = "isIsin";
7474
static IS_ISO8601 = "isIso8601";
7575
static IS_JSON = "isJson";
76+
static IS_JWT = "isJwt";
7677
static IS_OBJECT = "isObject";
7778
static IS_NOT_EMPTY_OBJECT = "isNotEmptyObject";
7879
static IS_LOWERCASE = "isLowercase";
@@ -227,6 +228,8 @@ export class ValidationTypes {
227228
return eachPrefix + "$property must be a valid ISO 8601 date string";
228229
case this.IS_JSON:
229230
return eachPrefix + "$property must be a json string";
231+
case this.IS_JWT:
232+
return eachPrefix + "$property must be a jwt string";
230233
case this.IS_OBJECT:
231234
return eachPrefix + "$property must be an object";
232235
case this.IS_NOT_EMPTY_OBJECT:

src/validation/Validator.ts

+10
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ export class Validator {
230230
return this.isISO8601(value);
231231
case ValidationTypes.IS_JSON:
232232
return this.isJSON(value);
233+
case ValidationTypes.IS_JWT:
234+
return this.isJWT(value);
233235
case ValidationTypes.IS_OBJECT:
234236
return this.isObject(value);
235237
case ValidationTypes.IS_NOT_EMPTY_OBJECT:
@@ -704,6 +706,14 @@ export class Validator {
704706
return typeof value === "string" && this.validatorJs.isJSON(value);
705707
}
706708

709+
/**
710+
* Checks if the string is valid JWT token.
711+
* If given value is not a string, then it returns false.
712+
*/
713+
isJWT(value: unknown): boolean {
714+
return typeof value === "string" && this.validatorJs.isJWT(value);
715+
}
716+
707717
/**
708718
* Checks if the value is valid Object.
709719
* Returns false if the value is not an object.

test/functional/validation-functions-and-decorators.spec.ts

+45
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
IsIn,
3737
IsInt,
3838
IsJSON,
39+
IsJWT,
3940
IsObject,
4041
IsNotEmptyObject,
4142
Length,
@@ -2314,6 +2315,50 @@ describe("IsJSON", function() {
23142315

23152316
});
23162317

2318+
describe("IsJWT", function() {
2319+
2320+
const validValues = [
2321+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI",
2322+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3JlbSI6Imlwc3VtIn0.ymiJSsMJXR6tMSr8G9usjQ15_8hKPDv_CArLhxw28MI",
2323+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2xvciI6InNpdCIsImFtZXQiOlsibG9yZW0iLCJpcHN1bSJdfQ.rRpe04zbWbbJjwM43VnHzAboDzszJtGrNsUxaqQ-GQ8",
2324+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqb2huIjp7ImFnZSI6MjUsImhlaWdodCI6MTg1fSwiamFrZSI6eyJhZ2UiOjMwLCJoZWlnaHQiOjI3MH19.YRLPARDmhGMC3BBk_OhtwwK21PIkVCqQe8ncIRPKo-E",
2325+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ", // No signature
2326+
];
2327+
const invalidValues = [
2328+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
2329+
"$Zs.ewu.su84",
2330+
"ks64$S/9.dy$§kz.3sd73b",
2331+
];
2332+
2333+
class MyClass {
2334+
@IsJWT()
2335+
someProperty: string;
2336+
}
2337+
2338+
it("should not fail if validator.validate said that its valid", function(done) {
2339+
checkValidValues(new MyClass(), validValues, done);
2340+
});
2341+
2342+
it("should fail if validator.validate said that its invalid", function(done) {
2343+
checkInvalidValues(new MyClass(), invalidValues, done);
2344+
});
2345+
2346+
it("should not fail if method in validator said that its valid", function() {
2347+
validValues.forEach(value => validator.isJWT(value).should.be.true);
2348+
});
2349+
2350+
it("should fail if method in validator said that its invalid", function() {
2351+
invalidValues.forEach(value => validator.isJWT(value).should.be.false);
2352+
});
2353+
2354+
it("should return error object with proper data", function(done) {
2355+
const validationType = "isJwt";
2356+
const message = "someProperty must be a jwt string";
2357+
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
2358+
});
2359+
2360+
});
2361+
23172362
describe("IsObject", function() {
23182363

23192364
const validValues = [{ "key": "value" }, { key: "value" }, {}];

0 commit comments

Comments
 (0)