Skip to content

Commit

Permalink
feat: @IsStrongPassword() converter
Browse files Browse the repository at this point in the history
  • Loading branch information
invakid404 committed Feb 9, 2023
1 parent 4821a25 commit dead663
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/defaultConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,41 @@ export const defaultConverters: ISchemaConverters = {
type: 'array',
uniqueItems: true,
},
[cv.IS_STRONG_PASSWORD]: (meta, options) => {
const passwordOptions: cv.IsStrongPasswordOptions = meta.constraints[0]

const requireChars = (chars: string, amount: number): string => {
const charMatcher = chars === '*' ? '' : `*(?:[^${chars}]*[${chars}])`

return `(?=.${charMatcher}{${amount}})`
}

const requirements = (
[
['*', passwordOptions.minLength],
['a-z', passwordOptions.minLowercase],
['A-Z', passwordOptions.minUppercase],
['0-9', passwordOptions.minNumbers],
[options.passwordSymbols, passwordOptions.minSymbols],
] as const
)
.map(([chars, amount]) => {
if (!amount) {
return
}

return requireChars(chars, amount)
})
.filter(Boolean)
.join('')

const pattern = `^${requirements}.*$`

return {
type: 'string',
pattern,
}
},
}

function getPropType(target: object, property: string) {
Expand Down
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ export interface IOptions extends ValidatorOptions {
* Defaults to `name`, i.e., class name.
*/
schemaNameField: string

/**
* Characters that are considered symbols n passwords.
* Defaults to the symbol character set of `validator.js`.
*/
passwordSymbols: string
}

export const defaultOptions: IOptions = {
additionalConverters: {},
classValidatorMetadataStorage: getMetadataStorage(),
refPointerPrefix: '#/definitions/',
schemaNameField: 'name',
passwordSymbols: '-#!$@£%^&*()_+|~=`{}\\[\\]:";\'<>?,.\\/ ',
}

0 comments on commit dead663

Please sign in to comment.