I've also created a validation-type library, which also allows extensions and user defined types. It's a little more verbose as a result, but it might also be more powerful. Maybe it has something you can use? Feel free to scavenge whatever you want from it. I'm always a fan of tools that improve error messages, they are perhaps one of the best tools at a programmer's disposal.
It lets you do things like below. (This is by far the most complex validation schema I'm using, it shows a lot of the available features.
Also perhaps of note is that most of these are default validators, but "color" is a special case in that it's defined as a compound-validator; the system lets you define things like "a color is a table with 4 numeric keys that are numbers between 0 and 255" and then automatically unpacks the "color" validator into its normal schema (recursively, so you can also use existing compound validators to make new ones))
Code: Select all
schema = {
width = { required = true, schemaType = "oneOf", possibilities = {
{ schemaType = "fromList", list = {"wrap", "fill"} },
{ schemaType = "number" }
}},
height = { required = true, schemaType = "oneOf", possibilities = {
{ schemaType = "fromList", list = {"wrap", "fill"} },
{ schemaType = "number" }
}},
padding = { required = false, schemaType = "table", options = {
left = { required = true, schemaType = "number" },
right = { required = true, schemaType = "number" },
bottom = { required = true, schemaType = "number" },
top = { required = true, schemaType = "number" },
}},
backgroundColor = { required = false, schemaType = "color" },
gravity = { required = false, schemaType = "table", options = {
{ schemaType = "fromList", list = { "start", "center", "end" } },
{ schemaType = "fromList", list = { "start", "center", "end" } },
}},
border = { required = false, schemaType = "table", options = {
color = { required = true, schemaType = "color" },
thickness = { required = true, schemaType = "number" }
}},
weight = { required = false, schemaType = "number" },
visibility = { required = false, schemaType = "fromList", list = { "visible", "cloaked", "gone" } },
externalSignalHandlers = {
required = false,
schemaType = "dict",
keyValidator = { schemaType = "string" },
valueValidator = { schemaType = "oneOf", possibilities = {
{ schemaType = "function" },
{ schemaType = "fromList", list = { "o", "c" } },
{ schemaType = "number" },
{ schemaType = "table" },
}},
},
childSignalHandlers = {
required = false,
schemaType = "dict",
keyValidator = { schemaType = "string" },
valueValidator = { schemaType = "oneOf", possibilities = {
{ schemaType = "function" },
{ schemaType = "fromList", list = { "o", "c" } },
{ schemaType = "number" },
{ schemaType = "table" },
}},
}
}
This is how the "color" validator is defined:
Code: Select all
validator:addPartialSchema("color", { schemaType = "table", options = {
{ required = true, schemaType = "betweenNumbers", min = 0, max = 255 },
{ required = true, schemaType = "betweenNumbers", min = 0, max = 255 },
{ required = true, schemaType = "betweenNumbers", min = 0, max = 255 },
{ required = true, schemaType = "betweenNumbers", min = 0, max = 255 },
}
})
You can find the validator here, if you want to have a look.
https://github.com/ErikRoelofs/renderer ... idator.lua