like function

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

like: the value must match the regular expression. Flags are the second argument: {like: ['^[a-z]+$', 'i']}; only i (ignore case) is supported. The output is the value as a string.

Implementation

LivrRule like(List<Object?> args, RuleContext context) {
  final pattern = stringArg(args, 0, 'like');
  final flags = args.length > 1 ? args[1] : null;
  final isIgnoreCase = flags is String && flags.contains('i');

  final re = _compile(pattern, isIgnoreCase);

  return (value, allValues) {
    if (isNoValue(value)) return RuleResult.valid;
    if (!isPrimitive(value)) return RuleResult.error('FORMAT_ERROR');

    final str = livrToString(value);
    if (!re.hasMatch(str)) return RuleResult.error('WRONG_FORMAT');

    return value is String ? RuleResult.valid : RuleResult.value(str);
  };
}