-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
20 deletions.
There are no files selected for viewing
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
30 changes: 30 additions & 0 deletions
30
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import lodashCamelCase from "lodash/camelCase"; | ||
import WebinyError from "@webiny/error"; | ||
|
||
// We allow "@" because that's we use it with the `createModelField` function. | ||
const VALID_STORAGE_ID_REGEX = /^([@a-zA-Z-0-9]+)$/; | ||
|
||
export const validateStorageId = (storageId: string) => { | ||
if (!storageId.match(VALID_STORAGE_ID_REGEX)) { | ||
const message = [ | ||
`Invalid storageId provided ("${storageId}").`, | ||
'Only alphanumeric characters and "@" are allowed.' | ||
].join(" "); | ||
|
||
throw new WebinyError(message, "STORAGE_ID_NOT_ALPHANUMERIC_ERROR"); | ||
} | ||
|
||
// We must do this because in the process of camel casing, Lodash removes the `@` | ||
// character from the string. For example, if we received `text@productName`, we | ||
// would be doing the `text@productName` vs. `textProductName` (camel cased) comparison. | ||
storageId = storageId.replace("@", ""); | ||
|
||
if (storageId !== lodashCamelCase(storageId)) { | ||
const message = [ | ||
`Invalid storageId provided ("${storageId}").`, | ||
"Must be a camelCased string." | ||
].join(" "); | ||
|
||
throw new WebinyError(message, "STORAGE_ID_NOT_CAMEL_CASED_ERROR"); | ||
} | ||
}; |
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
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