-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Theshedman/feat/functional-interface
Refactor password validator and add fluent API
- Loading branch information
Showing
14 changed files
with
227 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Password Validation Test With Coverage" type="JavaScriptTestRunnerJest"> | ||
<config-file value="$PROJECT_DIR$/jest.config.ts" /> | ||
<node-interpreter value="project" /> | ||
<node-options value="--experimental-vm-modules" /> | ||
<jest-package value="$PROJECT_DIR$/node_modules/jest" /> | ||
<working-dir value="$PROJECT_DIR$" /> | ||
<jest-options value="--coverage" /> | ||
<envs /> | ||
<scope-kind value="ALL" /> | ||
<method v="2" /> | ||
</configuration> | ||
</component> | ||
<configuration default="false" name="Password Validation Test With Coverage" type="JavaScriptTestRunnerJest"> | ||
<config-file value="$PROJECT_DIR$/jest.config.ts"/> | ||
<node-interpreter value="project"/> | ||
<node-options value="--experimental-vm-modules"/> | ||
<jest-package value="$PROJECT_DIR$/node_modules/jest"/> | ||
<working-dir value="$PROJECT_DIR$"/> | ||
<jest-options value="--coverage"/> | ||
<envs/> | ||
<scope-kind value="ALL"/> | ||
<method v="2"/> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { DigitValidator } from "../../validator/DigitValidator.js"; | ||
import { ValidatorManager } from "../standard/ValidatorManager.js"; | ||
import { ValidationResult } from "../../validator/ValidationResult.js"; | ||
import { PasswordValidator } from "../../validator/PasswordValidator.js"; | ||
import { UpperCaseValidator } from "../../validator/UpperCaseValidator.js"; | ||
import { MinLengthValidator } from "../../validator/MinLengthValidator.js"; | ||
import { MaxLengthValidator } from "../../validator/MaxLengthValidator.js"; | ||
import { LowerCaseValidator } from "../../validator/LowerCaseValidator.js"; | ||
import { PasswordValidatorManager } from "../standard/PasswordValidatorManager.js"; | ||
import { NoSpaceCharacterValidator } from "../../validator/NoSpaceCharacterValidator.js"; | ||
import { SpecialCharacterValidator } from "../../validator/SpecialCharacterValidator.js"; | ||
|
||
export class FluentPasswordValidator { | ||
private digitValidator: DigitValidator | undefined; | ||
private lowerCaseValidator: LowerCaseValidator | undefined; | ||
private minLengthValidator: MinLengthValidator | undefined; | ||
private maxLengthValidator: MaxLengthValidator | undefined; | ||
private upperCaseValidator: UpperCaseValidator | undefined; | ||
private noSpaceCharacterValidator: NoSpaceCharacterValidator | undefined; | ||
private specialCharacterValidator: SpecialCharacterValidator | undefined; | ||
|
||
public min(passwordRule: number): this { | ||
this.minLengthValidator = new MinLengthValidator(passwordRule); | ||
|
||
return this; | ||
} | ||
|
||
public max(passwordRule: number): this { | ||
this.maxLengthValidator = new MaxLengthValidator(passwordRule); | ||
|
||
return this; | ||
} | ||
|
||
public lower(passwordRule: number): this { | ||
this.lowerCaseValidator = new LowerCaseValidator(passwordRule); | ||
|
||
return this; | ||
} | ||
|
||
public upper(passwordRule: number): this { | ||
this.upperCaseValidator = new UpperCaseValidator(passwordRule); | ||
|
||
return this; | ||
} | ||
|
||
public digit(passwordRule: number): this { | ||
this.digitValidator = new DigitValidator(passwordRule); | ||
|
||
return this; | ||
} | ||
|
||
public noSpace(): this { | ||
this.noSpaceCharacterValidator = new NoSpaceCharacterValidator(); | ||
|
||
return this; | ||
} | ||
|
||
public specialCharacter(passwordRule: number): this { | ||
this.specialCharacterValidator = new SpecialCharacterValidator( | ||
passwordRule, | ||
); | ||
|
||
return this; | ||
} | ||
|
||
public validate(password: string): ValidationResult { | ||
const validators: Array<PasswordValidator> = [ | ||
this.digitValidator as PasswordValidator, | ||
this.minLengthValidator as PasswordValidator, | ||
this.maxLengthValidator as PasswordValidator, | ||
this.lowerCaseValidator as PasswordValidator, | ||
this.upperCaseValidator as PasswordValidator, | ||
this.noSpaceCharacterValidator as PasswordValidator, | ||
this.specialCharacterValidator as PasswordValidator, | ||
]; | ||
|
||
const pm: ValidatorManager = PasswordValidatorManager.standard(); | ||
|
||
for (const validator of validators) { | ||
if (!validator) { | ||
continue; | ||
} | ||
|
||
pm.register(validator); | ||
} | ||
|
||
return pm.validate(password); | ||
} | ||
} |
File renamed without changes.
17 changes: 12 additions & 5 deletions
17
...ord/validator/PasswordValidatorManager.ts → .../api/standard/PasswordValidatorManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/password/validator/ValidatorManager.ts → ...password/api/standard/ValidatorManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export * from "./validator/Util.js"; | ||
export * from "./validator/Validator.js"; | ||
export * from "./validator/DigitValidator.js"; | ||
export * from "./validator/ValidationResult.js"; | ||
export * from "./api/standard/ValidatorManager.js"; | ||
export * from "./validator/ValidatorCategory.js"; | ||
export * from "./validator/PasswordValidator.js"; | ||
export * from "./validator/LowerCaseValidator.js"; | ||
export * from "./validator/MaxLengthValidator.js"; | ||
export * from "./validator/UpperCaseValidator.js"; | ||
export * from "./validator/MinLengthValidator.js"; | ||
export * from "./validator/NoSpaceCharacterValidator.js"; | ||
export * from "./api/standard/PasswordValidatorManager.js"; | ||
export * from "./validator/SpecialCharacterValidator.js"; | ||
export * from "./api/standard/PasswordValidatorConflictException.js"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ValidationResult } from "../../validator/ValidationResult.js"; | ||
import { PasswordValidatorManager } from "../../api/standard/PasswordValidatorManager.js"; | ||
|
||
const result: ValidationResult = PasswordValidatorManager.fluent() | ||
.min(6) | ||
.digit(1) | ||
.specialCharacter(1) | ||
.validate("eihi2kd#"); | ||
|
||
console.log({ result }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { LowerCaseValidator } from "../../validator/LowerCaseValidator.js"; | ||
import { MinLengthValidator } from "../../validator/MinLengthValidator.js"; | ||
import { UpperCaseValidator } from "../../validator/UpperCaseValidator.js"; | ||
import { NoSpaceCharacterValidator } from "../../validator/NoSpaceCharacterValidator.js"; | ||
import { SpecialCharacterValidator } from "../../validator/SpecialCharacterValidator.js"; | ||
import { PasswordValidatorManager } from "../../api/standard/PasswordValidatorManager.js"; | ||
|
||
const pm = PasswordValidatorManager.standard(); | ||
|
||
const minLength = new MinLengthValidator(6); | ||
const specialChar = new SpecialCharacterValidator(1); | ||
const uppercase = new UpperCaseValidator(1); | ||
const lowercase = new LowerCaseValidator(1); | ||
const noSpace = new NoSpaceCharacterValidator(); | ||
|
||
pm.register(minLength, specialChar, uppercase, lowercase, noSpace); | ||
|
||
const results = pm.validate("a*bcUdsdfdsf"); | ||
|
||
console.log({ results }); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.