Functional Weave
Code in TypeScript

auth.validate-login@1.0.0

impl/typescript.ts

1,291 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { normaliseEmail } from "./auth_normalise_email.ts";  ← from auth.normalise-email ^1.0.0 · built alongside by fune
import { type LoginCheck } from "./auth_validate_login_types.ts";

function isAsciiSpace(ch: string): boolean {
  return ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === "\f" || ch === "\v";
}

function isBlank(text: string): boolean {
  return Array.from(text).every(isAsciiSpace);
}

/**
 * A login form checked for shape only: the email to look up when valid, and
 * a message for each field that needs fixing. The password is never judged
 * against a policy here, so a password set under older rules still logs in.
 */
export function validateLogin(email: string, password: string): LoginCheck {
  // A non-string (a malformed JSON body) is an empty field, not an exception.
  const emailText = typeof email === "string" ? email : "";
  const passwordText = typeof password === "string" ? password : "";
  const fields: Record<string, string> = {};

  const normalised = normaliseEmail(emailText);
  if (normalised === null) {
    fields.email = isBlank(emailText) ? "Enter your email address." : "Enter a valid email address, like name@example.com.";
  }
  if (passwordText === "") fields.password = "Enter your password.";

  return { valid: Object.keys(fields).length === 0, email: normalised, fields };
}