formula-one icon indicating copy to clipboard operation
formula-one copied to clipboard

No way to colocate validations and transformation

Open zgotsch opened this issue 6 years ago • 0 comments

Oftentimes, it is valuable to do validations and transformations in the same place. Example:

type DraftPortPair = {|
  origin: null | Port,
  destination: null | Port,
|};

type PortPair = {|
  origin: Port,
  destination: Port,
|};

function validatePortPair(draft: DraftPortPair): Result<PortPair, Array<string>> {
  const errors = [];
  if (draft.origin == null) {
    errors.push("Missing origin port");
  }
  if (draft.destination == null) {
    errors.push("Missing destination port");
  }
  
  if (errors.length > 0) {
    return resultError(errors);
  }
  // Another problem, need invariants here
  invariant(draft.origin != null);
  invariant(draft.destination != null);
  return resultValue({
    origin: draft.origin,
    destination: draft.destination,
  });
}

What a mouthful. Formula-one should probably make this easier.

zgotsch avatar Dec 17 '18 23:12 zgotsch