Variable Validator Utility Module (Type-Safe Data Validation)

I made a utility module to simplify validating values in Roblox, especially:

  • Input from TextBoxes
  • Data sent from client → server
  • General sanity checks before processing/saving data

The module is type-safe, easy to use, and designed to return clear feedback so you can show proper error messages to players or debug issues cleanly and quickly.


At the time im posting this, there is only two types of variables that can be validated:

Numbers
export type _NumberRules = {
	-- value must not be nil
	required: boolean?,

	-- value must be a whole number (no decimals)
	integer: boolean?,

	-- value must be >= min
	min: number?,

	-- value must be <= max
	max: number?,

	-- value must be between [min, max] (inclusive)
	-- usage: between = {min, max}
	between: {number}?,

	-- value must be strictly greater than this number
	greaterThan: number?,

	-- value must be greater than or equal to this number
	greatenThanOrEqual: number?,

	-- value must be strictly less than this number
	lessThan: number?,

	-- value must be less than or equal to this number
	lessThanOrEqual: number?,

	-- value must be one of the provided numbers
	-- usage: ["in"] = {1, 2, 3}
	["in"]: {number}?,

	-- value must NOT be any of the provided numbers
	notIn: {number}?,

	-- value must be a multiple of this number
	-- example: multipleOf = 5 → 15 valid, 12 invalid
	multipleOf: number?,
}
Strings
export type _StringRules = {
	-- value must not be nil
	required: boolean?,

	-- string length must be >= min
	min: number?,

	-- string length must be <= max
	max: number?,

	-- string length must be exactly this value
	size: number?,

	-- string length must be between [min, max] (inclusive)
	-- usage: between = {min, max}
	between: {number}?,

	-- string must start with ONE of these prefixes
	-- usage: starts_with = {"Mr", "Dr"}
	startsWith: {string}?,

	-- string must end with ONE of these suffixes
	-- usage: ends_with = {".png", ".jpg"}
	endsWith: {string}?,

	-- string must contain ALL of these substrings
	-- usage: contains = {"hello", "world"}
	contains: {string}?,

	-- string must match this Lua pattern
	-- example: "^[A-Za-z0-9]+$"
	regex: string?,

	-- string must exactly match one of the provided values
	-- usage: ["in"] = {"red", "blue"}
	["in"]: {string}?,

	-- string must NOT match any of the provided values
	notIn: {string}?,

	-- string must contain only letters (A-Z, a-z)
	alpha: boolean?,

	-- string must contain only letters and numbers
	alphaNum: boolean?,

	-- string must contain only letters, numbers, underscores, or dashes
	alphaDash: boolean?,
}

Usage Example:

--[[
Validating a string:
- must not be required (not nil)
- must be only uppercase
- it's length must be between 3 and 10 characters
]]

local value = ...

local valid, rule, message = ValidatorUtil.String(
	value, -- value to be examined
	{
		required = true,
		regex = "^[A-Z]+$", -- rules that value needs to respect
		between = {3, 10},
	},
	{ -- (optional) if not specified, the returning message will be the default for the corresponding failing rule
		required = "Value cannot be empty",
		regex = "Only uppercase letters allowed", -- fail messages for each rule
		between = "Must be between 3 and 10 characters",
		type = "Value must be a string", -- should always have a "type" message
	}
)

if not valid then
	print(message) -- prints the fail message for the corresponding failing rule
end

How to get it

Option 1 → Recommended

→ Get if from Creator Store

Option 2 → Easiest & Fastest

→ Copy from Github Gist (could not be up to date)


If you make any changes or add a new type to be validated to the module, please mention here and i will add to my version of the module and update it on the Creator Store :+1:

4 Likes