pivotValue function

IValidator pivotValue(
  1. dynamic transformFn(
    1. dynamic value
    ), {
  2. required IValidator child,
  3. required String errorMessage,
})

Unified helper for value pivoting operations.

This function creates a validator that transforms the input value using the provided transformFn, then validates the result with the child validator. If the transformation fails or returns null, it returns an invalid result with the provided errorMessage.

This preserves the behavior of the original functions:

  • If child validation succeeds, returns Result.valid with the transformed value
  • If child validation fails, returns the child's result (preserving error details)

This is used internally by pickKeys, pluckKey, and flattenMapKeys to reduce code duplication.

Implementation

IValidator pivotValue(
  dynamic Function(dynamic value) transformFn, {
  required IValidator child,
  required String errorMessage,
}) {
  return Validator((value) {
    final transformed = transformFn(value);

    if (transformed == null) {
      return Expectation(message: errorMessage, value: value).toInvalidResult();
    }

    final childResult = child.validate(transformed);
    if (childResult.isValid) return Result.valid(transformed);

    return childResult;
  });
}