UNPKG

@type-ddd/password

Version:

Library that provides TypeScript type definitions for handling Password in Domain-Driven Design contexts. It facilitates the validation and manipulation of passwords.

70 lines (69 loc) 2.32 kB
import { Result, ValueObject } from 'rich-domain'; import { ILength } from './utils'; declare class Password extends ValueObject<string> { protected static readonly MAX_LENGTH = 22; protected static readonly MIN_LENGTH = 5; protected static readonly REGEX: RegExp; protected static readonly MESSAGE: string; private constructor(); /** * @returns value as string */ value(): string; /** * * @description compare plainText with encrypted password * @param plainText plainText not encrypted to compare with encrypted password * @returns true if match else false */ compare(plainText: string): boolean; /** * * @returns true if instance value is encrypted else false */ isEncrypted(): boolean; /** * * @returns true if provided value is encrypted else false */ static isEncrypted(value: string): boolean; /** * * @param length password length as number 8/10/12/14/16/18 * @returns PasswordValueObject * @default 12 chars or greater is recommended for strongest password */ static random(length?: ILength): Password; /** * @summary Encrypts the password. * @description This method encrypts the password using bcrypt hashing algorithm. If the password is already encrypted, it returns the encrypted password as it is. Otherwise, it generates a salt, hashes the password with the salt, and returns the encrypted password. * @returns A Password object representing the encrypted password. */ encrypt(): Password; /** * * @param value check if password has a valid value length * @returns true if is all ok or false else not */ static isValid(value: string): boolean; /** * * @param value check if password has a valid value length * @returns true if is all ok or false else not */ static isValidProps(value: string): boolean; /** * * @param value value as string * @returns instance of Password or throw an error */ static init(value: string): Password; /** * * @param value password to create * @returns Result of PasswordValueObject */ static create(value: string): Result<Password | null>; } export { Password }; export default Password;