| name | mongez-supportive-is-formats |
| description | Documents the five string-format predicates — `isRegex`, `isValidId`, `isJson`, `isUrl`, and `isEmail` — including their rules, limitations, and known bugs.
TRIGGER when: code imports `isRegex`, `isValidId`, `isJson`, `isUrl`, or `isEmail` from `@mongez/supportive-is`; user asks "how do I validate URL / email / JSON / HTML id", "check if string is a regex or pattern", or "tell if a value looks like valid JSON"; typical import is `import { isUrl, isEmail } from "@mongez/supportive-is"` (or `Is.url(x)` / `Is.email(x)` via the legacy default `Is` namespace).
SKIP: trustworthy validation for auth, redirects, or stored data — use `zod`, `valibot`, or RFC-grade libraries instead since these are convenience filters not security gates; schema-based form validation libraries; `@mongez/reinforcements` for general string transforms, not predicates.
|
Formats
Five string-format predicates. None of them are security gates — they're convenience filters. For validation that needs to be trustworthy (auth flows, redirect targets, stored emails), use a real validator like zod plus a domain-specific library.
| Predicate | Quick rule |
|---|
isRegex(v) | RegExp instance / literal |
isValidId(v) | Valid HTML id attribute |
isJson(v) | Valid JSON string starting with { or [ |
isUrl(v) | http(s):// URL with a dotted hostname |
isEmail(v) | Standard email regex |
isRegex
isRegex(/x/);
isRegex(/abc/gi);
isRegex(new RegExp("x"));
isRegex("/x/");
isRegex("x");
isRegex(null);
Useful for "did the caller pass a regex or a string" branches:
function match(pattern: string | RegExp, text: string) {
const re = isRegex(pattern) ? pattern : new RegExp(pattern);
return re.test(text);
}
isValidId (Is.validHtmlId)
Matches /^[A-Za-z]+[\w\-:.]*$/. Letters start, followed by word characters / dashes / colons / dots. Wrapped in Boolean(...) so falsy inputs return real false.
isValidId("base-id");
isValidId("BASE-ID");
isValidId("has.dots");
isValidId("has:colon");
isValidId("has_underscore");
isValidId("has-number-3");
isValidId("1starts-with-digit");
isValidId("_starts-with-underscore");
isValidId("has,comma");
isValidId("has spaces");
isValidId("");
isValidId(null);
isJson
Valid JSON string starting with { or [. Doesn't accept primitive JSON values ("true", "null", "123", '"hello"') — only objects and arrays.
isJson('{"name":"John"}');
isJson("[]");
isJson("[1,2,3]");
isJson('{"nested":{"a":1}}');
isJson("");
isJson("12");
isJson('"hello"');
isJson("null");
isJson("true");
isJson("{name:1}");
isJson(null);
isJson({});
isJson([]);
Worth knowing the prefix gate exists: JSON.parse("12") would succeed but isJson("12") returns false. If you need to accept primitives, use try { JSON.parse(value); return true } catch { return false } directly.
isUrl
Tries to construct new URL(value), then checks:
- Protocol is
http: or https: (no FTP, file, data, etc.)
- Hostname contains a
.
- Every dot-separated label of the hostname is non-empty (rejects
google., ..com, google..com)
isUrl("https://google.com");
isUrl("http://example.com:8080");
isUrl("https://sub.example.co.uk");
isUrl("https://example.com/p?q=1");
isUrl("google.com");
isUrl("www.google.com");
isUrl("ftp://example.com");
isUrl("file:///etc/passwd");
isUrl("javascript:alert(1)");
isUrl("https://google.");
isUrl("https://google..com");
isUrl("");
isUrl(null);
isUrl(undefined);
isUrl(12);
isEmail
Standard email regex from RFC 5322 (the practical subset most projects use).
isEmail("user@example.com");
isEmail("a.b.c@example.co.uk");
isEmail("u+tag@example.com");
isEmail("user-name@sub.example.com");
isEmail("user");
isEmail("user@");
isEmail("@example.com");
isEmail("a@b");
isEmail("a b@example.com");
isEmail("");
isEmail(null);
isEmail(undefined);
isEmail(12);
isEmail(["user@example.com"]);
Notes
- These are predicates, not validators. They tell you "does this look like X". For trust decisions (open the link / send to address / interpret as JSON), do the actual operation in a
try/catch.
isJson will reject valid primitive JSON ("123", "true", "null", '"hello"'). This is intentional but easy to miss.
isUrl rejects schemes other than http(s):. Use the URL constructor directly if you need to accept mailto: or tel: URIs.