Skip to content

Commit

Permalink
Fix ObjectId regex and add tests for invalid ids (#29)
Browse files Browse the repository at this point in the history
【Fix】ObjectId regex and add tests for invalid ids
  • Loading branch information
jaspervhaastert authored May 19, 2020
1 parent 16a3191 commit 7c42f82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ts/tests/types-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const { test } = Deno;
test("ObjectId parse", () => {
let $oid = "d3e48b03-7baf-4d98-8df7-5002a8fae5e4";
assertThrows(() => ObjectId($oid));

$oid = "5e63a91b00d839ae0084dfe4";
assertEquals(ObjectId($oid), { $oid });

$oid = "INVALID-5e63a91b00d839ae0084dfe4";
assertThrows(() => ObjectId($oid));

$oid = "5e63a91b00d839ae0084dfe4-INVALID";
assertThrows(() => ObjectId($oid));
});
2 changes: 1 addition & 1 deletion ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ObjectId {
$oid: string;
}
export function ObjectId($oid: string) {
const isLegal = /[0-9a-fA-F]{24}/.test($oid);
const isLegal = /^[0-9a-fA-F]{24}$/.test($oid);
if (!isLegal) {
throw new Error(`ObjectId("${$oid}") is not legal.`);
}
Expand Down

0 comments on commit 7c42f82

Please sign in to comment.