Skip to main content
Module

x/value_schema/test/adjusters/ipv4.test.es

simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, and Deno
Go to Latest
File
import 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); } });}