Currently, I am in the process of validating a command that is applied to an array representing SVG path data using fp-ts.
type CommandValidation = (commands: CommandArray, nextCommand: Command) => option.Option<string>;
const newCommandValidations: Array<CommandValidation> = [
validateFirstCommandIsMove,
validateSymmetricCommandFollowsBezier,
validateNoMoveAfterMove,
];
export const safelyPushCommand = (command: Command) => either.map((commands: CommandArray) => {
// The errors are not overlapping yet, but there might be co-existing errors in the future
const validationErrors = fpFunction.pipe(newCommandValidations,
fpArray.map((validation) => validation(commands, command)),
fpArray.filter(option.isSome),
fpArray.map(option.fold(() => undefined, (some) => some)));
if (validationErrors.length > 0) {
return either.left(validationErrors);
}
return either.right(pushCommands([command])(commands));
});
Unfortunately, the validationErrors variable is still considered as a list of items with possible "none" values.
How can I obtain an array of strings (specifically typed) that represent any validation errors for this operation?
Should I use an "apply" function to pass parameters into the validation functions?
How can I get an apply function for the validators?
Is there a better way to perform the fold operation when the second function is simply the identity function? EDIT: I had this question addressed in this video https://youtu.be/1LCqHnaJJtY?t=2470 :
option.fold(() => undefined, (some) => some) === option.getOrElse(() => undefined) === option.toUndefined
Many questions from someone navigating through the realm of fp-ts, hoping for some insights to make my project more efficient. The sample code can be found in a branch I created here: https://github.com/justin-hackin/fp-ts-svg-path-d/tree/validators