-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasserts.mdc
More file actions
245 lines (190 loc) · 7.05 KB
/
asserts.mdc
File metadata and controls
245 lines (190 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
---
description: Asserts module API reference for runtime type validation
alwaysApply: false
---
# Asserts Module
Runtime type validation for Luau. Located at `ReplicatedStorage.UserGenerated.Lang.Asserts`. In plugins, the module may be located elsewhere—check the plugin's import paths.
## Basic Usage
```luau
-- Direct validation (throws on failure)
local n = Asserts.Integer(someValue)
local s = Asserts.String(input)
-- Composed validators return functions
local validatePositiveInt = Asserts.IntegerPositive
local value = validatePositiveInt(input)
```
## Primitives
| Function | Description |
|----------|-------------|
| `Number(x)` | Any number |
| `String(x)` | UTF-8 validated string |
| `RawString(x)` | String without UTF-8 validation |
| `Boolean(x)` | Boolean value |
| `Buffer(x)` | Buffer type |
| `Function(x)` | Function type |
| `Thread(x)` | Coroutine thread |
| `Any(x)` | Passthrough, no validation |
## Numbers
| Function | Description |
|----------|-------------|
| `Finite(x)` | Excludes NaN and infinity |
| `FinitePositive(x)` | Finite and > 0 |
| `FiniteNonNegative(x)` | Finite and >= 0 |
| `Positive(x)` | > 0 (excludes NaN) |
| `NonNegative(x)` | >= 0 (excludes NaN) |
| `Real(x)` | Excludes NaN only |
| `Integer(x)` | Whole number, finite |
| `IntegerPositive(x)` | Integer >= 1 |
| `IntegerNonNegative(x)` | Integer >= 0 |
| `Index(x)` | Alias for IntegerPositive |
| `Unsigned32(x)` | 32-bit unsigned integer (direct validator) |
| `UInt32` | Returns a validator for 32-bit unsigned integers (factory) |
| `Range(a, b)` | Returns validator for [a, b] |
| `IntegerRange(a, b)` | Returns validator for integer [a, b] |
## Strings
| Function | Description |
|----------|-------------|
| `ASCII(x)` | Printable ASCII only (32-126) |
| `ASCIIRange(a, b)` | ASCII with length in [a, b] |
| `StringRange(a, b)` | UTF-8 with char count in [a, b] |
| `Pattern(pattern)` | Returns validator matching Lua pattern |
| `HexLower(x)` | Lowercase hex string |
| `HexUpper(x)` | Uppercase hex string |
| `Base64(x)` | Valid Base64 encoding |
| `UUID(x)` | UUID with dashes (36 chars) |
| `UUIDStripped(x)` | UUID without dashes (32 chars) |
| `IsoDate(x)` | ISO 8601 date string |
## Roblox Types
| Function | Description |
|----------|-------------|
| `Vector2(x)`, `Vector3(x)` | Basic vector validation |
| `Vector2Finite(x)`, `Vector3Finite(x)` | Vectors with finite components |
| `Vector2Unit(x)`, `Vector3Unit(x)` | Unit vectors (magnitude ≈ 1) |
| `Vector3Positive(x)` | All components > 0 |
| `CFrame(x)`, `CFrameFinite(x)` | CFrame validation |
| `UDim(x)`, `UDim2(x)`, `Rect(x)` | UI types |
| `Region3(x)`, `Region3int16(x)` | Region types |
| `Vector2int16(x)`, `Vector3int16(x)` | Integer vector types |
| `PhysicalProperties(x)` | PhysicalProperties validation |
## Instances
| Function | Description |
|----------|-------------|
| `Instance(x)` | Any Instance |
| `InstanceOf<T>(className)` | Returns validator using IsA |
| `InstanceClass<T>(className)` | Returns validator for exact ClassName |
| `Player(x)`, `Model(x)`, `Humanoid(x)` | Specific instance types |
| `BasePart(x)`, `Part(x)`, `MeshPart(x)` | Part types |
| `Sound(x)`, `Animation(x)`, `ParticleEmitter(x)` | Other common types |
## Enums
```luau
-- Validate EnumItem of specific type
local validateMaterial = Asserts.Enum(Enum.Material)
local mat = validateMaterial(Enum.Material.Plastic)
-- Validate enum by numeric value
local validateMaterialValue = Asserts.EnumValue(Enum.Material)
local matValue = validateMaterialValue(256)
```
## Collections
### Arrays
```luau
-- Validate array elements
local validateNumbers = Asserts.Array(Asserts.Integer)
local nums = validateNumbers({1, 2, 3})
-- Array with unique elements
local validateUniqueStrings = Asserts.UniqueArray(Asserts.String)
-- Transform elements (Coerce variants)
local toIntegers = Asserts.ArrayCoerce(Asserts.ToNumber(Asserts.Integer))
local nums = toIntegers({"1", "2", "3"}) -- {1, 2, 3}
-- Unique array with coercion
local toUniqueInts = Asserts.UniqueArrayCoerce(Asserts.ToNumber(Asserts.Integer))
```
### Maps
```luau
-- Validate key-value pairs
local validateScores = Asserts.Map(Asserts.String, Asserts.Integer)
local scores = validateScores({alice = 100, bob = 200})
-- Optional length limit
local limitedMap = Asserts.Map(Asserts.String, Asserts.Any, 10)
-- Map with key/value coercion
local coercedMap = Asserts.MapCoerce(Asserts.String, Asserts.ToNumber(Asserts.Integer))
```
### Tables (Structured Objects)
```luau
-- Validate specific keys (strict - no extra keys allowed)
local validatePlayer = Asserts.Table({
name = Asserts.String,
score = Asserts.IntegerNonNegative,
active = Asserts.Boolean,
})
-- Permissive variant (ignores extra keys)
local validatePartial = Asserts.TablePermissive({
id = Asserts.String,
})
-- Transform values (Coerce variant)
local parseConfig = Asserts.TableCoerce({
port = Asserts.ToNumber(Asserts.IntegerPositive),
host = Asserts.String,
})
```
## Combinators
```luau
-- Optional (nil allowed)
local maybeString = Asserts.Optional(Asserts.String)
-- Default value when nil
local withDefault = Asserts.Default(Asserts.Integer, 0)
-- Union types (first match wins)
local stringOrNumber = Asserts.AnyOf(Asserts.String, Asserts.Number)
-- Intersection (all must pass)
local positiveFinite = Asserts.AllOf(Asserts.Positive, Asserts.Finite)
-- Exact value match
local validateVersion = Asserts.Equals(1)
-- Set membership
local validateRarity = Asserts.Set({"Common", "Rare", "Epic", "Legendary"})
-- String to number conversion
local parseInteger = Asserts.ToNumber(Asserts.Integer)
```
## Special Validators
```luau
-- DataStore-compatible types
Asserts.Storable(data) -- boolean | number | string | buffer | nested tables
-- Network-replicable types (includes Roblox types)
Asserts.Networkable(data) -- Storable + CFrame, Vector3, Instance, etc.
-- Table without metatable
Asserts.NoMetatable(t)
Asserts.AnyTable(t) -- Allows metatables
```
## Predicate Functions
For conditional logic without throwing:
```luau
if Asserts.IsFinite(x) then ... end
if Asserts.IsInteger(x) then ... end
if Asserts.IsASCII(s) then ... end
if Asserts.IsCFrameFinite(cf) then ... end
if Asserts.IsVector2Finite(v) then ... end
if Asserts.IsVector3Finite(v) then ... end
```
## Exported Types
```luau
Asserts.Map<K, V> -- Type returned by Map validator
Asserts.MapCoerce<K, V, K2, V2> -- Type returned by MapCoerce validator
Asserts.Table<T> -- Type returned by Table validator
Asserts.TableCoerce<T, T2> -- Type returned by TableCoerce validator
Asserts.Storable -- DataStore-compatible value type
Asserts.Networkable -- Network-replicable value type
```
## Error Handling
Validators throw descriptive errors with key paths:
```luau
-- Error: "Integer $[2]" (second array element failed)
-- Error: "String $["name"]" (name field failed)
-- Error: "Range(1,100) $["config"]["port"]" (nested path)
```
Use `pcall` to catch validation errors:
```luau
local success, result = pcall(Asserts.Table({
id = Asserts.UUID,
}), untrustedInput)
if not success then
warn("Validation failed:", result)
end
```