Skip to content

Commit 4bd586e

Browse files
Kiliandecavlapo
authored andcommitted
feat: add isISSN validator (#450)
1 parent 79dccff commit 4bd586e

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ validator.maxLength(str, max); // Checks if the string's length is not more than
860860
validator.matches(str, pattern, modifiers); // Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
861861
validator.isMilitaryTime(str); // Checks if the string is a valid representation of military time in the format HH:MM.
862862
validator.isHash(str, algorithm); // Checks if the string is a hash of type algorithm.
863+
validator.isISSN(str, options); // Checks if the string is a ISSN.
863864
864865
// array validation methods
865866
validator.arrayContains(array, values); // Checks if array contains all values from the given array of values.
@@ -955,6 +956,7 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
955956
| `@Matches(pattern: RegExp, modifiers?: string)` | Checks if string matches the pattern. Either matches('foo', /foo/i) or matches('foo', 'foo', 'i').
956957
| `@IsMilitaryTime()` | Checks if the string is a valid representation of military time in the format HH:MM. |
957958
| `@IsHash(algorithm: string)` | Checks if the string is a hash of type algorithm. <br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']` |
959+
| `@IsISSN(options?: IsISSNOptions)` | Checks if the string is a ISSN. |
958960
| **Array validation decorators** |
959961
| `@ArrayContains(values: any[])` | Checks if array contains all values from the given array of values. |
960962
| `@ArrayNotContains(values: any[])` | Checks if array does not contain any of the given values. |

Diff for: src/decorator/decorators.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,23 @@ export function IsHash(algorithm: string, validationOptions?: ValidationOptions)
12501250
};
12511251
}
12521252

1253+
1254+
/**
1255+
* Checks if the string is a valid ISSN.
1256+
*/
1257+
export function IsISSN(options?: ValidatorJS.IsISSNOptions, validationOptions?: ValidationOptions) {
1258+
return function (object: Object, propertyName: string) {
1259+
const args: ValidationMetadataArgs = {
1260+
type: ValidationTypes.IS_ISSN,
1261+
target: object.constructor,
1262+
propertyName: propertyName,
1263+
constraints: [options],
1264+
validationOptions: validationOptions
1265+
};
1266+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
1267+
};
1268+
}
1269+
12531270
// -------------------------------------------------------------------------
12541271
// Array checkers
12551272
// -------------------------------------------------------------------------

Diff for: src/validation/ValidationTypes.ts

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export class ValidationTypes {
9393
static MATCHES = "matches";
9494
static IS_MILITARY_TIME = "isMilitaryTime";
9595
static IS_HASH = "isHash";
96+
static IS_ISSN = "isISSN";
9697

9798
/* array checkers */
9899
static ARRAY_CONTAINS = "arrayContains";
@@ -284,6 +285,8 @@ export class ValidationTypes {
284285
return eachPrefix + "$property must be a valid representation of military time in the format HH:MM";
285286
case this.IS_HASH:
286287
return eachPrefix + "$property must be a hash of type $constraint1";
288+
case this.IS_ISSN:
289+
return eachPrefix + "$property must be a ISSN";
287290

288291
/* array checkers */
289292
case this.ARRAY_CONTAINS:

Diff for: src/validation/Validator.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ export class Validator {
270270
return this.isMilitaryTime(value);
271271
case ValidationTypes.IS_HASH:
272272
return this.isHash(value, metadata.constraints[0]);
273-
273+
case ValidationTypes.IS_ISSN:
274+
return this.isISSN(value, metadata.constraints[0]);
275+
274276
/* array checkers */
275277
case ValidationTypes.ARRAY_CONTAINS:
276278
return this.arrayContains(value, metadata.constraints[0]);
@@ -877,6 +879,14 @@ export class Validator {
877879
return typeof value === "string" && this.validatorJs.isHash(value, algorithm);
878880
}
879881

882+
/**
883+
* Checks if the string is a ISSN.
884+
* If given value is not a string, then it returns false.
885+
*/
886+
isISSN(value: unknown, options?: ValidatorJS.IsISSNOptions): boolean {
887+
return typeof value === "string" && this.validatorJs.isISSN(value, options);
888+
}
889+
880890
// -------------------------------------------------------------------------
881891
// Validation Methods: array checkers
882892
// -------------------------------------------------------------------------

Diff for: test/functional/validation-functions-and-decorators.spec.ts

+100
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
IsISO31661Alpha2,
7676
IsISO31661Alpha3,
7777
IsHash,
78+
IsISSN,
7879
} from "../../src/decorator/decorators";
7980
import {Validator} from "../../src/validation/Validator";
8081
import {ValidatorOptions} from "../../src/validation/ValidatorOptions";
@@ -3371,6 +3372,105 @@ describe("isHash", function() {
33713372
});
33723373
});
33733374

3375+
describe("IsISSN", function() {
3376+
3377+
const validValues = [
3378+
"0378-5955",
3379+
"0000-0000",
3380+
"2434-561X",
3381+
"2434-561x",
3382+
"01896016",
3383+
"20905076",
3384+
];
3385+
const invalidValues = [
3386+
null,
3387+
undefined,
3388+
"0378-5954",
3389+
"0000-0001",
3390+
"0378-123",
3391+
"037-1234",
3392+
"0",
3393+
"2434-561c",
3394+
"1684-5370",
3395+
"19960791",
3396+
"",
3397+
];
3398+
3399+
class MyClass {
3400+
@IsISSN()
3401+
someProperty: string;
3402+
}
3403+
3404+
it("should not fail if validator.validate said that its valid", function(done) {
3405+
checkValidValues(new MyClass(), validValues, done);
3406+
});
3407+
3408+
it("should fail if validator.validate said that its invalid", function(done) {
3409+
checkInvalidValues(new MyClass(), invalidValues, done);
3410+
});
3411+
3412+
it("should not fail if method in validator said that its valid", function() {
3413+
validValues.forEach(value => validator.isISSN(value).should.be.true);
3414+
});
3415+
3416+
it("should fail if method in validator said that its invalid", function() {
3417+
invalidValues.forEach(value => validator.isISSN(value).should.be.false);
3418+
});
3419+
3420+
it("should return error object with proper data", function(done) {
3421+
const validationType = "isISSN";
3422+
const message = "someProperty must be a ISSN";
3423+
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
3424+
});
3425+
3426+
});
3427+
3428+
describe("IsISSN with options", function() {
3429+
3430+
const options = {case_sensitive: true, require_hyphen: true};
3431+
3432+
const validValues = [
3433+
"2434-561X",
3434+
"0378-5955",
3435+
];
3436+
const invalidValues = [
3437+
null,
3438+
undefined,
3439+
"2434-561x",
3440+
"2434561X",
3441+
"2434561x",
3442+
"03785955",
3443+
];
3444+
3445+
class MyClass {
3446+
@IsISSN(options)
3447+
someProperty: string;
3448+
}
3449+
3450+
it("should not fail if validator.validate said that its valid", function(done) {
3451+
checkValidValues(new MyClass(), validValues, done);
3452+
});
3453+
3454+
it("should fail if validator.validate said that its invalid", function(done) {
3455+
checkInvalidValues(new MyClass(), invalidValues, done);
3456+
});
3457+
3458+
it("should not fail if method in validator said that its valid", function() {
3459+
validValues.forEach(value => validator.isISSN(value, options).should.be.true);
3460+
});
3461+
3462+
it("should fail if method in validator said that its invalid", function() {
3463+
invalidValues.forEach(value => validator.isISSN(value, options).should.be.false);
3464+
});
3465+
3466+
it("should return error object with proper data", function(done) {
3467+
const validationType = "isISSN";
3468+
const message = "someProperty must be a ISSN";
3469+
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
3470+
});
3471+
3472+
});
3473+
33743474

33753475

33763476

0 commit comments

Comments
 (0)