Skip to main content
Module

x/value_schema/test/schemas/array.test.ts

simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, Deno, and Bun
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
import vs from "value-schema";import {describe, expect, it} from "@jest/globals";
{ describe("type", testType); describe("ifUndefined", testIfUndefined); describe("ifNull", testIfNull); describe("ifEmptyString", testIfEmptyString); describe("separatedBy", testSeparatedBy); describe("toArray", testToArray); describe("minLength", testMinLength); describe("maxLength", testMaxLength); describe("converter", testConverter); describe("number", testNumber); describe("string", testString);}
/** * type */function testType(): void{ it("should be OK", () => { expect( vs.array().applyTo([]) ).toEqual([]);
expect( vs.array().applyTo([1, 2]) ).toEqual([1, 2]);
expect( vs.array().applyTo(["a", "b"]) ).toEqual(["a", "b"]);
expect( vs.array().applyTo([1, "a"]) ).toEqual([1, "a"]); });}
/** * if undefined */function testIfUndefined(): void{ it("should be adjusted", () => { expect( vs.array({ ifUndefined: [1, "a"], }).applyTo(undefined) ).toEqual([1, "a"]); }); it("should cause error(s)", () => { expect(() => { vs.array().applyTo(undefined); }).toThrow(vs.CAUSE.UNDEFINED); });}
/** * if null */function testIfNull(): void{ it("should be adjusted", () => { expect( vs.array({ ifNull: [1, "a"], }).applyTo(null) ).toEqual([1, "a"]);
expect( vs.array({ ifNull: null, }).applyTo(null) ).toEqual(null); }); it("should cause error(s)", () => { expect(() => { vs.array().applyTo(null); }).toThrow(vs.CAUSE.NULL); });}
/** * if empty string */function testIfEmptyString(): void{ it("should be adjusted", () => { expect( vs.array({ ifEmptyString: [1, "a"], }).applyTo("") ).toEqual([1, "a"]);
expect( vs.array({ ifEmptyString: null, }).applyTo("") ).toEqual(null); }); it("should cause error(s)", () => { expect(() => { vs.array().applyTo(""); }).toThrow(vs.CAUSE.EMPTY_STRING); });}
/** * separator */function testSeparatedBy(): void{ it("should be adjusted", () => { expect( vs.array({ separatedBy: ",", }).applyTo("1,a") ).toEqual(["1", "a"]); });}
/** * convert to array */function testToArray(): void{ it("should be adjusted", () => { expect( vs.array({ toArray: true, }).applyTo("abc") ).toEqual(["abc"]); }); it("should cause error(s)", () => { expect(() => { vs.array().applyTo("abc"); }).toThrow(vs.CAUSE.TYPE); });}
/** * minimum length of elements */function testMinLength(): void{ it("should be OK", () => { expect( vs.array({ minLength: 1, }).applyTo(["a"]) ).toEqual(["a"]); }); it("should cause error(s)", () => { expect(() => { vs.array({ minLength: 1, }).applyTo([]); }).toThrow(vs.CAUSE.MIN_LENGTH); });}
/** * maximum length of elements */function testMaxLength(): void{ it("should be OK", () => { expect( vs.array({ maxLength: 1, }).applyTo([1]) ).toEqual([1]);
expect( vs.array({ maxLength: { length: 1, trims: true, }, }).applyTo([1]) ).toEqual([1]); }); it("should be adjusted", () => { expect( vs.array({ maxLength: { length: 1, trims: true, }, }).applyTo([1, 2]) ).toEqual([1]); }); it("should cause error(s)", () => { expect(() => { vs.array({ maxLength: 1, }).applyTo([1, 2]); }).toThrow(vs.CAUSE.MAX_LENGTH); });}
/** * converter */function testConverter(): void{ it("should be adjusted", () => { expect( vs.array({ each: vs.number(), separatedBy: ",", converter: (values) => { return values.sort(); }, }).applyTo("4,1,5,2") ).toEqual([1, 2, 4, 5]); });}
/** * array of number */function testNumber(): void{ it("should be OK", () => { expect( vs.array({ each: vs.number(), }).applyTo([3.14, 2.71]) ).toEqual([3.14, 2.71]);
expect( vs.array({ each: vs.number({ integer: true, }), }).applyTo([1, 2, 3]) ).toEqual([1, 2, 3]);
expect( vs.array({ each: vs.number({ only: [1, 2, 3], }), }).applyTo([1, 2, 3]) ).toEqual([1, 2, 3]);
expect( vs.array({ each: vs.number({ minValue: 10, }), }).applyTo([10, 11, 12]) ).toEqual([10, 11, 12]);
expect( vs.array({ each: vs.number({ maxValue: 10, }), }).applyTo([8, 9, 10]) ).toEqual([8, 9, 10]); }); it("should be adjusted", () => { expect( vs.array({ each: vs.number(), }).applyTo([false, true, 2, "+3", "-4"]) ).toEqual([0, 1, 2, 3, -4]);
expect( vs.array({ each: { schema: vs.number(), ignoresErrors: true, }, }).applyTo([false, "abc", true, "+2"]) ).toEqual([0, 1, 2]);
expect( vs.array({ separatedBy: ",", each: vs.number(), }).applyTo("1,2,3") ).toEqual([1, 2, 3]);
expect( vs.array({ each: vs.number({ ifUndefined: 999, }), }).applyTo(["1", undefined, 3]) ).toEqual([1, 999, 3]);
expect( vs.array({ each: vs.number({ ifNull: 999, }), }).applyTo(["1", null, 3]) ).toEqual([1, 999, 3]);
expect( vs.array({ each: vs.number({ ifEmptyString: 999, }), }).applyTo(["1", "", 3]) ).toEqual([1, 999, 3]);
expect( vs.array({ each: vs.number({ acceptsSpecialFormats: true, }), }).applyTo(["1e+2", "0x100", "0o100", "0b100"]) ).toEqual([100, 256, 64, 4]);
expect( vs.array({ each: vs.number({ integer: vs.NUMBER.INTEGER.FLOOR_RZ, }), }).applyTo([3.14, -3.14, "3.14"]) ).toEqual([3, -3, 3]);
expect( vs.array({ each: vs.number({ minValue: { value: 10, adjusts: true, }, }), }).applyTo([9, 10, 11]) ).toEqual([10, 10, 11]);
expect( vs.array({ each: vs.number({ maxValue: { value: 10, adjusts: true, }, }), }).applyTo([9, 10, 11]) ).toEqual([9, 10, 10]); });
it("should cause error(s)", () => { try { vs.array({ each: vs.number(), }).applyTo([false, "abc", true, "+2"]);
expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.TYPE); expect(err.keyStack).toEqual([1]); } }); it("should cause error(s)", () => { try { vs.array({ each: vs.number(), }).applyTo([1, undefined, 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.UNDEFINED); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number(), }).applyTo([1, null, 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.NULL); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number(), }).applyTo([1, "", 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.EMPTY_STRING); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number({ ifUndefined: 999, }), }).applyTo([1, null, 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.NULL); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number({ ifUndefined: 999, }), }).applyTo([1, "", 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.EMPTY_STRING); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number({ ifNull: 999, }), }).applyTo([1, undefined, 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.UNDEFINED); expect(err.keyStack).toEqual([1]); } }); it("should cause error(s)", () => { try { vs.array({ each: vs.number({ ifNull: 999, }), }).applyTo([1, "", 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.EMPTY_STRING); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number({ ifEmptyString: 999, }), }).applyTo([1, undefined, 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.UNDEFINED); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number({ ifEmptyString: 999, }), }).applyTo([1, null, 3]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.NULL); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.number(), }).applyTo(["1e+2", "0x100", "0o100", "0b100"]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.TYPE); expect(err.keyStack).toEqual([0]); }
try { vs.array({ each: vs.number({ integer: true, }), }).applyTo([3.14]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.TYPE); expect(err.keyStack).toEqual([0]); }
try { vs.array({ each: vs.number({ only: [1, 2, 3], }), }).applyTo([0, 1, 2]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.ONLY); expect(err.keyStack).toEqual([0]); } }); it("should cause error(s)", () => { try { vs.array({ each: vs.number({ minValue: 10, }), }).applyTo([9, 10, 11]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.MIN_VALUE); expect(err.keyStack).toEqual([0]); }
try { vs.array({ each: vs.number({ maxValue: 10, }), }).applyTo([9, 10, 11]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.MAX_VALUE); expect(err.keyStack).toEqual([2]); } });}
/** * array of string */function testString(): void{ it("should be OK", () => { expect( vs.array({ each: vs.string({ only: ["a", "b", "c"], }), }).applyTo(["a", "b", "c"]) ).toEqual(["a", "b", "c"]);
expect( vs.array({ each: vs.string({ minLength: 3, }), }).applyTo(["abc", "xyz"]) ).toEqual(["abc", "xyz"]);
expect( vs.array({ each: vs.string({ maxLength: 3, }), }).applyTo(["abc", "xyz"]) ).toEqual(["abc", "xyz"]);
expect( vs.array({ each: vs.string({ maxLength: { length: 3, trims: true, }, }), }).applyTo(["abc", "xyz"]) ).toEqual(["abc", "xyz"]);
expect( vs.array({ each: vs.string({ pattern: /^Go+gle$/, }), }).applyTo(["Gogle", "Google", "Gooogle"]) ).toEqual(["Gogle", "Google", "Gooogle"]); }); it("should be adjusted", () => { expect( vs.array({ each: vs.string(), }).applyTo([false, true, 2, "+3", "-4"]) ).toEqual(["false", "true", "2", "+3", "-4"]);
expect( vs.array({ each: vs.string({ trims: true, }), }).applyTo([" a", "b\t", "\rc\n"]) ).toEqual(["a", "b", "c"]);
expect( vs.array({ each: vs.string({ only: ["a", "b", "c"], }), }).applyTo(["a", "b", "c"]) ).toEqual(["a", "b", "c"]);
expect( vs.array({ each: vs.string({ maxLength: { length: 3, trims: true, }, }), }).applyTo(["abcd", "xyz0"]) ).toEqual(["abc", "xyz"]); }); it("should cause error(s)", () => { try { vs.array({ each: vs.string({ only: ["a", "b", "c"], }), }).applyTo(["a", "b", "x"]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.ONLY); expect(err.keyStack).toEqual([2]); }
try { vs.array({ each: vs.string({ minLength: 3, }), }).applyTo(["ab", "xy"]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.MIN_LENGTH); expect(err.keyStack).toEqual([0]); }
try { vs.array({ each: vs.string({ maxLength: 3, }), }).applyTo(["abc", "xyz0"]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.MAX_LENGTH); expect(err.keyStack).toEqual([1]); }
try { vs.array({ each: vs.string({ pattern: /^Go+gle$/, }), }).applyTo(["google", "Ggle"]); expect(true).toEqual(false); } catch(err) { expect(err instanceof vs.ValueSchemaError).toBeTruthy(); if(!(err instanceof vs.ValueSchemaError)) { return; } expect(err.cause).toEqual(vs.CAUSE.PATTERN); expect(err.keyStack).toEqual([0]); } });}