From ef1f47e8b00113f8d00e28520f3f91cd1ca64bbb Mon Sep 17 00:00:00 2001 From: rachids Date: Thu, 16 Jul 2020 12:15:00 -0400 Subject: [PATCH 1/2] Added `isUrl()` rule --- src/rules/is_url.ts | 14 +++++++++++++ tests/rules/is_url.test.ts | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/rules/is_url.ts create mode 100644 tests/rules/is_url.test.ts diff --git a/src/rules/is_url.ts b/src/rules/is_url.ts new file mode 100644 index 0000000..6c6f849 --- /dev/null +++ b/src/rules/is_url.ts @@ -0,0 +1,14 @@ +import { invalid } from "../utils.ts"; +import { Validity } from "../types.ts"; + +export function isUrl(value: any): Validity { + // From + const expression = + /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/g; + + const regex = new RegExp(expression); + + if (typeof value !== "string" || !regex.test(value.toLowerCase())) { + return invalid("isUrl", { value }); + } +} diff --git a/tests/rules/is_url.test.ts b/tests/rules/is_url.test.ts new file mode 100644 index 0000000..75e1fbc --- /dev/null +++ b/tests/rules/is_url.test.ts @@ -0,0 +1,41 @@ +import { isUrl } from "../../src/rules/is_url.ts"; +import { invalid } from "../../src/utils.ts"; +import { assertInvalid, assertValid } from "../utils.ts"; + +Deno.test("rules.isUrl(null) should be invalid", () => { + assertInvalid(isUrl(null), invalid("isUrl", { value: null })); +}); + +Deno.test("rules.isUrl(undefined) should be invalid", () => { + assertInvalid(isUrl(undefined), invalid("isUrl", { value: undefined })); +}); + +Deno.test("rules.isUrl(0.1) should be invalid", () => { + assertInvalid(isUrl(0.1), invalid("isUrl", { value: 0.1 })); +}); + +Deno.test("rules.isUrl() with invalid urls should be invalid", () => { + const url = "test" + assertInvalid(isUrl(url), invalid("isUrl", { value: url })); +}); + +Deno.test("rules.isUrl() with invalid urls should be invalid", () => { + const url = "http:/test.com" + assertInvalid(isUrl(url), invalid("isUrl", { value: url })); +}); + +Deno.test("rules.isUrl() with valid urls should be valid", () => { + assertValid(isUrl("http://www.test.com")); +}); + +Deno.test("rules.isUrl() with valid urls should be valid", () => { + assertValid(isUrl("www.test.com")); +}); + +Deno.test("rules.isUrl() with valid urls should be valid", () => { + assertValid(isUrl("test.com")); +}); + +Deno.test("rules.isUrl() with valid urls should be valid", () => { + assertValid(isUrl("test.com/")); +}); From ff0791aaaa667a6554cf061cca24549f62317b2a Mon Sep 17 00:00:00 2001 From: rachids Date: Thu, 16 Jul 2020 12:20:34 -0400 Subject: [PATCH 2/2] fmt --- src/rules/is_url.ts | 2 +- tests/rules/is_url.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rules/is_url.ts b/src/rules/is_url.ts index 6c6f849..63fff48 100644 --- a/src/rules/is_url.ts +++ b/src/rules/is_url.ts @@ -2,7 +2,7 @@ import { invalid } from "../utils.ts"; import { Validity } from "../types.ts"; export function isUrl(value: any): Validity { - // From + // From const expression = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/g; diff --git a/tests/rules/is_url.test.ts b/tests/rules/is_url.test.ts index 75e1fbc..3da9ca7 100644 --- a/tests/rules/is_url.test.ts +++ b/tests/rules/is_url.test.ts @@ -15,12 +15,12 @@ Deno.test("rules.isUrl(0.1) should be invalid", () => { }); Deno.test("rules.isUrl() with invalid urls should be invalid", () => { - const url = "test" + const url = "test"; assertInvalid(isUrl(url), invalid("isUrl", { value: url })); }); Deno.test("rules.isUrl() with invalid urls should be invalid", () => { - const url = "http:/test.com" + const url = "http:/test.com"; assertInvalid(isUrl(url), invalid("isUrl", { value: url })); });