Module
x/value_schema/test/schemas/numericString.test.ts
simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, Deno, and Bun
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341import vs from "value-schema";import {describe, expect, it} from "@jest/globals";
{ describe("type", testType); describe("ifUndefined", testIfUndefined); describe("ifNull", testIfNull); describe("joinArray", testJoinArray); describe("separatedBy", testSeparatedBy); describe("minLength", testMinLength); describe("maxLength", testMaxLength); describe("checksum (Luhn)", testChecksumLuhn); describe("checksum (Modulus 10 / Weight 3:1)", testChecksumModulus10Weight31); describe("checksum (Others)", testChecksumOthers); describe("converter", testConverter);}
/** * type */function testType(): void{ it("should be OK", () => { expect( vs.numericString().applyTo("1111222233334444") ).toEqual("1111222233334444"); }); it("should be adjusted", () => { expect( vs.numericString().applyTo(1111222233334444) ).toEqual("1111222233334444");
expect( vs.numericString({ fullWidthToHalf: true, }).applyTo("1111222233334444") ).toEqual("1111222233334444"); }); it("should cause error(s)", () => { expect(() => { vs.numericString().applyTo("1111222233334444"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * if undefined */function testIfUndefined(): void{ it("should be adjusted", () => { expect( vs.numericString({ ifUndefined: "12345", }).applyTo(undefined) ).toEqual("12345"); }); it("should cause error(s)", () => { expect(() => { vs.numericString().applyTo(undefined); }).toThrow(vs.CAUSE.UNDEFINED); });}
/** * if null */function testIfNull(): void{ it("should be adjusted", () => { expect( vs.numericString({ ifNull: "123", }).applyTo(null) ).toEqual("123");
expect( vs.numericString({ ifNull: null, }).applyTo(null) ).toEqual(null); }); it("should cause error(s)", () => { expect(() => { vs.numericString().applyTo(null); }).toThrow(vs.CAUSE.NULL); });}
/** * join array */function testJoinArray(): void{ it("should be adjusted", () => { expect( vs.numericString({ joinsArray: true, }).applyTo(["1111", "2222", "3333", "4444"]) ).toEqual("1111222233334444"); }); it("should cause error(s)", () => { expect(() => { vs.numericString().applyTo(["1111", "2222", "3333", "4444"]); }).toThrow(vs.CAUSE.TYPE); });}
/** * separator */function testSeparatedBy(): void{ it("should be adjusted", () => { expect( vs.numericString({ separatedBy: "-", }).applyTo("1111-2222-3333-4444") ).toEqual("1111222233334444"); }); it("should cause error(s)", () => { expect(() => { vs.numericString().applyTo("1111-2222-3333-4444"); }).toThrow(vs.CAUSE.PATTERN); });}
/** * min-length */function testMinLength(): void{ it("should be OK", () => { expect( vs.numericString({ minLength: 4, separatedBy: "-", }).applyTo("111-1") ).toEqual("1111"); }); it("should cause error(s)", () => { expect(() => { vs.numericString({ minLength: 4, separatedBy: "-", }).applyTo("111"); }).toThrow(vs.CAUSE.MIN_LENGTH); expect(() => { vs.numericString({ minLength: 4, separatedBy: "-", }).applyTo("11-1"); }).toThrow(vs.CAUSE.MIN_LENGTH); });}
/** * max-length */function testMaxLength(): void{ it("should be OK", () => { expect( vs.numericString({ maxLength: 4, }).applyTo("1111") ).toEqual("1111"); expect( vs.numericString({ maxLength: 4, separatedBy: "-", }).applyTo("11-22") ).toEqual("1122"); }); it("should be adjusted", () => { expect( vs.numericString({ maxLength: { length: 4, trims: true, }, }).applyTo("12345") ).toEqual("1234"); expect( vs.numericString({ maxLength: { length: 4, trims: true, }, separatedBy: "-", }).applyTo("123-456") ).toEqual("1234"); }); it("should cause error(s)", () => { expect(() => { vs.numericString({ maxLength: 4, }).applyTo("11111"); }).toThrow(vs.CAUSE.MAX_LENGTH); });}
/** * checksum - Luhn algorithm */function testChecksumLuhn(): void{ it("should be OK", () => { expect( vs.numericString({ checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.CREDIT_CARD, }).applyTo("49927398716") ).toEqual("49927398716"); expect( vs.numericString({ checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.CREDIT_CARD, }).applyTo("049927398716") ).toEqual("049927398716"); }); it("should cause error(s)", () => { expect(() => { vs.numericString({ checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.CREDIT_CARD, }).applyTo("12345"); }).toThrow(vs.CAUSE.CHECKSUM); });}
/** * checksum - Modulus 10 / Weight 3:1 */function testChecksumModulus10Weight31(): void{ it("should be OK", () => { expect( vs.numericString({ separatedBy: "-", checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.ISBN13, }).applyTo("978-4-10-109205-8") ).toEqual("9784101092058"); // https://ja.wikipedia.org/wiki/ISBN expect( vs.numericString({ separatedBy: "-", checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.ISBN13, }).applyTo("978-0-306-40615-7") ).toEqual("9780306406157"); // https://en.wikipedia.org/wiki/International_Standard_Book_Number }); it("should cause error(s)", () => { expect(() => { vs.numericString({ separatedBy: "-", checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.ISBN13, }).applyTo("978-4-10-109205-1"); }).toThrow(vs.CAUSE.CHECKSUM); });}
/** * checksum - Others */function testChecksumOthers(): void{ it("should cause error(s)", () => { expect(() => { vs.numericString({ checksum: -1 as any, // eslint-disable-line @typescript-eslint/no-explicit-any }).applyTo("0123456789"); }).toThrow(vs.CAUSE.CHECKSUM); });}
/** * converter */function testConverter(): void{ it("should be adjusted", () => { expect( vs.numericString({ converter: (value, fail) => { if(value.length >= 9) { fail(); } return value.padStart(8, "0"); }, }).applyTo("1234") ).toEqual("00001234"); }); it("should cause error(s)", () => { expect(() => { vs.string({ converter: (value, fail) => { if(value.length >= 9) { fail(); } return value.padStart(8, "0"); }, }).applyTo("123456789"); }).toThrow(vs.CAUSE.CONVERTER); });}