ArgumentValidator - A robust assertion function for type validation

ArgumentValidator

ArgumentValidator is an assertion function for type validation.
GitHub Repository

Get it on Roblox Get it on Github


Features

  • Verbose Output: Clear and informative error messages.
  • Instance-only Validation: Keep out everything but instances.
  • Documentation: Extensive documentation within the script to help you understand.
  • Lightweight: No services and external dependencies.

Usage

Parameters:

  • arg: The value you want to validate.
  • expectedType: The type you expect the value to have (e.g., "string", "Instance", or a Roblox class like "Humanoid").
  • argName: The name of the argument being validated (used for error messages).

Here is a very simple usage:

local ArgumentValidator = require(script.ArgumentValidator)

local function PrintHelloWithPrefix(Prefix: string)
    ArgumentValidator(Prefix, "string", "Prefix")
    print(string.format("%s :: Hello!", Prefix))
end
  • PrintHelloWithPrefix("Example")
    Output: Example :: Hello!

  • PrintHelloWithPrefix(123)
    Output: Argument 'Prefix' only allows type 'string', got type 'number'

4 Likes

I mean it’s cool and all but it wouldn’t really replace a short

if arg ~= typeof("string") then print(arg,"no string") end

I guess this is just the modulized, in-place version

2 Likes

The idea behind ArgumentValidator is to provide a reusable and consistent way to get the job done. Think this as the assert() alternative for type validation. By the way, assert() is much slower when you do string formatting for the error message. String formatting is not done unless assertion fails in ArgumentValidator. For reliability, ArgumentValidator does not use assert().

1 Like

im not sure but wouldn’t putting arg: type automatically do it

Sadly, this is not typescript. If you want this, use roblox-ts

The example he gave is already a reusable and consistent way to get the job done. It’s also insane that the majority (literally half of it) of the script is just a goddamn copyright disclaimer.

What is the use-case for this function?

Is this meant for validating arguments in networking? If so, there are a couple more features I’d like to see before I’d consider it usable for in games.
For instance, I might pass more complex datastructures than simple strings or objects. Is this module easily extendible to cope with this? With what level of ease could this be extendible to cope with this?
Additionally, can this module be configured to not error when an incorrect argument is passed? I might have a reasonable backup plan in case a function call is invalid, and it seems silly to have to pcall() every function call to check for potential errors, not to mention that this might silence any errors generated further downstream, in which case my reasonable backup strategy might not actually be needed.
Furthermore, are there capabilities to report errors in a different way? Maybe you have an incorrectly configured remote event call which got through testing. In this case, it’s interesting to save the error to a datastore so it can be viewed and debugged later on. Adding to this, is it easy to report errors for a bulk of validation issues at once? I might be passing 5 incorrect arguments to a function, but because I only get an error message for the first argument, I’d need to restart the game 5 times to fully debug the issue.

Alternatively, is this to be used in between function calls, maybe to make sure incorrect data doesn’t get passed to a library? If so, I’d still like to see most of the points above implemented, but with one additional feature - the capacity to disable type checking via configuration. If one has a library function that’s expected to be called often, the type checking could be an unwanted overhead. In this case, I’d like to validation to be active when in studio, but disabled in production for efficiency.

1 Like