Skip to content

Commit 45b7df7

Browse files
Kiliandecavlapo
authored andcommitted
feat: add isMACAddress validator (#449)
1 parent 4bd586e commit 45b7df7

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ validator.isHalfWidth(str); // Checks if the string contains any half-width char
831831
validator.isVariableWidth(str); // Checks if the string contains variable-width chars.
832832
validator.isHexColor(str); // Checks if the string is a hexadecimal color.
833833
validator.isHexadecimal(str); // Checks if the string is a hexadecimal number.
834+
validator.isMACAddress(str); // Checks if the string is a MAC Address.
834835
validator.isIP(str, version); // Checks if the string is an IP (version 4 or 6).
835836
validator.isPort(str); // Check if the string is a valid port number.
836837
validator.isISBN(str, version); // Checks if the string is an ISBN (version 10 or 13).
@@ -926,6 +927,7 @@ validator.isInstance(value, target); // Checks value is an instance of the targe
926927
| `@IsVariableWidth()` | Checks if the string contains a mixture of full and half-width chars. |
927928
| `@IsHexColor()` | Checks if the string is a hexadecimal color. |
928929
| `@IsHexadecimal()` | Checks if the string is a hexadecimal number. |
930+
| `@IsMACAddress()` | Checks if the string is a MAC Address. |
929931
| `@IsIP(version?: "4"\|"6")` | Checks if the string is an IP (version 4 or 6). |
930932
| `@IsPort()` | Check if the string is a valid port number. |
931933
| `@IsISBN(version?: "10"\|"13")` | Checks if the string is an ISBN (version 10 or 13). |

Diff for: src/decorator/decorators.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,21 @@ export function IsHexadecimal(validationOptions?: ValidationOptions) {
830830
};
831831
}
832832

833+
/**
834+
* Checks if the string is MAC Address.
835+
*/
836+
export function IsMACAddress(validationOptions?: ValidationOptions) {
837+
return function (object: Object, propertyName: string) {
838+
const args: ValidationMetadataArgs = {
839+
type: ValidationTypes.IS_MAC_ADDRESS,
840+
target: object.constructor,
841+
propertyName: propertyName,
842+
validationOptions: validationOptions
843+
};
844+
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
845+
};
846+
}
847+
833848
/**
834849
* Checks if the string is an IP (version 4 or 6).
835850
*/
@@ -846,7 +861,6 @@ export function IsIP(version?: number, validationOptions?: ValidationOptions) {
846861
};
847862
}
848863

849-
850864
/**
851865
* Check if the string is a valid port number.
852866
*/

Diff for: src/validation/ValidationTypes.ts

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class ValidationTypes {
6767
static IS_VARIABLE_WIDTH = "isVariableWidth";
6868
static IS_HEX_COLOR = "isHexColor";
6969
static IS_HEXADECIMAL = "isHexadecimal";
70+
static IS_MAC_ADDRESS = "isMacAddress";
7071
static IS_IP = "isIp";
7172
static IS_PORT = "isPort";
7273
static IS_ISBN = "isIsbn";
@@ -220,6 +221,8 @@ export class ValidationTypes {
220221
return eachPrefix + "$property must be a hexadecimal color";
221222
case this.IS_HEXADECIMAL:
222223
return eachPrefix + "$property must be a hexadecimal number";
224+
case this.IS_MAC_ADDRESS:
225+
return eachPrefix + "$property must be a MAC Address";
223226
case this.IS_IP:
224227
return eachPrefix + "$property must be an ip address";
225228
case this.IS_ISBN:

Diff for: src/validation/Validator.ts

+10
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ export class Validator {
218218
return this.isHexColor(value);
219219
case ValidationTypes.IS_HEXADECIMAL:
220220
return this.isHexadecimal(value);
221+
case ValidationTypes.IS_MAC_ADDRESS:
222+
return this.isMACAddress(value);
221223
case ValidationTypes.IS_IP:
222224
return this.isIP(value, metadata.constraints[0]);
223225
case ValidationTypes.IS_PORT:
@@ -663,6 +665,14 @@ export class Validator {
663665
return typeof value === "string" && this.validatorJs.isHexadecimal(value);
664666
}
665667

668+
/**
669+
* Check if the string is a MAC address.
670+
* If given value is not a string, then it returns false.
671+
*/
672+
isMACAddress(value: unknown): boolean {
673+
return typeof value === "string" && this.validatorJs.isMACAddress(value);
674+
}
675+
666676
/**
667677
* Checks if the string is an IP (version 4 or 6).
668678
* If given value is not a string, then it returns false.

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

+50
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
IsISO31661Alpha2,
7676
IsISO31661Alpha3,
7777
IsHash,
78+
IsMACAddress,
7879
IsISSN,
7980
} from "../../src/decorator/decorators";
8081
import {Validator} from "../../src/validation/Validator";
@@ -2025,6 +2026,55 @@ describe("IsHexadecimal", function() {
20252026

20262027
});
20272028

2029+
describe("IsMACAddress", function() {
2030+
2031+
const validValues = [
2032+
"ab:ab:ab:ab:ab:ab",
2033+
"FF:FF:FF:FF:FF:FF",
2034+
"01:02:03:04:05:ab",
2035+
"01:AB:03:04:05:06"
2036+
];
2037+
const invalidValues = [
2038+
null,
2039+
undefined,
2040+
"abc",
2041+
"01:02:03:04:05",
2042+
"01:02:03:04::ab",
2043+
"1:2:3:4:5:6",
2044+
"AB:CD:EF:GH:01:02",
2045+
"A9C5 D4 9F EB D3",
2046+
"01-02 03:04 05 ab",
2047+
];
2048+
2049+
class MyClass {
2050+
@IsMACAddress()
2051+
someProperty: string;
2052+
}
2053+
2054+
it("should not fail if validator.validate said that its valid", function(done) {
2055+
checkValidValues(new MyClass(), validValues, done);
2056+
});
2057+
2058+
it("should fail if validator.validate said that its invalid", function(done) {
2059+
checkInvalidValues(new MyClass(), invalidValues, done);
2060+
});
2061+
2062+
it("should not fail if method in validator said that its valid", function() {
2063+
validValues.forEach(value => validator.isMACAddress(value).should.be.true);
2064+
});
2065+
2066+
it("should fail if method in validator said that its invalid", function() {
2067+
invalidValues.forEach(value => validator.isMACAddress(value).should.be.false);
2068+
});
2069+
2070+
it("should return error object with proper data", function(done) {
2071+
const validationType = "isMacAddress";
2072+
const message = "someProperty must be a MAC Address";
2073+
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
2074+
});
2075+
2076+
});
2077+
20282078
describe("IsIP", function() {
20292079

20302080
const validValues = [

0 commit comments

Comments
 (0)