Skip to content

Instantly share code, notes, and snippets.

@hungneox
Created February 20, 2022 22:12
Show Gist options
  • Save hungneox/d8a1a371c5e19d9f6e50f5085f037ff7 to your computer and use it in GitHub Desktop.
Save hungneox/d8a1a371c5e19d9f6e50f5085f037ff7 to your computer and use it in GitHub Desktop.
import * as E from "fp-ts/Either";
import * as F from "fp-ts/function";
const minxLength = (s: string): E.Either<Error, string> => {
return s.length < 8 ? E.left(new Error("Password is too short")) : E.right(s);
};
const oneCapital = (s: string): E.Either<Error, string> =>
/[A-Z]/g.test(s)
? E.right(s)
: E.left(new Error("at least one capital letter"));
const oneNumber = (s: string): E.Either<Error, string> =>
/[0-9]/g.test(s) ? E.right(s) : E.left(new Error("at least one number"));
// This also works
// F.pipe(minLength(s), E.chain(oneCapital), E.chain(oneNumber));
const validatePassword = (s: string): E.Either<Error, string> =>
F.pipe(s, minLength, E.chain(oneCapital), E.chain(oneNumber));
// validatePassword('123456'); // Error: at least one capital letter
// validatePassword('salaSANA123'); // salaSANA123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment