@digitalcube/username-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Username Validator

Install

$ npm i -S @digitalcube/username-validator

Usage

In React Hook

import { validateUsername, isInvalidUsernameError } from  '@digitalcube/username-validator'
const useUsernameValidator = (username: string) => {
    const [errorMessage, putErrorMessage] = useState('')
    useEffect(() => {
        try {
            validateUsername(username)
            putErrorMessage('')
        } catch (e) {
            if (isInvalidUsernameError(e)) {
                putErrorMessage(e.message)
            }
        }
    }, [username, putErrorMessage])
    return errorMessage
}

Custom username block lists

import { validateUsername, isInvalidUsernameError } from  '@digitalcube/username-validator'
const additionalBlockLists = ['hello', 'world']
const useUsernameValidator = (username: string) => {
    const [errorMessage, putErrorMessage] = useState('')
    useEffect(() => {
        try {
            validateUsername(username, {blockLists: additionalBlockLists})
            putErrorMessage('')
        } catch (e) {
            if (isInvalidUsernameError(e)) {
                putErrorMessage(e.message)
            }
        }
    }, [username, putErrorMessage])
    return errorMessage
}

JavaScript

const {
    validateUsername
} = require('@digitalcube/username-validator')

const test = (username) => {
    try {
        validateUsername(username)
        console.log(`=== Validation succeeded ===`)
        console.log(`Username ${username} can use`)
    } catch (e) {
        console.error(`=== ${e.name} ===`)
        console.error(e.message)
    }
    console.log('\n')
}


test(new Array(65).fill('a').join(''))
test('test')
test('test1')
test('test2')
test('shifter')
test('hello')
test('i09Nxxpv%ZqJ')


=== InvalidUsernameError ===
Username should be 64 letters or less and is only allowed to contain alphanumeric, dot (.), hyphen (-) and underscore (_).


=== InvalidUsernameError ===
Username: test is already exists.


=== InvalidUsernameError ===
Username: test1 is already exists.


=== InvalidUsernameError ===
Username: test2 is already exists.


=== InvalidUsernameError ===
Username: shifter is already exists.


=== Validation succeeded ===
Username hello can use


=== InvalidUsernameError ===
Username should be 64 letters or less and is only allowed to contain alphanumeric, dot (.), hyphen (-) and underscore (_).

Readme

Keywords

none

Package Sidebar

Install

npm i @digitalcube/username-validator

Weekly Downloads

11

Version

1.0.2

License

MIT

Unpacked Size

163 kB

Total Files

20

Last publish

Collaborators

  • emaildano
  • hideokamoto