Skip to main content
Module

x/value_schema/test/adjusters/numberArray.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("type", testType); describe("default", testDefault); describe("allowNull", testAllowNull); describe("allowEmptyString", testAllowEmptyString); describe("separatedBy", testSeparatedBy); describe("toArray", testToArray); describe("minLength", testMinLength); describe("maxLength", testMaxLength); describe("ignoreEachErrors", testIgnoreEachErrors); describe("eachDefault", testEachDefault); describe("eachAllowNull", testEachAllowNull); describe("eachAllowEmptyString", testEachAllowEmptyString); describe("eachOnly", testEachOnly); describe("eachMinValue", testEachMinValue); describe("eachMaxValue", testEachMaxValue);}
/** * type */function testType(){ it("should be OK", () => { expect(adjuster.numberArray() .adjust([])).toEqual([]);
expect(adjuster.numberArray() .adjust([1, 2])).toEqual([1, 2]); }); it("should be adjusted", () => { expect(adjuster.numberArray() .adjust([1, "2", "+3", "-4"])).toEqual([1, 2, 3, -4]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray() .adjust("abc"); }).toThrow(adjuster.CAUSE.TYPE); expect(() => { adjuster.numberArray() .adjust(0); }).toThrow(adjuster.CAUSE.TYPE); });}
/** * default value */function testDefault(){ it("should be adjusted", () => { expect(adjuster.numberArray().default([1, 2]) .adjust(undefined)).toEqual([1, 2]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray() .adjust(undefined); }).toThrow(adjuster.CAUSE.REQUIRED); });}
/** * null */function testAllowNull(){ it("should be adjusted", () => { expect(adjuster.numberArray().allowNull([1, 2, 3]) .adjust(null)).toEqual([1, 2, 3]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray() .adjust(null); }).toThrow(adjuster.CAUSE.NULL); });}
/** * empty string */function testAllowEmptyString(){ it("should be adjusted", () => { expect(adjuster.numberArray().allowEmptyString([1, 2]) .adjust("")).toEqual([1, 2]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray() .adjust(""); }).toThrow(adjuster.CAUSE.EMPTY); });}
/** * separator */function testSeparatedBy(){ it("should be adjusted", () => { expect(adjuster.numberArray().separatedBy(",") .adjust("1,2,3")).toEqual([1, 2, 3]); });}
/** * convert to array */function testToArray(){ it("should be adjusted", () => { expect(adjuster.numberArray().toArray() .adjust(0)).toEqual([0]); });}
/** * minimum length of elements */function testMinLength(){ it("should be OK", () => { expect(adjuster.numberArray().minLength(1) .adjust([0])).toEqual([0]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().minLength(1) .adjust([]); }).toThrow(adjuster.CAUSE.MIN_LENGTH); });}
/** * maximum length of elements */function testMaxLength(){ it("should be OK", () => { expect(adjuster.numberArray().maxLength(1) .adjust([0])).toEqual([0]);
expect(adjuster.numberArray().maxLength(1, true) .adjust([0])).toEqual([0]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().maxLength(1) .adjust([1, 2]); }).toThrow(adjuster.CAUSE.MAX_LENGTH); }); it("should be adjusted", () => { expect(adjuster.numberArray().maxLength(1, true) .adjust([1, 2])).toEqual([1]); });}
/** * ignore elements' error */function testIgnoreEachErrors(){ it("should be adjusted", () => { expect(adjuster.numberArray().ignoreEachErrors().separatedBy(",") .adjust([undefined, 1, "abc", 2])).toEqual([1, 2]);
expect(adjuster.numberArray().ignoreEachErrors().separatedBy(",") .adjust("1,abc,2,,3")).toEqual([1, 2, 3]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray() .adjust(["abc"]); }).toThrow(adjuster.CAUSE.EACH_TYPE); expect(() => { adjuster.numberArray() .adjust([1, undefined, 3]); }).toThrow(adjuster.CAUSE.EACH_REQUIRED); expect(() => { adjuster.numberArray() .adjust([""]); }).toThrow(adjuster.CAUSE.EACH_EMPTY); });}
/** * each elements; default value */function testEachDefault(){ it("should be adjusted", () => { expect(adjuster.numberArray().eachDefault(999) .adjust([1, undefined, 3])).toEqual([1, 999, 3]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().eachDefault(999) .adjust([1, "", 3]); }).toThrow(adjuster.CAUSE.EACH_EMPTY); });}
/** * each elements; null */function testEachAllowNull(){ it("should be adjusted", () => { expect(adjuster.numberArray().eachAllowNull(999) .adjust([1, null, 3])).toEqual([1, 999, 3]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray() .adjust([1, null, 3]); }).toThrow(adjuster.CAUSE.EACH_NULL); expect(() => { adjuster.numberArray().eachAllowNull(999) .adjust([1, undefined, 3]); }).toThrow(adjuster.CAUSE.EACH_REQUIRED); });}
/** * each elements; empty string */function testEachAllowEmptyString(){ it("should be adjusted", () => { expect(adjuster.numberArray().eachAllowEmptyString(999) .adjust([1, "", 3])).toEqual([1, 999, 3]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().eachAllowEmptyString(999) .adjust([1, undefined, 3]); }).toThrow(adjuster.CAUSE.EACH_REQUIRED); });}
/** * each elements; set */function testEachOnly(){ it("should be OK", () => { expect(adjuster.numberArray().eachOnly(1, 2, 3) .adjust([1, 2, 3])).toEqual([1, 2, 3]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().eachOnly(1, 2, 3) .adjust([0, 1, 2]); }).toThrow(adjuster.CAUSE.EACH_ONLY); });}
/** * each elements; minimum value */function testEachMinValue(){ it("should be OK", () => { expect(adjuster.numberArray().eachMinValue(10) .adjust([10, 11, 12])).toEqual([10, 11, 12]); }); it("should be adjusted", () => { expect(adjuster.numberArray().eachMinValue(10, true) .adjust([9, 10, 11])).toEqual([10, 10, 11]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().eachMinValue(10) .adjust([9, 10, 11]); }).toThrow(adjuster.CAUSE.EACH_MIN_VALUE); });}
/** * each elements; maximum value */function testEachMaxValue(){ it("should be OK", () => { expect(adjuster.numberArray().eachMaxValue(10) .adjust([8, 9, 10])).toEqual([8, 9, 10]); }); it("should be adjusted", () => { expect(adjuster.numberArray().eachMaxValue(10, true) .adjust([9, 10, 11])).toEqual([9, 10, 10]); }); it("should cause error(s)", () => { expect(() => { adjuster.numberArray().eachMaxValue(10) .adjust([9, 10, 11]); }).toThrow(adjuster.CAUSE.EACH_MAX_VALUE); });}