-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new API slug validator; Add/update block category tests;
- Loading branch information
1 parent
7a7f98e
commit e2746d2
Showing
7 changed files
with
182 additions
and
35 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
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
14 changes: 9 additions & 5 deletions
14
packages/app-page-builder/src/admin/views/BlockCategories/validators.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,9 +1,13 @@ | ||
export const blockCategorySlugValidator = (value: string): boolean => { | ||
if (value.match(/^[a-z]+(\-[a-z]+)*$/)) { | ||
return true; | ||
if (!value.match(/^[a-z]+(\-[a-z]+)*$/)) { | ||
throw new Error( | ||
"Block Category slug must consist of only 'a-z' and '-' characters (for example: 'block-category-slug')" | ||
); | ||
} | ||
|
||
throw new Error( | ||
"Block Category slug must consist of only 'a-z' and '-' characters (for example: 'block-category-slug')" | ||
); | ||
if (value.length > 100) { | ||
throw new Error("Block Category slug must shorter than 100 characters"); | ||
} | ||
|
||
return true; | ||
}; |
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,71 @@ | ||
import { validation, ValidationError } from "../src"; | ||
|
||
describe("slug test", () => { | ||
it("should not get triggered if empty value was set", async () => { | ||
await expect(validation.validate(null, "slug")).resolves.toBe(true); | ||
}); | ||
|
||
it("should not get triggered if correct value was set", async () => { | ||
//await expect(validation.validate("test-slug-correct", "slug")).resolves.toBe(true); | ||
await expect(validation.validate("test-slug", "slug")).resolves.toBe(true); | ||
await expect(validation.validate("test", "slug")).resolves.toBe(true); | ||
}); | ||
|
||
it("should fail - wrong dash character usage", async () => { | ||
await expect(validation.validate("test--slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("test---slug", "slug")).rejects.toThrow(ValidationError); | ||
|
||
await expect(validation.validate("-test-slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("test-slug-", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("--slug--", "slug")).rejects.toThrow(ValidationError); | ||
|
||
await expect(validation.validate("-slug-", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("-slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("slug-", "slug")).rejects.toThrow(ValidationError); | ||
}); | ||
|
||
it("should fail - uppercase letters are not allowed", async () => { | ||
await expect(validation.validate("Test-Slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("test-Slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("Test-slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("tesT-sluG", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("Test", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("tEst", "slug")).rejects.toThrow(ValidationError); | ||
}); | ||
|
||
it("should fail - numbers are not allowed", async () => { | ||
await expect(validation.validate("test-slug-12345", "slug")).rejects.toThrow( | ||
ValidationError | ||
); | ||
await expect(validation.validate("test-12345-slug", "slug")).rejects.toThrow( | ||
ValidationError | ||
); | ||
await expect(validation.validate("12345-test-slug", "slug")).rejects.toThrow( | ||
ValidationError | ||
); | ||
await expect(validation.validate("test123-slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("test-slug123", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("12345-slug", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("slug-12345", "slug")).rejects.toThrow(ValidationError); | ||
|
||
await expect(validation.validate("slug12345", "slug")).rejects.toThrow(ValidationError); | ||
}); | ||
|
||
it("should fail - special chars are not allowed", async () => { | ||
await expect(validation.validate("test-slug-&%^#", "slug")).rejects.toThrow( | ||
ValidationError | ||
); | ||
|
||
await expect(validation.validate("test-&%^#-slug", "slug")).rejects.toThrow( | ||
ValidationError | ||
); | ||
await expect(validation.validate("&%^#-test-slug", "slug")).rejects.toThrow( | ||
ValidationError | ||
); | ||
|
||
await expect(validation.validate("&%^#-test", "slug")).rejects.toThrow(ValidationError); | ||
await expect(validation.validate("test-&%^#", "slug")).rejects.toThrow(ValidationError); | ||
|
||
await expect(validation.validate("test&%^#", "slug")).rejects.toThrow(ValidationError); | ||
}); | ||
}); |
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,16 @@ | ||
import ValidationError from "~/validationError"; | ||
|
||
export default (value: any) => { | ||
if (!value) { | ||
return; | ||
} | ||
value = value + ""; | ||
|
||
if (value.match(/^[a-z]+(\-[a-z]+)*$/) && value.length <= 100) { | ||
return; | ||
} | ||
|
||
throw new ValidationError( | ||
"Slug must consist of only 'a-z' and '-' and be max 100 characters long (for example: 'some-entry-slug')" | ||
); | ||
}; |