uuid function
uuid: the value must be a UUID (RFC 9562), 8-4-4-4-12 hexadecimal
digits. Without arguments the version is 1-8 and the variant is RFC 9562,
and the nil and the max UUID pass too. With versions, {uuid: 'v4'} or
{uuid: ['v4', 'v7']}, the version must be one of them. The output is
the value as a string.
Implementation
LivrRule uuid(List<Object?> args, RuleContext context) {
if (args.isEmpty) return _anyUuid;
// Allowed version digits as bits: "v4" is 1 << 4
var allowedVersions = 0;
for (final version in args) {
final digit =
version is String &&
version.length == 2 &&
version.codeUnitAt(0) ==
0x76 // "v"
? version.codeUnitAt(1) - 0x30
: 0;
if (digit < 1 || digit > 8) {
throw LivrSchemaException(
'Rule [uuid] expects versions "v1"-"v8", got [$version]',
);
}
allowedVersions |= 1 << digit;
}
return (value, allValues) => _check(value, (str) {
if (!_isUuid(str) || !_hasVariant(str)) return false;
final version = str.codeUnitAt(14) - 0x30;
return version >= 1 &&
version <= 8 &&
allowedVersions & (1 << version) != 0;
});
}