mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-20 09:20:16 +00:00
Fix input validation patterns for resource ids (#2086)
* Fix input element pattern matching and add validation reporting for cases where the element is not within a form element. * Update test snapshots. * Remove old code and fix trigger error message. * Move id validation to a util class. * Add unit tests, fix standalone function, rename constants.
This commit is contained in:
18
src/Utils/ValidationUtils.test.ts
Normal file
18
src/Utils/ValidationUtils.test.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { IsValidCosmosDbResourceId } from "Utils/ValidationUtils";
|
||||
|
||||
const testCases = [
|
||||
["validId", true],
|
||||
["forward/slash", false],
|
||||
["back\\slash", false],
|
||||
["question?mark", false],
|
||||
["hash#mark", false],
|
||||
["?invalidstart", false],
|
||||
["invalidEnd/", false],
|
||||
["space-at-end ", false],
|
||||
];
|
||||
|
||||
describe("IsValidCosmosDbResourceId", () => {
|
||||
test.each(testCases)("IsValidCosmosDbResourceId(%p). Expected: %p", (id: string, expected: boolean) => {
|
||||
expect(IsValidCosmosDbResourceId(id)).toBe(expected);
|
||||
});
|
||||
});
|
||||
24
src/Utils/ValidationUtils.ts
Normal file
24
src/Utils/ValidationUtils.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Common methods and constants for validation
|
||||
//
|
||||
|
||||
//
|
||||
// Validation of id for Cosmos DB resources:
|
||||
// - Database
|
||||
// - Container
|
||||
// - Stored Procedure
|
||||
// - User Defined Function (UDF)
|
||||
// - Trigger
|
||||
//
|
||||
// Use these with <input> elements
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
export const ValidCosmosDbIdInputPattern: RegExp = /[^\/?#\\]*[^\/?# \\]/;
|
||||
export const ValidCosmosDbIdDescription: string = "May not end with space nor contain characters '\\' '/' '#' '?'";
|
||||
|
||||
// For a standalone function regex, we need to wrap the previous reg expression,
|
||||
// to test against the entire value. This is done implicitly by input elements.
|
||||
const ValidCosmosDbIdRegex: RegExp = new RegExp(`^(?:${ValidCosmosDbIdInputPattern.source})$`);
|
||||
|
||||
export function IsValidCosmosDbResourceId(id: string): boolean {
|
||||
return id && ValidCosmosDbIdRegex.test(id);
|
||||
}
|
||||
Reference in New Issue
Block a user