unique function

LivrRule unique(
  1. List<Object?> args,
  2. RuleContext context
)

unique: the elements of the list must be unique. Without arguments the elements are primitive values, compared as strings (1 and '1' are duplicates). With keys, {unique: ['country', 'zip']}, the elements are objects, duplicates when the values of all the keys are equal. Empty elements and objects with an empty key value are ignored. Duplicates are one NOT_UNIQUE_ITEMS for the field, an element of a wrong type is FORMAT_ERROR.

Implementation

LivrRule unique(List<Object?> args, RuleContext context) {
  if (args.isEmpty) return _uniqueValues;

  // A number is its string form, a boolean is not a key name
  final keys = List<String>.generate(args.length, (i) {
    final key = args[i];
    if (key is! String && !(key is num && key.isFinite)) {
      throw LivrSchemaException('Rule [unique] expects key names, got [$key]');
    }
    return livrToString(key);
  }, growable: false);

  if (keys.length == 1) return _uniqueKey(keys[0]);
  return _uniqueKeys(keys);
}