-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate product and collection handles to be URL safe (#7310)
* fix: allow URL safe characters for product handle Fixes: CORE-2072
- Loading branch information
1 parent
7c4f4d7
commit 17486cd
Showing
8 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/core/utils/src/common/__tests__/is-valid-handle.spec.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,36 @@ | ||
import { isValidHandle } from "../validate-handle" | ||
|
||
describe("normalizeHandle", function () { | ||
it("should generate URL friendly handles", function () { | ||
const expectations = [ | ||
{ | ||
input: "the-fan-boy's-club", | ||
isValid: false, | ||
}, | ||
{ | ||
input: "@t-the-sky", | ||
isValid: false, | ||
}, | ||
{ | ||
input: "nouvelles-annees", | ||
isValid: true, | ||
}, | ||
{ | ||
input: "@t-the-sky", | ||
isValid: false, | ||
}, | ||
{ | ||
input: "user.product", | ||
isValid: false, | ||
}, | ||
{ | ||
input: 'sky"bar', | ||
isValid: false, | ||
}, | ||
] | ||
|
||
expectations.forEach((expectation) => { | ||
expect(isValidHandle(expectation.input)).toEqual(expectation.isValid) | ||
}) | ||
}) | ||
}) |
44 changes: 44 additions & 0 deletions
44
packages/core/utils/src/common/__tests__/to-handle.spec.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,44 @@ | ||
import { toHandle } from "../to-handle" | ||
|
||
describe("normalizeHandle", function () { | ||
it("should generate URL friendly handles", function () { | ||
const expectations = [ | ||
{ | ||
input: "The fan boy's club", | ||
output: "the-fan-boys-club", | ||
}, | ||
{ | ||
input: "nouvelles années", | ||
output: "nouvelles-annees", | ||
}, | ||
{ | ||
input: "25% OFF", | ||
output: "25-off", | ||
}, | ||
{ | ||
input: "25% de réduction", | ||
output: "25-de-reduction", | ||
}, | ||
{ | ||
input: "-first-product", | ||
output: "-first-product", | ||
}, | ||
{ | ||
input: "user.product", | ||
output: "userproduct", | ||
}, | ||
{ | ||
input: "_first-product", | ||
output: "-first-product", | ||
}, | ||
{ | ||
input: "_HELLO_WORLD", | ||
output: "-hello-world", | ||
}, | ||
] | ||
|
||
expectations.forEach((expectation) => { | ||
expect(toHandle(expectation.input)).toEqual(expectation.output) | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { kebabCase } from "./to-kebab-case" | ||
|
||
/** | ||
* Helper method to create a to be URL friendly "handle" from | ||
* a string value. | ||
* | ||
* - Works by converting the value to lowercase | ||
* - Splits and remove accents from characters | ||
* - Removes all unallowed characters like a '"%$ and so on. | ||
*/ | ||
export const toHandle = (value: string): string => { | ||
return kebabCase( | ||
value | ||
.toLowerCase() | ||
.normalize("NFD") | ||
.replace(/[\u0300-\u036f]/g, "") | ||
).replace(/[^a-z0-9A-Z-_]/g, "") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { kebabCase } from "./to-kebab-case" | ||
|
||
/** | ||
* Helper method to validate entity "handle" to be URL | ||
* friendly. | ||
*/ | ||
export const isValidHandle = (value: string): boolean => { | ||
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value) | ||
} |
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