Skip to content

Commit

Permalink
changed to a simple switch
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed May 10, 2021
1 parent e0804b8 commit 9f35113
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
29 changes: 26 additions & 3 deletions nats-base-client/jsutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,38 @@ export function validateStreamName(name?: string) {
return validateName("stream", name);
}

function badChar(s: string): string {
switch (s) {
case ".":
return "dot (.)";
case "*":
return "asterisk (*)";
case ">":
return "greater than (>)";
case "\r":
return "carriage return (␍)";
case "\n":
return "line feed (␊)";
case "\t":
return "tab (␉)";
case "\f":
return "form feed (␌)";
default:
return "unknown";
}
}

export function validateName(context: string, name = "") {
if (name === "") {
throw Error(`${context} name required`);
}
const bad = [".", "*", ">"];
const bad = [".", "*", ">", "\r", "\n", "\t", "\f"];
bad.forEach((v) => {
if (name.indexOf(v) !== -1) {
const idx = name.indexOf(v);
if (idx !== -1) {
const rep = badChar(v);
throw Error(
`invalid ${context} name - ${context} name cannot contain '${v}'`,
`invalid ${context} name - ${context} name cannot contain '${rep}'`,
);
}
});
Expand Down
36 changes: 22 additions & 14 deletions tests/jsm_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ import {
} from "./jstest_util.ts";
import { connect } from "../src/mod.ts";
import { assertThrowsAsyncErrorCode } from "./helpers/mod.ts";
import { validateName } from "../nats-base-client/jsutil.ts";
import {
validateName,
validateStreamName,
} from "../nats-base-client/jsutil.ts";

const StreamNameRequired = "stream name required";
const ConsumerNameRequired = "durable name required";
Expand Down Expand Up @@ -536,19 +539,23 @@ Deno.test("jsm - advisories", async () => {
});

Deno.test("jsm - validate name", () => {
type t = [string, boolean];
type t = [string, boolean, string];
const tests: t[] = [
["", false],
[".", false],
["*", false],
[">", false],
["hello.", false],
["hello.*", false],
["hello.>", false],
["one.two", false],
["one*two", false],
["one>two", false],
["stream", true],
["", false, "name required"],
[".", false, "dot"],
["*", false, "asterisk"],
[">", false, "greater than"],
["hello\n", false, "line feed"],
["hello\r", false, "carriage return"],
["he\tllo", false, "tab"],
["he\fllo", false, "form feed"],
["hello.", false, "dot"],
["hello*", false, "asterisk"],
["hello>", false, "greater than"],
["one.two", false, "dot"],
["one*two", false, "asterisk"],
["one>two", false, "greater than"],
["stream", true, ""],
];

tests.forEach((v, idx) => {
Expand All @@ -557,10 +564,11 @@ Deno.test("jsm - validate name", () => {
if (!v[1]) {
fail(`${v[0]} should have been rejected`);
}
} catch (_err) {
} catch (err) {
if (v[1]) {
fail(`${v[0]} should have been valid`);
}
assert(err.message.includes(v[2]));
}
});
});
Expand Down

0 comments on commit 9f35113

Please sign in to comment.