Module
x/value_schema/test/adjusters/ipv4.test.es
simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, and Deno
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import adjuster from "index";
{ describe("default", testDefault); describe("acceptNull", testAcceptNull); describe("acceptEmptyString", testAcceptEmptyString); describe("trim", testTrim); describe("pattern", testPattern);}
/** * default value * @return {void} */function testDefault(){ it("should be adjusted", () => { expect(adjuster.ipv4().default("1.1.1.1") .adjust(undefined)).toEqual("1.1.1.1"); }); it("should cause error(s)", () => { expect(() => { adjuster.ipv4() .adjust(undefined); }).toThrow(adjuster.CAUSE.REQUIRED); });}
/** * null * @return {void} */function testAcceptNull(){ it("should be OK", () => { expect(adjuster.ipv4().acceptNull("1.1.1.1") .adjust(null)).toEqual("1.1.1.1"); }); it("should cause error(s)", () => { expect(() => { adjuster.ipv4() .adjust(null); }).toThrow(adjuster.CAUSE.NULL); });}
/** * empty string * @return {void} */function testAcceptEmptyString(){ it("should be OK", () => { expect(adjuster.ipv4().acceptEmptyString("1.1.1.1") .adjust("")).toEqual("1.1.1.1"); }); it("should cause error(s)", () => { expect(() => { adjuster.ipv4() .adjust(""); }).toThrow(adjuster.CAUSE.EMPTY); });}
/** * remove whitespace from both ends * @return {void} */function testTrim(){ it("should be adjusted", () => { expect(adjuster.ipv4().trim() .adjust("\r\n 1.1.1.1 \t ")).toEqual("1.1.1.1"); }); it("should cause error(s)", () => { expect(() => { adjuster.ipv4().trim() .adjust(" \t\r\n "); }).toThrow(adjuster.CAUSE.EMPTY); });}
/** * IPv4 pattern * @return {void} */function testPattern(){ it("should be OK", () => { const values = [ "0.0.0.0", "192.168.0.1", "255.255.255.255", ]; for(const value of values) { expect(adjuster.ipv4() .adjust(value)).toEqual(value); } }); it("should cause error(s)", () => { const values = [ "0.0.0.", "0.0.0.0.", "255.255.255.256", "999.255.255.255", ]; for(const value of values) { expect(() => { adjuster.ipv4() .adjust(value); }).toThrow(adjuster.CAUSE.PATTERN); } });}