| 1 |
context("Lua shape validation", function() |
| 2 |
local T = require "lua_shape.core" |
| 3 |
local Registry = require "lua_shape.registry" |
| 4 |
|
| 5 |
-- Scalar type tests |
| 6 |
context("Scalar types", function() |
| 7 |
test("String type - valid", function() |
| 8 |
local schema = T.string() |
| 9 |
local ok, val = schema:check("hello") |
| 10 |
assert_true(ok) |
| 11 |
assert_equal(val, "hello") |
| 12 |
end) |
| 13 |
|
| 14 |
test("String type - invalid", function() |
| 15 |
local schema = T.string() |
| 16 |
local ok, err = schema:check(123) |
| 17 |
assert_false(ok) |
| 18 |
assert_equal(err.kind, "type_mismatch") |
| 19 |
assert_equal(err.details.expected, "string") |
| 20 |
assert_equal(err.details.got, "number") |
| 21 |
end) |
| 22 |
|
| 23 |
test("String with length constraints", function() |
| 24 |
local schema = T.string({ min_len = 3, max_len = 10 }) |
| 25 |
|
| 26 |
local ok, val = schema:check("hello") |
| 27 |
assert_true(ok) |
| 28 |
|
| 29 |
ok = schema:check("hi") |
| 30 |
assert_false(ok) |
| 31 |
|
| 32 |
ok = schema:check("this is too long") |
| 33 |
assert_false(ok) |
| 34 |
end) |
| 35 |
|
| 36 |
test("String with pattern", function() |
| 37 |
local schema = T.string({ pattern = "^%d+$" }) |
| 38 |
|
| 39 |
local ok = schema:check("123") |
| 40 |
assert_true(ok) |
| 41 |
|
| 42 |
ok = schema:check("abc") |
| 43 |
assert_false(ok) |
| 44 |
end) |
| 45 |
|
| 46 |
test("Integer type with range", function() |
| 47 |
local schema = T.integer({ min = 0, max = 100 }) |
| 48 |
|
| 49 |
local ok, val = schema:check(50) |
| 50 |
assert_true(ok) |
| 51 |
assert_equal(val, 50) |
| 52 |
|
| 53 |
ok = schema:check(150) |
| 54 |
assert_false(ok) |
| 55 |
|
| 56 |
ok = schema:check(-10) |
| 57 |
assert_false(ok) |
| 58 |
end) |
| 59 |
|
| 60 |
test("Integer rejects non-integer", function() |
| 61 |
local schema = T.integer() |
| 62 |
local ok, err = schema:check(3.14) |
| 63 |
assert_false(ok) |
| 64 |
assert_equal(err.kind, "constraint_violation") |
| 65 |
assert_equal(err.details.constraint, "integer") |
| 66 |
end) |
| 67 |
|
| 68 |
test("Number accepts integer and float", function() |
| 69 |
local schema = T.number({ min = 0, max = 10 }) |
| 70 |
|
| 71 |
local ok = schema:check(5) |
| 72 |
assert_true(ok) |
| 73 |
|
| 74 |
ok = schema:check(5.5) |
| 75 |
assert_true(ok) |
| 76 |
|
| 77 |
ok = schema:check(15) |
| 78 |
assert_false(ok) |
| 79 |
end) |
| 80 |
|
| 81 |
test("Boolean type", function() |
| 82 |
local schema = T.boolean() |
| 83 |
|
| 84 |
local ok, val = schema:check(true) |
| 85 |
assert_true(ok) |
| 86 |
assert_equal(val, true) |
| 87 |
|
| 88 |
ok, val = schema:check(false) |
| 89 |
assert_true(ok) |
| 90 |
assert_equal(val, false) |
| 91 |
|
| 92 |
ok = schema:check("true") |
| 93 |
assert_false(ok) |
| 94 |
end) |
| 95 |
|
| 96 |
test("Enum type", function() |
| 97 |
local schema = T.enum({"debug", "info", "warning", "error"}) |
| 98 |
|
| 99 |
local ok = schema:check("info") |
| 100 |
assert_true(ok) |
| 101 |
|
| 102 |
ok, err = schema:check("trace") |
| 103 |
assert_false(ok) |
| 104 |
assert_equal(err.kind, "enum_mismatch") |
| 105 |
end) |
| 106 |
|
| 107 |
test("Literal type", function() |
| 108 |
local schema = T.literal("exact_value") |
| 109 |
|
| 110 |
local ok = schema:check("exact_value") |
| 111 |
assert_true(ok) |
| 112 |
|
| 113 |
ok = schema:check("other_value") |
| 114 |
assert_false(ok) |
| 115 |
end) |
| 116 |
end) |
| 117 |
|
| 118 |
-- Array type tests |
| 119 |
context("Array type", function() |
| 120 |
test("Array of strings - valid", function() |
| 121 |
local schema = T.array(T.string()) |
| 122 |
local ok, val = schema:check({"foo", "bar", "baz"}) |
| 123 |
assert_true(ok) |
| 124 |
assert_rspamd_table_eq({expect = {"foo", "bar", "baz"}, actual = val}) |
| 125 |
end) |
| 126 |
|
| 127 |
test("Array of strings - invalid item", function() |
| 128 |
local schema = T.array(T.string()) |
| 129 |
local ok, err = schema:check({"foo", 123, "baz"}) |
| 130 |
assert_false(ok) |
| 131 |
assert_equal(err.kind, "array_items_invalid") |
| 132 |
end) |
| 133 |
|
| 134 |
test("Array with size constraints", function() |
| 135 |
local schema = T.array(T.string(), { min_items = 2, max_items = 5 }) |
| 136 |
|
| 137 |
local ok = schema:check({"a", "b", "c"}) |
| 138 |
assert_true(ok) |
| 139 |
|
| 140 |
ok = schema:check({"a"}) |
| 141 |
assert_false(ok) |
| 142 |
|
| 143 |
ok = schema:check({"a", "b", "c", "d", "e", "f"}) |
| 144 |
assert_false(ok) |
| 145 |
end) |
| 146 |
|
| 147 |
test("Array rejects table with non-array keys", function() |
| 148 |
local schema = T.array(T.string()) |
| 149 |
local ok, err = schema:check({foo = "bar"}) |
| 150 |
assert_false(ok) |
| 151 |
assert_equal(err.kind, "type_mismatch") |
| 152 |
end) |
| 153 |
end) |
| 154 |
|
| 155 |
-- Table type tests |
| 156 |
context("Table type", function() |
| 157 |
test("Simple table - valid", function() |
| 158 |
local schema = T.table({ |
| 159 |
name = T.string(), |
| 160 |
age = T.integer({ min = 0 }) |
| 161 |
}) |
| 162 |
|
| 163 |
local ok, val = schema:check({ name = "Alice", age = 30 }) |
| 164 |
assert_true(ok) |
| 165 |
assert_equal(val.name, "Alice") |
| 166 |
assert_equal(val.age, 30) |
| 167 |
end) |
| 168 |
|
| 169 |
test("Table - missing required field", function() |
| 170 |
local schema = T.table({ |
| 171 |
name = T.string(), |
| 172 |
age = T.integer() |
| 173 |
}) |
| 174 |
|
| 175 |
local ok, err = schema:check({ name = "Bob" }) |
| 176 |
assert_false(ok) |
| 177 |
assert_equal(err.kind, "table_invalid") |
| 178 |
end) |
| 179 |
|
| 180 |
test("Table - optional field", function() |
| 181 |
local schema = T.table({ |
| 182 |
name = T.string(), |
| 183 |
email = T.string():optional() |
| 184 |
}) |
| 185 |
|
| 186 |
local ok, val = schema:check({ name = "Charlie" }) |
| 187 |
assert_true(ok) |
| 188 |
assert_equal(val.name, "Charlie") |
| 189 |
assert_nil(val.email) |
| 190 |
end) |
| 191 |
|
| 192 |
test("Table - optional field with explicit syntax", function() |
| 193 |
local schema = T.table({ |
| 194 |
name = T.string(), |
| 195 |
email = { schema = T.string(), optional = true } |
| 196 |
}) |
| 197 |
|
| 198 |
local ok = schema:check({ name = "David" }) |
| 199 |
assert_true(ok) |
| 200 |
end) |
| 201 |
|
| 202 |
test("Table - default value in transform mode", function() |
| 203 |
local schema = T.table({ |
| 204 |
name = T.string(), |
| 205 |
port = { schema = T.integer(), optional = true, default = 8080 } |
| 206 |
}) |
| 207 |
|
| 208 |
local val, err = schema:transform({ name = "server" }) |
| 209 |
assert_nil(err) |
| 210 |
assert_equal(val.port, 8080) |
| 211 |
end) |
| 212 |
|
| 213 |
test("Table - closed table rejects unknown fields", function() |
| 214 |
local schema = T.table({ |
| 215 |
name = T.string() |
| 216 |
}, { open = false }) |
| 217 |
|
| 218 |
local ok, err = schema:check({ name = "Eve", extra = "field" }) |
| 219 |
assert_false(ok) |
| 220 |
assert_equal(err.kind, "table_invalid") |
| 221 |
end) |
| 222 |
|
| 223 |
test("Table - open table allows unknown fields", function() |
| 224 |
local schema = T.table({ |
| 225 |
name = T.string() |
| 226 |
}, { open = true }) |
| 227 |
|
| 228 |
local ok, val = schema:check({ name = "Frank", extra = "field" }) |
| 229 |
assert_true(ok) |
| 230 |
assert_equal(val.extra, "field") |
| 231 |
end) |
| 232 |
end) |
| 233 |
|
| 234 |
-- Optional and default tests |
| 235 |
context("Optional and default values", function() |
| 236 |
test("Optional wrapper", function() |
| 237 |
local schema = T.optional(T.string()) |
| 238 |
|
| 239 |
local ok, val = schema:check("hello") |
| 240 |
assert_true(ok) |
| 241 |
assert_equal(val, "hello") |
| 242 |
|
| 243 |
ok, val = schema:check(nil) |
| 244 |
assert_true(ok) |
| 245 |
assert_nil(val) |
| 246 |
end) |
| 247 |
|
| 248 |
test("Optional with default in check mode", function() |
| 249 |
local schema = T.string():with_default("default") |
| 250 |
|
| 251 |
local ok, val = schema:check(nil) |
| 252 |
assert_true(ok) |
| 253 |
assert_nil(val) -- check mode doesn't apply defaults |
| 254 |
end) |
| 255 |
|
| 256 |
test("Optional with default in transform mode", function() |
| 257 |
local schema = T.string():with_default("default") |
| 258 |
|
| 259 |
local val, err = schema:transform(nil) |
| 260 |
assert_nil(err) |
| 261 |
assert_equal(val, "default") |
| 262 |
end) |
| 263 |
end) |
| 264 |
|
| 265 |
-- Transform tests |
| 266 |
context("Transform support", function() |
| 267 |
test("Transform string to number", function() |
| 268 |
-- New semantics: first arg is accepted type, second is transformer |
| 269 |
local schema = T.transform(T.string(), tonumber) |
| 270 |
|
| 271 |
-- Transform mode: converts string to number |
| 272 |
local val, err = schema:transform("42") |
| 273 |
assert_nil(err) |
| 274 |
assert_equal(val, 42) |
| 275 |
|
| 276 |
-- Invalid string returns nil, which is caught as error |
| 277 |
val, err = schema:transform("not a number") |
| 278 |
assert_nil(val) |
| 279 |
assert_not_nil(err) |
| 280 |
assert_equal(err.kind, "transform_error") |
| 281 |
end) |
| 282 |
|
| 283 |
test("Transform validates input type first", function() |
| 284 |
local schema = T.transform(T.string(), tonumber) |
| 285 |
|
| 286 |
-- Number input fails because accepted type is string |
| 287 |
local val, err = schema:transform(42) |
| 288 |
assert_nil(val) |
| 289 |
assert_not_nil(err) |
| 290 |
assert_equal(err.kind, "type_mismatch") |
| 291 |
end) |
| 292 |
|
| 293 |
test("Transform accepts number or string using one_of", function() |
| 294 |
local schema = T.one_of({ |
| 295 |
T.number(), |
| 296 |
T.transform(T.string(), tonumber) |
| 297 |
}) |
| 298 |
|
| 299 |
-- Number passes through |
| 300 |
local val, err = schema:transform(42) |
| 301 |
assert_nil(err) |
| 302 |
assert_equal(val, 42) |
| 303 |
|
| 304 |
-- String is converted |
| 305 |
val, err = schema:transform("123") |
| 306 |
assert_nil(err) |
| 307 |
assert_equal(val, 123) |
| 308 |
end) |
| 309 |
|
| 310 |
test("Transform only in transform mode", function() |
| 311 |
local schema = T.transform(T.number(), function(val) |
| 312 |
return val * 2 |
| 313 |
end) |
| 314 |
|
| 315 |
-- Check mode: no transform, just validates input is number |
| 316 |
local ok, val = schema:check(5) |
| 317 |
assert_true(ok) |
| 318 |
assert_equal(val, 5) |
| 319 |
|
| 320 |
-- Transform mode: applies transform |
| 321 |
val, err = schema:transform(5) |
| 322 |
assert_nil(err) |
| 323 |
assert_equal(val, 10) |
| 324 |
end) |
| 325 |
|
| 326 |
test("Chained transform using :transform_with", function() |
| 327 |
local schema = T.string():transform_with(function(val) |
| 328 |
return val:upper() |
| 329 |
end) |
| 330 |
|
| 331 |
local val, err = schema:transform("hello") |
| 332 |
assert_nil(err) |
| 333 |
assert_equal(val, "HELLO") |
| 334 |
end) |
| 335 |
|
| 336 |
test("Transform result is not type-checked", function() |
| 337 |
-- Transform string to table - result type is not validated |
| 338 |
local schema = T.transform(T.string(), function(val) |
| 339 |
return { value = val } |
| 340 |
end) |
| 341 |
|
| 342 |
local val, err = schema:transform("test") |
| 343 |
assert_nil(err) |
| 344 |
assert_equal(type(val), "table") |
| 345 |
assert_equal(val.value, "test") |
| 346 |
end) |
| 347 |
end) |
| 348 |
|
| 349 |
-- one_of tests |
| 350 |
context("one_of type", function() |
| 351 |
test("one_of - first variant matches", function() |
| 352 |
local schema = T.one_of({ |
| 353 |
T.string(), |
| 354 |
T.integer() |
| 355 |
}) |
| 356 |
|
| 357 |
local ok, val = schema:check("text") |
| 358 |
assert_true(ok) |
| 359 |
assert_equal(val, "text") |
| 360 |
end) |
| 361 |
|
| 362 |
test("one_of - second variant matches", function() |
| 363 |
local schema = T.one_of({ |
| 364 |
T.string(), |
| 365 |
T.integer() |
| 366 |
}) |
| 367 |
|
| 368 |
local ok, val = schema:check(42) |
| 369 |
assert_true(ok) |
| 370 |
assert_equal(val, 42) |
| 371 |
end) |
| 372 |
|
| 373 |
test("one_of - no variant matches", function() |
| 374 |
local schema = T.one_of({ |
| 375 |
T.string(), |
| 376 |
T.integer() |
| 377 |
}) |
| 378 |
|
| 379 |
local ok, err = schema:check(true) |
| 380 |
assert_false(ok) |
| 381 |
assert_equal(err.kind, "one_of_mismatch") |
| 382 |
assert_equal(#err.details.variants, 2) |
| 383 |
end) |
| 384 |
|
| 385 |
test("one_of with named variants", function() |
| 386 |
local schema = T.one_of({ |
| 387 |
{ name = "string_variant", schema = T.string() }, |
| 388 |
{ name = "number_variant", schema = T.integer() } |
| 389 |
}) |
| 390 |
|
| 391 |
local ok = schema:check("text") |
| 392 |
assert_true(ok) |
| 393 |
end) |
| 394 |
|
| 395 |
test("one_of with table variants shows intersection", function() |
| 396 |
local schema = T.one_of({ |
| 397 |
{ |
| 398 |
name = "adult", |
| 399 |
schema = T.table({ |
| 400 |
name = T.string(), |
| 401 |
age = T.integer({ min = 18 }) |
| 402 |
}) |
| 403 |
}, |
| 404 |
{ |
| 405 |
name = "child", |
| 406 |
schema = T.table({ |
| 407 |
name = T.string(), |
| 408 |
age = T.integer({ max = 17 }) |
| 409 |
}) |
| 410 |
} |
| 411 |
}) |
| 412 |
|
| 413 |
local ok, err = schema:check({ age = 25 }) |
| 414 |
assert_false(ok) |
| 415 |
assert_equal(err.kind, "one_of_mismatch") |
| 416 |
-- Should have intersection showing common fields |
| 417 |
assert_not_nil(err.details.intersection) |
| 418 |
assert_not_nil(err.details.intersection.required_fields.name) |
| 419 |
assert_not_nil(err.details.intersection.required_fields.age) |
| 420 |
end) |
| 421 |
end) |
| 422 |
|
| 423 |
-- Registry tests |
| 424 |
context("Registry", function() |
| 425 |
test("Define and get schema", function() |
| 426 |
local reg = Registry.global() |
| 427 |
|
| 428 |
local schema = reg:define("test.simple", T.string()) |
| 429 |
assert_not_nil(schema) |
| 430 |
|
| 431 |
local retrieved = reg:get("test.simple") |
| 432 |
assert_not_nil(retrieved) |
| 433 |
end) |
| 434 |
|
| 435 |
test("Reference resolution", function() |
| 436 |
-- Use global registry but with unique schema ID |
| 437 |
local reg = Registry.global() |
| 438 |
local unique_id = "test.ref_user_" .. tostring(os.time()) |
| 439 |
|
| 440 |
local user_schema = T.table({ |
| 441 |
name = T.string(), |
| 442 |
email = T.string() |
| 443 |
}) |
| 444 |
|
| 445 |
reg:define(unique_id, user_schema) |
| 446 |
|
| 447 |
-- Create a simple test: resolve a ref directly |
| 448 |
local ref_schema = T.ref(unique_id) |
| 449 |
local resolved = reg:resolve_schema(ref_schema) |
| 450 |
|
| 451 |
-- Resolved schema should now be the actual table schema |
| 452 |
local ok, val = resolved:check({ name = "Alice", email = "[email protected]" }) |
| 453 |
assert_true(ok) |
| 454 |
assert_equal(val.name, "Alice") |
| 455 |
end) |
| 456 |
|
| 457 |
test("List registered schemas", function() |
| 458 |
local reg = Registry.global() |
| 459 |
local ids = reg:list() |
| 460 |
assert_not_nil(ids) |
| 461 |
assert_equal(type(ids), "table") |
| 462 |
end) |
| 463 |
end) |
| 464 |
|
| 465 |
-- Error formatting tests |
| 466 |
context("Error formatting", function() |
| 467 |
test("Format type mismatch error", function() |
| 468 |
local schema = T.string() |
| 469 |
local ok, err = schema:check(123) |
| 470 |
assert_false(ok) |
| 471 |
|
| 472 |
local formatted = T.format_error(err) |
| 473 |
assert_not_nil(formatted) |
| 474 |
assert_true(#formatted > 0) |
| 475 |
assert_true(formatted:find("type mismatch") ~= nil) |
| 476 |
end) |
| 477 |
|
| 478 |
test("Format constraint violation error", function() |
| 479 |
local schema = T.integer({ min = 0, max = 100 }) |
| 480 |
local ok, err = schema:check(150) |
| 481 |
assert_false(ok) |
| 482 |
|
| 483 |
local formatted = T.format_error(err) |
| 484 |
assert_not_nil(formatted) |
| 485 |
assert_true(formatted:find("constraint violation") ~= nil) |
| 486 |
assert_true(formatted:find("max") ~= nil) |
| 487 |
end) |
| 488 |
|
| 489 |
test("Format nested table errors", function() |
| 490 |
local schema = T.table({ |
| 491 |
name = T.string(), |
| 492 |
config = T.table({ |
| 493 |
port = T.integer({ min = 1, max = 65535 }) |
| 494 |
}) |
| 495 |
}) |
| 496 |
|
| 497 |
local ok, err = schema:check({ |
| 498 |
name = "server", |
| 499 |
config = { port = 99999 } |
| 500 |
}) |
| 501 |
assert_false(ok) |
| 502 |
|
| 503 |
local formatted = T.format_error(err) |
| 504 |
assert_not_nil(formatted) |
| 505 |
assert_true(formatted:find("config.port") ~= nil) |
| 506 |
end) |
| 507 |
|
| 508 |
test("Format one_of error with intersection", function() |
| 509 |
local schema = T.one_of({ |
| 510 |
{ |
| 511 |
name = "config_a", |
| 512 |
schema = T.table({ type = T.literal("a"), value_a = T.string() }) |
| 513 |
}, |
| 514 |
{ |
| 515 |
name = "config_b", |
| 516 |
schema = T.table({ type = T.literal("b"), value_b = T.integer() }) |
| 517 |
} |
| 518 |
}) |
| 519 |
|
| 520 |
local ok, err = schema:check({ value_a = "test" }) |
| 521 |
assert_false(ok) |
| 522 |
|
| 523 |
local formatted = T.format_error(err) |
| 524 |
assert_not_nil(formatted) |
| 525 |
assert_true(formatted:find("alternative") ~= nil) |
| 526 |
assert_true(formatted:find("type") ~= nil) |
| 527 |
end) |
| 528 |
end) |
| 529 |
|
| 530 |
-- Documentation support |
| 531 |
context("Documentation", function() |
| 532 |
test("Add documentation to schema", function() |
| 533 |
local schema = T.string():doc({ |
| 534 |
summary = "User name", |
| 535 |
description = "Full name of the user", |
| 536 |
examples = {"Alice", "Bob"} |
| 537 |
}) |
| 538 |
|
| 539 |
assert_not_nil(schema.opts.doc) |
| 540 |
assert_equal(schema.opts.doc.summary, "User name") |
| 541 |
end) |
| 542 |
|
| 543 |
test("Documentation doesn't affect validation", function() |
| 544 |
local schema = T.integer({ min = 0 }):doc({ summary = "Age" }) |
| 545 |
|
| 546 |
local ok = schema:check(25) |
| 547 |
assert_true(ok) |
| 548 |
|
| 549 |
ok = schema:check(-5) |
| 550 |
assert_false(ok) |
| 551 |
end) |
| 552 |
end) |
| 553 |
|
| 554 |
-- Utility functions |
| 555 |
context("Utility functions", function() |
| 556 |
test("Deep clone", function() |
| 557 |
local original = { |
| 558 |
a = 1, |
| 559 |
b = { c = 2, d = { e = 3 } } |
| 560 |
} |
| 561 |
|
| 562 |
local cloned = T.deep_clone(original) |
| 563 |
|
| 564 |
assert_rspamd_table_eq({expect = original, actual = cloned}) |
| 565 |
assert_not_equal(cloned, original) -- different object |
| 566 |
assert_not_equal(cloned.b, original.b) -- nested is cloned too |
| 567 |
end) |
| 568 |
|
| 569 |
test("Deep clone handles non-tables", function() |
| 570 |
assert_equal(T.deep_clone("string"), "string") |
| 571 |
assert_equal(T.deep_clone(42), 42) |
| 572 |
assert_equal(T.deep_clone(true), true) |
| 573 |
assert_nil(T.deep_clone(nil)) |
| 574 |
end) |
| 575 |
end) |
| 576 |
end) |