summaryrefslogtreecommitdiff
path: root/utils/validate_identifier.ts
blob: ec8b77b65cdbdc3de41eaa8dc21d4fbface8290c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { Either } from "./mod.ts";

export const validateIdentifier = (token: string) => {
  return (/^[a-zA-Z0-9_\-:. \/]+$/).test(token) && !token.includes("..");
};

// ensure {@param obj} is a Record<string, string> with stuff that won't
// have the potential for shell injection, just to be super safe.
export const validateExecutionEntries = (
  obj: Record<string, unknown>,
): Either<Array<[string, unknown]>, Record<string, string>> => {
  const invalidEntries = Object.entries(obj).filter((e) =>
    !e.every((x) => typeof x === "string" && validateIdentifier(x))
  );
  if (invalidEntries.length > 0) return Either.left(invalidEntries);
  return Either.right(<Record<string, string>> obj);
};