DirkScripts
Docs
Validate
βΉοΈ Shared Module
Build validation chains to assert value types, ranges, and custom constraints.
#validate
Creates a new validation chain for a value.
lualocal name = validate(playerName).string(32).min(1).done() local count = validate(amount).number().min(1).max(100).done()
#Methods
| Method | Parameters | Description |
|---|---|---|
.string(len?) | len: number β optional max length | Assert value is a string |
.number() | β | Assert value is a number |
.boolean() | β | Assert value is a boolean |
.min(min) | min: number | Minimum value (number) or minimum length (string) |
.max(max) | max: number | Maximum value (number) or maximum length (string) |
.refine(fn, err?) | fn: function(value) β boolean, err: string | Custom validation function |
.default(value) | value: any | Use this value if the input is nil |
.done() | β | Finalize and return the raw value |
The chain can also be called directly β validate(x).string()() is equivalent to .done().
#Example
lualocal function processPayment(data) local amount = validate(data.amount).number().min(1).max(10000).done() local note = validate(data.note).string(200).default('').done() local rush = validate(data.rush).boolean().default(false).done() -- Custom validation local code = validate(data.code).string().refine(function(v) return #v == 6 end, 'Code must be 6 characters').done() end
