mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-04-10 06:55:35 -04:00
chore: Nuxt 4 upgrade (#7426)
This commit is contained in:
2
frontend/app/lib/validators/index.ts
Normal file
2
frontend/app/lib/validators/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { scorePassword } from "./password";
|
||||
export { required, email, whitespace, url, urlOptional, minLength, maxLength } from "./inputs";
|
||||
94
frontend/app/lib/validators/inputs.test.ts
Normal file
94
frontend/app/lib/validators/inputs.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { expect, test, vi } from "vitest";
|
||||
import enUS from "~/lang/messages/en-US.json";
|
||||
|
||||
import { required, email, whitespace, url, minLength, maxLength } from "./inputs";
|
||||
|
||||
vi.mock("~/composables/use-global-i18n", () => {
|
||||
const interpolate = (msg: string, params?: Record<string, unknown>) => {
|
||||
if (!params) return msg;
|
||||
return msg
|
||||
.replace("{min}", String(params.min ?? ""))
|
||||
.replace("{max}", String(params.max ?? ""));
|
||||
};
|
||||
|
||||
const t = (key: string, params?: Record<string, unknown>) => {
|
||||
const parts = key.split(".");
|
||||
let acc: any = enUS as any;
|
||||
for (const p of parts) acc = acc?.[p];
|
||||
const msg: string | undefined = acc;
|
||||
return interpolate(msg ?? key, params);
|
||||
};
|
||||
|
||||
return { useGlobalI18n: () => ({ t }) };
|
||||
});
|
||||
|
||||
export { scorePassword } from "./password";
|
||||
|
||||
// Tests
|
||||
|
||||
test("validator required", () => {
|
||||
const falsey = enUS.validators.required;
|
||||
expect(required("123")).toBe(true);
|
||||
expect(required("")).toBe(falsey);
|
||||
expect(required(undefined)).toBe(falsey);
|
||||
expect(required(null)).toBe(falsey);
|
||||
});
|
||||
|
||||
const nulls = [undefined, null];
|
||||
|
||||
test("validator email", () => {
|
||||
const falsey = enUS.validators["invalid-email"];
|
||||
expect(email("123")).toBe(falsey);
|
||||
expect(email("email@example.com")).toBe(true);
|
||||
|
||||
for (const n of nulls) {
|
||||
expect(email(n)).toBe(falsey);
|
||||
}
|
||||
});
|
||||
|
||||
test("whitespace", () => {
|
||||
const falsey = enUS.validators["no-whitespace"];
|
||||
expect(whitespace("123")).toBe(true);
|
||||
expect(whitespace(" ")).toBe(falsey);
|
||||
expect(whitespace("123 123")).toBe(falsey);
|
||||
|
||||
for (const n of nulls) {
|
||||
expect(whitespace(n)).toBe(falsey);
|
||||
}
|
||||
});
|
||||
|
||||
test("url", () => {
|
||||
const falsey = enUS.validators["invalid-url"];
|
||||
expect(url("https://example.com")).toBe(true);
|
||||
expect(url("")).toBe(falsey);
|
||||
|
||||
for (const n of nulls) {
|
||||
expect(url(n)).toBe(falsey);
|
||||
}
|
||||
});
|
||||
|
||||
test("minLength", () => {
|
||||
const min = 3;
|
||||
const falsey = enUS.validators["min-length"].replace("{min}", String(min));
|
||||
const fn = minLength(min);
|
||||
expect(fn("123")).toBe(true);
|
||||
expect(fn("12")).toBe(falsey);
|
||||
expect(fn("")).toBe(falsey);
|
||||
|
||||
for (const n of nulls) {
|
||||
expect(fn(n)).toBe(falsey);
|
||||
}
|
||||
});
|
||||
|
||||
test("maxLength", () => {
|
||||
const max = 3;
|
||||
const falsey = enUS.validators["max-length"].replace("{max}", String(max));
|
||||
const fn = maxLength(max);
|
||||
expect(fn("123")).toBe(true);
|
||||
expect(fn("1234")).toBe(falsey);
|
||||
expect(fn("")).toBe(true);
|
||||
|
||||
for (const n of nulls) {
|
||||
expect(fn(n)).toBe(true);
|
||||
}
|
||||
});
|
||||
48
frontend/app/lib/validators/inputs.ts
Normal file
48
frontend/app/lib/validators/inputs.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { useGlobalI18n } from "~/composables/use-global-i18n";
|
||||
|
||||
const EMAIL_REGEX
|
||||
= /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
|
||||
export function required(v: string | undefined | null) {
|
||||
const i18n = useGlobalI18n();
|
||||
return !!v || i18n.t("validators.required");
|
||||
}
|
||||
|
||||
export function email(v: string | undefined | null) {
|
||||
const i18n = useGlobalI18n();
|
||||
return (!!v && EMAIL_REGEX.test(v)) || i18n.t("validators.invalid-email");
|
||||
}
|
||||
|
||||
export function whitespace(v: string | null | undefined) {
|
||||
const i18n = useGlobalI18n();
|
||||
return (!!v && v.split(" ").length <= 1) || i18n.t("validators.no-whitespace");
|
||||
}
|
||||
|
||||
export function url(v: string | undefined | null) {
|
||||
const i18n = useGlobalI18n();
|
||||
if (!v) {
|
||||
return i18n.t("validators.invalid-url");
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = new URL(v);
|
||||
return (parsed.protocol === "http:" || parsed.protocol === "https:") || i18n.t("validators.invalid-url");
|
||||
}
|
||||
catch {
|
||||
return i18n.t("validators.invalid-url");
|
||||
}
|
||||
}
|
||||
|
||||
export function urlOptional(v: string | undefined | null) {
|
||||
return v ? url(v) : true;
|
||||
}
|
||||
|
||||
export function minLength(min: number) {
|
||||
const i18n = useGlobalI18n();
|
||||
return (v: string | undefined | null) => (!!v && v.length >= min) || i18n.t("validators.min-length", { min });
|
||||
}
|
||||
|
||||
export function maxLength(max: number) {
|
||||
const i18n = useGlobalI18n();
|
||||
return (v: string | undefined | null) => !v || v.length <= max || i18n.t("validators.max-length", { max });
|
||||
}
|
||||
30
frontend/app/lib/validators/password.test.ts
Normal file
30
frontend/app/lib/validators/password.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { describe, test, expect } from "vitest";
|
||||
import { scorePassword } from "./password";
|
||||
|
||||
describe("scorePassword tests", () => {
|
||||
test("flagged words should return negative number", () => {
|
||||
const flaggedWords = ["password", "mealie", "admin", "qwerty", "login"];
|
||||
|
||||
for (const word of flaggedWords) {
|
||||
expect(scorePassword(word)).toBe(0);
|
||||
}
|
||||
});
|
||||
|
||||
test("should return 0 for empty string", () => {
|
||||
expect(scorePassword("")).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 for strings less than 6", () => {
|
||||
expect(scorePassword("12345")).toBe(0);
|
||||
});
|
||||
|
||||
test("should return positive number for long string", () => {
|
||||
const result = expect(scorePassword("123456"));
|
||||
result.toBeGreaterThan(0);
|
||||
result.toBeLessThan(31);
|
||||
});
|
||||
|
||||
test("should return max number for long string with all variations", () => {
|
||||
expect(scorePassword("3bYWcfYOwqxljqeOmQXTLlBwkrH6HV")).toBe(100);
|
||||
});
|
||||
});
|
||||
45
frontend/app/lib/validators/password.ts
Normal file
45
frontend/app/lib/validators/password.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
const flaggedWords = ["password", "mealie", "admin", "qwerty", "login"];
|
||||
|
||||
/**
|
||||
* scorePassword returns a score for a given password between 0 and 100.
|
||||
* if a password contains a flagged word, it returns 0.
|
||||
* @param pass
|
||||
* @returns
|
||||
*/
|
||||
export function scorePassword(pass: string): number {
|
||||
let score = 0;
|
||||
if (!pass) return score;
|
||||
|
||||
if (pass.length < 6) return score;
|
||||
|
||||
// Check for flagged words
|
||||
for (const word of flaggedWords) {
|
||||
if (pass.toLowerCase().includes(word)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// award every unique letter until 5 repetitions
|
||||
const letters: { [key: string]: number } = {};
|
||||
|
||||
for (let i = 0; i < pass.length; i++) {
|
||||
letters[pass[i]] = (letters[pass[i]] || 0) + 1;
|
||||
score += 5.0 / letters[pass[i]];
|
||||
}
|
||||
|
||||
// bonus points for mixing it up
|
||||
const variations: { [key: string]: boolean } = {
|
||||
digits: /\d/.test(pass),
|
||||
lower: /[a-z]/.test(pass),
|
||||
upper: /[A-Z]/.test(pass),
|
||||
nonWords: /\W/.test(pass),
|
||||
};
|
||||
|
||||
let variationCount = 0;
|
||||
for (const check in variations) {
|
||||
variationCount += variations[check] === true ? 1 : 0;
|
||||
}
|
||||
score += (variationCount - 1) * 10;
|
||||
|
||||
return Math.max(Math.min(score, 100), 0);
|
||||
}
|
||||
Reference in New Issue
Block a user