-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-headless-cms): remove camelCase conversion in storageId valid…
…ation
- Loading branch information
Showing
2 changed files
with
21 additions
and
22 deletions.
There are no files selected for viewing
28 changes: 16 additions & 12 deletions
28
packages/api-headless-cms/__tests__/helpers/validateStorageId.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
); | ||
}); |
15 changes: 5 additions & 10 deletions
15
packages/api-headless-cms/src/crud/contentModel/validateStorageId.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |