formula-one
formula-one copied to clipboard
No way to colocate validations and transformation
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.