@@ -54,6 +54,84 @@ describe('Validation Library', () => {
5454 } )
5555 } )
5656
57+ describe ( 'Password Validator' , ( ) => {
58+ test ( 'basic password validation' , ( ) => {
59+ const validator = v . password ( )
60+ expect ( validator . test ( 'password123' ) ) . toBe ( true )
61+ expect ( validator . test ( '' ) ) . toBe ( true )
62+ expect ( validator . test ( 123 as any ) ) . toBe ( false )
63+ } )
64+
65+ test ( 'password matching validation' , ( ) => {
66+ const originalPassword = 'MySecureP@ss123'
67+ const validator = v . password ( ) . matches ( originalPassword )
68+ expect ( validator . test ( originalPassword ) ) . toBe ( true )
69+ expect ( validator . test ( 'DifferentP@ss123' ) ) . toBe ( false )
70+ } )
71+
72+ test ( 'password length validation' , ( ) => {
73+ const validator = v . password ( ) . minLength ( 8 ) . maxLength ( 20 )
74+ expect ( validator . test ( 'Secure123' ) ) . toBe ( true )
75+ expect ( validator . test ( 'Short1' ) ) . toBe ( false )
76+ expect ( validator . test ( 'ThisPasswordIsWayTooLongToBeValid123' ) ) . toBe ( false )
77+ } )
78+
79+ test ( 'password character requirements' , ( ) => {
80+ const validator = v . password ( )
81+ . hasUppercase ( )
82+ . hasLowercase ( )
83+ . hasNumbers ( )
84+ . hasSpecialCharacters ( )
85+
86+ // Valid password with all requirements
87+ expect ( validator . test ( 'MySecureP@ss123' ) ) . toBe ( true )
88+
89+ // Missing uppercase
90+ expect ( validator . test ( 'mysecurep@ss123' ) ) . toBe ( false )
91+
92+ // Missing lowercase
93+ expect ( validator . test ( 'MYSECUREP@SS123' ) ) . toBe ( false )
94+
95+ // Missing numbers
96+ expect ( validator . test ( 'MySecureP@ssword' ) ) . toBe ( false )
97+
98+ // Missing special characters
99+ expect ( validator . test ( 'MySecurePass123' ) ) . toBe ( false )
100+ } )
101+
102+ test ( 'comprehensive password validation with multiple rules' , ( ) => {
103+ const validator = v . password ( )
104+ . minLength ( 8 )
105+ . maxLength ( 20 )
106+ . hasUppercase ( )
107+ . hasLowercase ( )
108+ . hasNumbers ( )
109+ . hasSpecialCharacters ( )
110+
111+ const result = validator . validate ( 'weak' )
112+ expect ( result . valid ) . toBe ( false )
113+ expect ( result . errors . length ) . toBeGreaterThan ( 0 )
114+
115+ // Check specific error messages
116+ const errorMessages = result . errors . map ( e => e . message )
117+ expect ( errorMessages ) . toContain ( 'Password must be at least 8 characters long' )
118+ expect ( errorMessages ) . toContain ( 'Password must contain at least one uppercase letter' )
119+ expect ( errorMessages ) . toContain ( 'Password must contain at least one number' )
120+ expect ( errorMessages ) . toContain ( 'Password must contain at least one special character' )
121+
122+ // Test a valid password
123+ const validResult = validator . validate ( 'MySecureP@ss123' )
124+ expect ( validResult . valid ) . toBe ( true )
125+ expect ( validResult . errors ) . toHaveLength ( 0 )
126+ } )
127+
128+ test ( 'alphanumeric password validation' , ( ) => {
129+ const validator = v . password ( ) . alphanumeric ( )
130+ expect ( validator . test ( 'Password123' ) ) . toBe ( true )
131+ expect ( validator . test ( 'Password@123' ) ) . toBe ( false )
132+ } )
133+ } )
134+
57135 describe ( 'Number Validator' , ( ) => {
58136 test ( 'basic number validation' , ( ) => {
59137 const validator = v . number ( )
0 commit comments