Skip to content

Commit 2f4bf4e

Browse files
rubiinvlapo
authored andcommitted
fix: add locale parameter for isAlpha and isAlphanumeric validat… (#406)
* added locale for isAlpha and isAlphanumeric with tests * changed the parameter pattern on both functions * removed default en-US lacale * Added numberOptions on IsNumberString
1 parent 76c948a commit 2f4bf4e

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

src/decorator/decorators.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,13 @@ export function IsBooleanString(validationOptions?: ValidationOptions) {
504504
/**
505505
* Checks if the string is a number.
506506
*/
507-
export function IsNumberString(validationOptions?: ValidationOptions) {
507+
export function IsNumberString(validationOptions?: ValidationOptions, NumberOptions?: IsNumberOptions) {
508508
return function (object: Object, propertyName: string) {
509509
const args: ValidationMetadataArgs = {
510510
type: ValidationTypes.IS_NUMBER_STRING,
511511
target: object.constructor,
512512
propertyName: propertyName,
513+
constraints: [NumberOptions],
513514
validationOptions: validationOptions
514515
};
515516
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
@@ -555,12 +556,13 @@ export function NotContains(seed: string, validationOptions?: ValidationOptions)
555556
/**
556557
* Checks if the string contains only letters (a-zA-Z).
557558
*/
558-
export function IsAlpha(validationOptions?: ValidationOptions) {
559+
export function IsAlpha(locale?: string, validationOptions?: ValidationOptions) {
559560
return function (object: Object, propertyName: string) {
560561
const args: ValidationMetadataArgs = {
561562
type: ValidationTypes.IS_ALPHA,
562563
target: object.constructor,
563564
propertyName: propertyName,
565+
constraints: [locale],
564566
validationOptions: validationOptions
565567
};
566568
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
@@ -570,12 +572,13 @@ export function IsAlpha(validationOptions?: ValidationOptions) {
570572
/**
571573
* Checks if the string contains only letters and numbers.
572574
*/
573-
export function IsAlphanumeric(validationOptions?: ValidationOptions) {
575+
export function IsAlphanumeric(locale?: string, validationOptions?: ValidationOptions) {
574576
return function (object: Object, propertyName: string) {
575577
const args: ValidationMetadataArgs = {
576578
type: ValidationTypes.IS_ALPHANUMERIC,
577579
target: object.constructor,
578580
propertyName: propertyName,
581+
constraints: [locale],
579582
validationOptions: validationOptions
580583
};
581584
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));

src/validation/Validator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ export class Validator {
174174
case ValidationTypes.NOT_CONTAINS:
175175
return this.notContains(value, metadata.constraints[0]);
176176
case ValidationTypes.IS_ALPHA:
177-
return this.isAlpha(value);
177+
return this.isAlpha(value, metadata.constraints[0]);
178178
case ValidationTypes.IS_ALPHANUMERIC:
179-
return this.isAlphanumeric(value);
179+
return this.isAlphanumeric(value, metadata.constraints[0]);
180180
case ValidationTypes.IS_DECIMAL:
181181
return this.isDecimal(value, metadata.constraints[0]);
182182
case ValidationTypes.IS_ASCII:
@@ -495,16 +495,16 @@ export class Validator {
495495
* Checks if the string contains only letters (a-zA-Z).
496496
* If given value is not a string, then it returns false.
497497
*/
498-
isAlpha(value: string): boolean {
499-
return typeof value === "string" && this.validatorJs.isAlpha(value);
498+
isAlpha(value: string, locale?: ValidatorJS.AlphaLocale): boolean {
499+
return typeof value === "string" && this.validatorJs.isAlpha(value, locale);
500500
}
501501

502502
/**
503503
* Checks if the string contains only letters and numbers.
504504
* If given value is not a string, then it returns false.
505505
*/
506-
isAlphanumeric(value: string): boolean {
507-
return typeof value === "string" && this.validatorJs.isAlphanumeric(value);
506+
isAlphanumeric(value: string, locale?: ValidatorJS.AlphanumericLocale): boolean {
507+
return typeof value === "string" && this.validatorJs.isAlphanumeric(value, locale);
508508
}
509509

510510
/**

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ describe("NotContains", function() {
12611261

12621262
describe("IsAlpha", function() {
12631263

1264-
const constraint = "";
1264+
const constraint = "en-GB";
12651265
const validValues = ["hellomynameisalex"];
12661266
const invalidValues = [null, undefined, "hello1mynameisalex"];
12671267

@@ -1279,11 +1279,11 @@ describe("IsAlpha", function() {
12791279
});
12801280

12811281
it("should not fail if method in validator said that its valid", function() {
1282-
validValues.forEach(value => validator.isAlpha(value).should.be.true);
1282+
validValues.forEach(value => validator.isAlpha(value, constraint).should.be.true);
12831283
});
12841284

12851285
it("should fail if method in validator said that its invalid", function() {
1286-
invalidValues.forEach(value => validator.isAlpha(value).should.be.false);
1286+
invalidValues.forEach(value => validator.isAlpha(value, constraint).should.be.false);
12871287
});
12881288

12891289
it("should return error object with proper data", function(done) {

0 commit comments

Comments
 (0)