Skip to main content
Module

x/value_schema/test/schemas/string.test.ts

simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, Deno, and Bun
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
import vs from "value-schema";import {describe, expect, it} from "@jest/globals";
{ describe("type", testType); describe("ifUndefined", testIfUndefined); describe("ifNull", testIfNull); describe("ifEmptyString", testIfEmptyString); describe("trims", testTrims); describe("only", testOnly); describe("minLength", testMinLength); describe("maxLength", testMaxLength); describe("pattern", testPattern); describe("converter", testConverter);}
/** * type */function testType(): void{ it("should be OK", () => { expect( vs.string().applyTo("abc") ).toEqual("abc");
expect( vs.string({ strictType: true, }).applyTo("abc") ).toEqual("abc"); }); it("should be adjusted", () => { expect( vs.string().applyTo(0) ).toEqual("0");
expect( vs.string().applyTo(-1) ).toEqual("-1");
expect( vs.string().applyTo(true) ).toEqual("true");
expect( vs.string().applyTo(false) ).toEqual("false"); }); it("should cause error(s)", () => { expect(() => { vs.string().applyTo([]); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.string().applyTo({}); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.string({ strictType: true, }).applyTo(0); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.string({ strictType: true, }).applyTo(true); }).toThrow(vs.CAUSE.TYPE); });}
/** * if undefined */function testIfUndefined(): void{ it("should be adjusted", () => { expect( vs.string({ ifUndefined: "xyz", }).applyTo(undefined) ).toEqual("xyz"); }); it("should cause error(s)", () => { expect(() => { vs.string().applyTo(undefined); }).toThrow(vs.CAUSE.UNDEFINED); });}
/** * if null */function testIfNull(): void{ it("should be adjusted", () => { expect( vs.string({ ifNull: "abc", }).applyTo(null) ).toEqual("abc");
expect( vs.string({ ifNull: null, }).applyTo(null) ).toEqual(null); }); it("should cause error(s)", () => { expect(() => { vs.string().applyTo(null); }).toThrow(vs.CAUSE.NULL); });}
/** * if empty string */function testIfEmptyString(): void{ it("should be OK", () => { expect( vs.string({ ifEmptyString: "qwerty", }).applyTo("") ).toEqual("qwerty");
expect( vs.string({ ifEmptyString: null, }).applyTo("") ).toEqual(null); }); it("should cause error(s)", () => { expect(() => { vs.string().applyTo(""); }).toThrow(vs.CAUSE.EMPTY_STRING); });}
/** * remove whitespace from both ends */function testTrims(): void{ it("should be adjusted", () => { expect( vs.string({ trims: true, }).applyTo("\r\n hell, word \t ") ).toEqual("hell, word"); }); it("should cause error(s)", () => { expect(() => { vs.string({ trims: true, }).applyTo(" \t\r\n "); }).toThrow(vs.CAUSE.EMPTY_STRING); });}
/** * only */function testOnly(): void{ it("should be OK", () => { expect( vs.string({ only: ["", "eat", "sleep", "play"], }).applyTo("") ).toEqual("");
expect( vs.string({ only: ["", "eat", "sleep", "play"], }).applyTo("eat") ).toEqual("eat");
expect( vs.string({ only: ["", "eat", "sleep", "play"], }).applyTo("sleep") ).toEqual("sleep");
expect( vs.string({ only: ["", "eat", "sleep", "play"], }).applyTo("play") ).toEqual("play"); }); it("should cause error(s)", () => { expect(() => { vs.string({ only: ["", "eat", "sleep", "play"], }).applyTo("study"); }).toThrow(vs.CAUSE.ONLY); expect(() => { vs.string({ only: ["", "eat", "sleep", "play"], }).applyTo("EAT"); }).toThrow(vs.CAUSE.ONLY); });}
/** * minimum length of string */function testMinLength(): void{ it("should be OK", () => { expect( vs.string({ minLength: 4, }).applyTo("1234") ).toEqual("1234"); }); it("should cause error(s)", () => { expect(() => { vs.string({ minLength: 4, }).applyTo("abc"); }).toThrow(vs.CAUSE.MIN_LENGTH); });}
/** * maximum length of string */function testMaxLength(): void{ it("should be OK", () => { expect( vs.string({ maxLength: 8, }).applyTo("12345678") ).toEqual("12345678"); }); it("should be adjusted", () => { expect( vs.string({ maxLength: { length: 8, trims: true, }, }).applyTo("123456789") ).toEqual("12345678"); }); it("should cause error(s)", () => { expect(() => { vs.string({ maxLength: 8, }).applyTo("123456789"); }).toThrow(vs.CAUSE.MAX_LENGTH); });}
/** * pattern */function testPattern(): void{ describe("http", () => { testpatternHttp(); }); describe("ipv4", () => { testPatternIpv4(); }); describe("ipv6", () => { testPatternIpv6(); }); describe("uri", () => { testPatternUri(); }); describe("uuid", () => { testPatternUuid(); }); describe("others", () => { testPatternOthers(); });}
/** * pattern; HTTP */function testpatternHttp(): void{ it("should be OK", () => { expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://example.com/") ).toEqual("https://example.com/");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://example.com") ).toEqual("https://example.com");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://example.com:") ).toEqual("https://example.com:");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://user:password@example.com:8080") ).toEqual("https://user:password@example.com:8080");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://example.com/path/to/resource?name=value#hash") ).toEqual("https://example.com/path/to/resource?name=value#hash");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://192.168.10.2/") ).toEqual("https://192.168.10.2/");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://[fe80::a1b3:125d:c1f8:4781]/") ).toEqual("https://[fe80::a1b3:125d:c1f8:4781]/");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://github.com/shimataro/value-schema") ).toEqual("https://github.com/shimataro/value-schema");
expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://www.npmjs.com/package/vs") ).toEqual("https://www.npmjs.com/package/vs");
// https://tools.ietf.org/html/rfc3986#section-1.1.2 expect( vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("http://www.ietf.org/rfc/rfc2396.txt") ).toEqual("http://www.ietf.org/rfc/rfc2396.txt"); }); it("should cause error(s)", () => { expect(() => { vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://例.com/"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("http:/example.com/"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("http://example.com::80/"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("http://example.com:abc/"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.HTTP, }).applyTo("https://1[fe80::a1b3:125d:c1f8:4781]/"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * pattern; IPv4 */function testPatternIpv4(): void{ it("should be OK", () => { expect( vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("0.0.0.0") ).toEqual("0.0.0.0");
expect( vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("192.168.0.1") ).toEqual("192.168.0.1");
expect( vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("255.255.255.255") ).toEqual("255.255.255.255"); }); it("should cause error(s)", () => { expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("0.0.0."); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("0.0.0.0."); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("255.255.255.256"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV4, }).applyTo("999.255.255.255"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * pattern; IPv6 */function testPatternIpv6(): void{ it("should be OK", () => { expect( vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("0000:0000:0000:0000:0000:0000:0000:0000") ).toEqual("0000:0000:0000:0000:0000:0000:0000:0000");
expect( vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("::1") ).toEqual("::1");
expect( vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("::") ).toEqual("::");
expect( vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("1::1") ).toEqual("1::1");
// IPv4-mapped address expect( vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("::ffff:192.0.2.1") ).toEqual("::ffff:192.0.2.1"); }); it("should cause error(s)", () => { expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("0000"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("ffff:"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.IPV6, }).applyTo("0000:0000:0000:0000:0000:0000:0000:0000:"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * pattern; URI */function testPatternUri(): void{ it("should be OK", () => { expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("https://example.com/path/to/resource?name=value#hash") ).toEqual("https://example.com/path/to/resource?name=value#hash");
expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("https://192.168.10.2/") ).toEqual("https://192.168.10.2/");
expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("https://[fe80::a1b3:125d:c1f8:4781]/") ).toEqual("https://[fe80::a1b3:125d:c1f8:4781]/");
// https://tools.ietf.org/html/rfc3986#section-1.1.2 expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("ftp://ftp.is.co.za/rfc/rfc1808.txt") ).toEqual("ftp://ftp.is.co.za/rfc/rfc1808.txt");
expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("http://www.ietf.org/rfc/rfc2396.txt") ).toEqual("http://www.ietf.org/rfc/rfc2396.txt");
expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("ldap://[2001:db8::7]/c=GB?objectClass?one") ).toEqual("ldap://[2001:db8::7]/c=GB?objectClass?one"); expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("mailto:John.Doe@example.com") ).toEqual("mailto:John.Doe@example.com"); expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("news:comp.infosystems.www.servers.unix") ).toEqual("news:comp.infosystems.www.servers.unix"); expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("tel:+1-816-555-1212") ).toEqual("tel:+1-816-555-1212"); expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("telnet://192.0.2.16:80/") ).toEqual("telnet://192.0.2.16:80/"); expect( vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("urn:oasis:names:specification:docbook:dtd:xml:4.1.2") ).toEqual("urn:oasis:names:specification:docbook:dtd:xml:4.1.2"); }); it("should cause error(s)", () => { expect(() => { vs.string({ pattern: vs.STRING.PATTERN.URI, }).applyTo("https://例.com/"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * pattern; UUID */function testPatternUuid(): void{ it("should be OK", () => { expect( vs.string({ pattern: vs.STRING.PATTERN.UUID, }).applyTo("966a073b-b26e-4f88-888d-1f3a4ccfcd31") ).toEqual("966a073b-b26e-4f88-888d-1f3a4ccfcd31");
expect( vs.string({ pattern: vs.STRING.PATTERN.UUID, }).applyTo("966A073B-B26E-4F88-888D-1F3A4CCFCD31") ).toEqual("966A073B-B26E-4F88-888D-1F3A4CCFCD31"); }); it("should cause error(s)", () => { expect(() => { vs.string({ pattern: vs.STRING.PATTERN.UUID, }).applyTo("X966a073b-b26e-4f88-888d-1f3a4ccfcd31X"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.UUID, }).applyTo("966a073bb26e4f88888d1f3a4ccfcd31"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.UUID, }).applyTo("this-is-not-a-UUID-pattern"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * pattern; Others */function testPatternOthers(): void{ it("should be OK", () => { expect( vs.string({ pattern: /^Go+gle$/, }).applyTo("Gogle") ).toEqual("Gogle");
expect( vs.string({ pattern: /^Go+gle$/, }).applyTo("Google") ).toEqual("Google");
expect( vs.string({ pattern: /^Go+gle$/, }).applyTo("Gooogle") ).toEqual("Gooogle");
expect( vs.string({ pattern: vs.STRING.PATTERN.EMAIL, }).applyTo("john.doe@example.com") ).toEqual("john.doe@example.com"); }); it("should cause error(s)", () => { expect(() => { vs.string({ pattern: /^Go+gle$/, }).applyTo("Ggle"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: /^Go+gle$/, }).applyTo("google"); }).toThrow(vs.CAUSE.PATTERN);
expect(() => { vs.string({ pattern: vs.STRING.PATTERN.EMAIL, }).applyTo("john..doe@example.com"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * converter */function testConverter(): void{ it("should be adjusted", () => { expect( vs.string({ converter: (value) => { return value.toLowerCase(); }, }).applyTo("123ABCxyz") ).toEqual("123abcxyz");
expect( vs.string({ converter: (value, fail) => { if(value.length >= 9) { fail(); } return value.padStart(8, " "); }, }).applyTo("test") ).toEqual(" test"); }); it("should cause error(s)", () => { expect(() => { vs.string({ converter: (value, fail) => { if(value.length >= 9) { fail(); } return value.padStart(8, " "); }, }).applyTo("123ABCxyz"); }).toThrow(vs.CAUSE.CONVERTER); });}