Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve name validation #2019

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/components/src/utils/validationSchemes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PROHIBITED_CHARACTERS } from "@umami/state";
import { type Network } from "@umami/tezos";
import { z } from "zod";

Expand All @@ -24,6 +25,10 @@ export const getNetworkValidationScheme = (availableNetworks: Network[], network
: z
.string()
.min(1, "Name is required")
.max(256, "Name should not exceed 256 characters")
.refine(name => !PROHIBITED_CHARACTERS.some(char => name.includes(char)), {
message: "Name contains special character(s)",
})
.refine(name => !availableNetworks.find(n => n.name === name), {
message: "Network with this name already exists",
}),
Expand Down
18 changes: 17 additions & 1 deletion packages/state/src/hooks/labels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@umami/core";
import { MAINNET } from "@umami/tezos";

import { useGetNextAvailableAccountLabels, useValidateName } from "./labels";
import { PROHIBITED_CHARACTERS, useGetNextAvailableAccountLabels, useValidateName } from "./labels";
import { contactsActions, multisigsActions, networksActions } from "../slices";
import { type UmamiStore, makeStore } from "../store";
import { addTestAccounts, renderHook } from "../testUtils";
Expand All @@ -22,6 +22,22 @@ beforeEach(() => {

describe("labelsHooks", () => {
describe("useValidateName", () => {
it.each(PROHIBITED_CHARACTERS)("fails if name contains `%s` special character", char => {
const {
result: { current: validateName },
} = renderHook(() => useValidateName(), { store });

expect(validateName(`Some ${char} name`)).toEqual("Name contains special character(s)");
});

it("fails if name exceeds 256 characters", () => {
const {
result: { current: validateName },
} = renderHook(() => useValidateName(), { store });

expect(validateName("a".repeat(257))).toEqual("Name should not exceed 256 characters");
});

describe.each([
{ testCase: "with trailing whitespaces", withWhitespaces: true },
{ testCase: "without trailing whitespaces", withWhitespaces: false },
Expand Down
8 changes: 8 additions & 0 deletions packages/state/src/hooks/labels.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { useAllContacts } from "./contacts";
import { useAllAccounts } from "./getAccountData";

export const PROHIBITED_CHARACTERS = `/\\"'<>:{}$#|\`\t\r\n`.split("");

/** Hook for validating name for account or contact. */
export const useValidateName = (oldName?: string | undefined) => {
const isUniqueLabel = useIsUniqueLabel();

return (name: string) => {
const trimmedName = name.trim();
if (trimmedName.length > 256) {
return "Name should not exceed 256 characters";
}
if (PROHIBITED_CHARACTERS.some(char => trimmedName.includes(char))) {
return "Name contains special character(s)";
}
if (trimmedName.length === 0) {
return "Name should not be empty";
}
Expand Down
Loading