File tree 2 files changed +48
-0
lines changed
2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -365,6 +365,47 @@ export const defaultConverters: ISchemaConverters = {
365
365
type : 'array' ,
366
366
uniqueItems : true ,
367
367
} ,
368
+ [ cv . IS_STRONG_PASSWORD ] : ( meta , options ) => {
369
+ const {
370
+ minLength = 8 ,
371
+ minLowercase = 1 ,
372
+ minUppercase = 1 ,
373
+ minNumbers = 1 ,
374
+ minSymbols = 1 ,
375
+ } : cv . IsStrongPasswordOptions = meta . constraints [ 0 ]
376
+
377
+ const requireChars = ( chars : string , amount : number ) : string => {
378
+ const charMatcher = chars === '*' ? '' : `*(?:[^${ chars } ]*[${ chars } ])`
379
+
380
+ return `(?=.${ charMatcher } {${ amount } })`
381
+ }
382
+
383
+ const requirements = (
384
+ [
385
+ [ '*' , minLength ] ,
386
+ [ 'a-z' , minLowercase ] ,
387
+ [ 'A-Z' , minUppercase ] ,
388
+ [ '0-9' , minNumbers ] ,
389
+ [ options . passwordSymbols , minSymbols ] ,
390
+ ] as const
391
+ )
392
+ . map ( ( [ chars , amount ] ) => {
393
+ if ( ! amount ) {
394
+ return
395
+ }
396
+
397
+ return requireChars ( chars , amount )
398
+ } )
399
+ . filter ( Boolean )
400
+ . join ( '' )
401
+
402
+ const pattern = `^${ requirements } .*$`
403
+
404
+ return {
405
+ type : 'string' ,
406
+ pattern,
407
+ }
408
+ } ,
368
409
}
369
410
370
411
function getPropType ( target : object , property : string ) {
Original file line number Diff line number Diff line change @@ -40,11 +40,18 @@ export interface IOptions extends ValidatorOptions {
40
40
* Defaults to `name`, i.e., class name.
41
41
*/
42
42
schemaNameField : string
43
+
44
+ /**
45
+ * Characters that are considered symbols n passwords.
46
+ * Defaults to the symbol character set of `validator.js`.
47
+ */
48
+ passwordSymbols : string
43
49
}
44
50
45
51
export const defaultOptions : IOptions = {
46
52
additionalConverters : { } ,
47
53
classValidatorMetadataStorage : getMetadataStorage ( ) ,
48
54
refPointerPrefix : '#/definitions/' ,
49
55
schemaNameField : 'name' ,
56
+ passwordSymbols : '-#!$@£%^&*()_+|~=`{}\\[\\]:";\'<>?,.\\/ ' ,
50
57
}
You can’t perform that action at this time.
0 commit comments