Skip to content

Commit

Permalink
fix(api-headless-cms): remove camelCase conversion in storageId valid…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Pavel910 committed Jun 14, 2024
1 parent 164b9cf commit f2c4f8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { validateStorageId } from "~/crud/contentModel/validateStorageId";

describe("validateStorageId", () => {
it("should validate storage ids", async () => {
// Valid storage IDs
expect(() => validateStorageId("pageTeaserMedia")).not.toThrow();
expect(() => validateStorageId("number@productPrice123")).not.toThrow();
expect(() => validateStorageId("long-text@description")).not.toThrow();
expect(() => validateStorageId("text@0b6vu1w0")).not.toThrow();
expect(() => validateStorageId("0b6vu1w0")).not.toThrow();
expect(() => validateStorageId("0b6vu1w0-abc")).not.toThrow();

// Invalid storage IDs
expect(() => validateStorageId("some_weird_$_id")).toThrow();
expect(() => validateStorageId("my!R3ally$creWedI\\d^^")).toThrow();
it.each([
["pageTeaserMedia"],
["number@productPrice123"],
["long-text@description"],
["text@0b6vu1w0"],
["0b6vu1w0"],
["0b6vu1w0-abc"]
])(`should pass storage id validation - "%s"`, async storageId => {
expect(() => validateStorageId(storageId)).not.toThrow();
});

it.each([["some_weird_$_id"], ["my!/[R]{3}ally$creWedI:_+\\d^^"], ["读写汉字 - 学中文"]])(
`should fail storage id validation - "%s"`,
async storageId => {
expect(() => validateStorageId(storageId)).toThrow();
}
);
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import lodashCamelCase from "lodash/camelCase";
import WebinyError from "@webiny/error";

// We allow "@" because that's how we construct storageIds with `createModelField` utility.
const VALID_STORAGE_ID_REGEX = /^([@a-zA-Z-0-9]+)$/;

export const validateStorageId = (storageId: string) => {
if (!storageId.match(VALID_STORAGE_ID_REGEX)) {
const camelCasedStorageId = lodashCamelCase(storageId);
const message = [
`Invalid storageId provided ("${storageId}").`,
'Only alphanumeric characters and "@" are allowed.'
].join(" ");

if (storageId !== camelCasedStorageId) {
const message = [
`Invalid storageId provided ("${storageId}").`,
"Must be a camelCased string."
].join(" ");

throw new WebinyError(message, "STORAGE_ID_NOT_CAMEL_CASED_ERROR");
}
throw new WebinyError(message, "STORAGE_ID_NOT_ALPHANUMERIC_ERROR");
}
};

0 comments on commit f2c4f8b

Please sign in to comment.