https://github.com/digitalcube/username-validator
https://github.com/digitalcube/username-validator
Last synced: 11 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/digitalcube/username-validator
- Owner: digitalcube
- License: mit
- Created: 2020-09-29T10:03:37.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-09-29T11:00:58.000Z (over 5 years ago)
- Last Synced: 2024-08-08T15:18:45.970Z (over 1 year ago)
- Language: TypeScript
- Size: 178 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Username Validator
## Install
```bash
$ npm i -S @digitalcube/username-validator
```
## Usage
### In React Hook
```typescript
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
```typescript
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
```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 (_).
```