Module
x/value_schema/test/adjusters/numberArray.test.es
simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, and Deno
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353import 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); });}