Username Validation Function Documentation

The validateUsername function is used to validate usernames. It returns an object with two properties: "isValid" (boolean) and "errorMsg" (string). The "isValid" property will be true if the username meets the specified criteria, and "errorMsg" will contain the error message if the username is invalid, or it will be null if the username is valid.

Import

The function can be imported using ES6 syntax from the "multiform-validator" package:

import { validateUsername } from 'multiform-validator';

Alternatively, you can import the function using CommonJS syntax with require (Node.js):

const { validateUsername } = require('multiform-validator');

Function Signature

interface OptionsParams {
	minLength?: number;
	maxLength?: number;
	errorMsg?: (string | null)[];
}

const defaultOptionsParams: OptionsParams = {
    minLength: 1,
    maxLength: infinity,
    errorMsg: defaultErrorMsg,
};

function validateUsername(
    username: string,
    { minLength, maxLength, errorMsg }: OptionsParams = defaultOptionsParams,
): { isValid: boolean, errorMsg: string | null };

Parameters

Default Error Messages

[
	'Invalid value passed',
	'Username too short',
	'This username is too long',
	'Username cannot contain spaces',
	'Cannot start with a number',
	'Cannot contain only numbers',
	'Unknown error'
]

Examples

const result1 = validateUsername("User999", {
    minLength: 8,
    maxLength: 20,
});
console.log(result1);
// Output: { isValid: true, errorMsg: null }

const customErrorMsg = ["Custom error 1", "Custom error 2"];
const result2 = validateUsername("User999", {
    minLength: 8,
    maxLength: 20,
    errorMsg: customErrorMsg,
});
console.log(result2);
// Output: { isValid: false, errorMsg: 'Username too short' }