Lua data validation [Tool]

Hi there fellow developers!

A very basic data validation system for FiveM ( Not really FiveM, it’s environment agnostic, meaning it can be used anywhere where Lua runs with minor tweaks ). During my time creating custom resources to sharpen my general programming skills, I came across an issue where sometimes it would be awesome to surely know that the data being passed around the resource would be pure and the type I expect it to be since it can only be detected during runtime. And lua-vBuilder-fivem was born.

This resource allows you to safely validate your data by creating custom schemas, here’s a very short and quick example.

local playerNameValidation <const> = vBuilder:string().min(1).max(10)
local result, error = playerNameValidation.parse("John Doe")

print(json.encode(result)) -- "John Doe"

It supports these data types: arrays, objects, strings, numbers, booleans and even custom enums and unions.

A more detailed README is published in the GitHub repository of this resource.

aquapha/lua-vBuilder-fivem: Data validation builder for FiveM Lua (github.com)

A B
Code is accessible Yes
Subscription-based No
Lines (approximately) 421
Requirements None
Support Yes/OSS
11 Likes

Greate release, it looks fantastic! It reminds me of zod for JavaScript.

1 Like

Glad to hear that! You are correct, zod was used as a refference for this.

1 Like

Zod is a great library!
Keep up the work, looks very impressive!

2 Likes

2024-10-16 Changelog

Added support for unions

Caveats

  1. Can’t be used within array builders. Focus on creating pure array elements instead of unions.

Example

local playerJobNameOrIdUnion <const> = vBuilder:union({
  vBuilder:string(),
  vBuilder:number(),
})

local validString <const> = playerJobNameOrIdUnion.parse("Police")
print(validString) -- "Police"

local validNumber <const> = playerJobNameOrIdUnion.parse(123)
print(validNumber) -- 123

local _, invalidError <const> = playerJobNameOrIdUnion.parse({})
print(json.encode(invalidError)) -- { code = "invalid_union", message = "Invalid union. Received: table, expected: string, number", path = "" }