chore: import upstream snapshot with attribution
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
type Valid = {
|
||||
valid: true;
|
||||
error: null;
|
||||
};
|
||||
|
||||
type Invalid = {
|
||||
valid: false;
|
||||
error: string;
|
||||
};
|
||||
|
||||
type Validated = Valid | Invalid;
|
||||
|
||||
export const invalidCharError: Invalid = {
|
||||
valid: false,
|
||||
error: 'contains invalid characters'
|
||||
};
|
||||
export const validationSuccess: Valid = { valid: true, error: null };
|
||||
export const usernameTooShort: Invalid = {
|
||||
valid: false,
|
||||
error: 'is too short'
|
||||
};
|
||||
export const usernameIsHttpStatusCode: Invalid = {
|
||||
valid: false,
|
||||
error: 'is a reserved error code'
|
||||
};
|
||||
|
||||
const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
|
||||
export const isHttpStatusCode = (str: string): boolean => {
|
||||
const output = parseInt(str, 10);
|
||||
return !isNaN(output) && output >= 100 && output <= 599;
|
||||
};
|
||||
|
||||
export const isValidUsername = (str: string): Validated => {
|
||||
if (!validCharsRE.test(str)) return invalidCharError;
|
||||
if (str.length < 3) return usernameTooShort;
|
||||
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
|
||||
return validationSuccess;
|
||||
};
|
||||
|
||||
// link template:
|
||||
// https://learn.microsoft.com/LOCALE/users/USERNAME/transcript/ID
|
||||
export const isMicrosoftTranscriptLink = (value: string): boolean => {
|
||||
let url;
|
||||
try {
|
||||
url = new URL(value);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
const correctDomain = url.hostname === 'learn.microsoft.com';
|
||||
const correctPath = !!url.pathname.match(
|
||||
/^\/[^/]+\/users\/[^/]+\/transcript\/[^/]+$/
|
||||
);
|
||||
const notPlaceholder = !url.pathname.match(
|
||||
'/LOCALE/users/USERNAME/transcript/ID'
|
||||
);
|
||||
return correctDomain && correctPath && notPlaceholder;
|
||||
};
|
||||
Reference in New Issue
Block a user