Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Either, left, right } from 'fp-ts/lib/Either';
- type File = {
- name: string;
- ext: string;
- size: number;
- };
- const checkName = (f: File): Either<string, File> =>
- f.name.length < 20 ? right(f) : left('File length should be less then 20 characters');
- const checkExt = (f: File): Either<string, File> =>
- ['ts', 'tsx'].includes(f.ext) ? right(f) : left('File extension should be either ts or tsx');
- const checkSize = (f: File): Either<string, File> => (f.size < 1000 ? right(f) : left('Error message about size'));
- const file = {
- name: 'file',
- ext: 'tss',
- size: 123,
- };
- // we'll have Either Right<File> or Left<string> here
- const validatedFile = checkName(file)
- .chain(checkExt)
- .chain(checkSize);
Add Comment
Please, Sign In to add comment