Skip to main content
Module

x/value_schema/test/schemas/number.test.ts

simple, easy-to-use, and declarative input validator; supports Node.js, TypeScript, Deno, and Bun
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
import vs from "value-schema";import {describe, expect, it} from "@jest/globals";
{ describe("type", testType); describe("integer", testInteger); describe("ifUndefined", testIfUndefined); describe("ifNull", testIfNull); describe("ifEmptyString", testIfEmptyString); describe("only", testOnly); describe("minValue", testMinValue); describe("maxValue", testMaxValue); describe("converter", testConverter);}
/** * type */function testType(): void{ it("should be OK", () => { expect( vs.number().applyTo(0) ).toEqual(0);
expect( vs.number().applyTo(3.14) ).toEqual(3.14);
expect( vs.number({ strictType: true, }).applyTo(0) ).toEqual(0);
expect( vs.number({ strictType: true, }).applyTo(3.14) ).toEqual(3.14); }); it("should be adjusted", () => { expect( vs.number().applyTo("123") ).toEqual(123);
expect( vs.number().applyTo("+456") ).toEqual(456);
expect( vs.number().applyTo("-789") ).toEqual(-789);
expect( vs.number().applyTo(true) ).toEqual(1);
expect( vs.number().applyTo(false) ).toEqual(0);
expect( vs.number({ acceptsSpecialFormats: true, }).applyTo("1e+2") ).toEqual(100);
expect( vs.number({ acceptsSpecialFormats: true, }).applyTo("0x100") ).toEqual(256);
expect( vs.number({ acceptsSpecialFormats: true, }).applyTo("0o100") ).toEqual(64);
expect( vs.number({ acceptsSpecialFormats: true, }).applyTo("0b100") ).toEqual(4);
expect( vs.number({ acceptsFullWidth: true, }).applyTo("+1234.5") ).toEqual(1234.5);
expect( vs.number({ acceptsFullWidth: true, }).applyTo("-1234.5") ).toEqual(-1234.5); }); it("should cause error(s)", () => { expect(() => { vs.number().applyTo("true"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo("false"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number({ acceptsSpecialFormats: true, }).applyTo("true"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo("1e+2"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo("0x100"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo("0o100"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo("0b100"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo("1234.5"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo([]); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number().applyTo({}); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number({ strictType: true, }).applyTo("1"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number({ strictType: true, }).applyTo(true); }).toThrow(vs.CAUSE.TYPE); });}
/** * integer */function testInteger(): void{ it("should be OK", () => { expect( vs.number({ integer: false, }).applyTo(0) ).toEqual(0); expect( vs.number({ integer: vs.NUMBER.INTEGER.NO, }).applyTo(0) ).toEqual(0);
expect( vs.number({ integer: false, }).applyTo(0.5) ).toEqual(0.5);
expect( vs.number({ integer: true, }).applyTo(0) ).toEqual(0); }); describe("rounding", () => { it("floor", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.FLOOR, }).applyTo(3.14) ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.FLOOR, }).applyTo("3.14") ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.FLOOR, }).applyTo(-3.14) ).toEqual(-4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.FLOOR, }).applyTo("-3.14") ).toEqual(-4); });
it("floor (toward zero)", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.FLOOR_RZ, }).applyTo(3.14) ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.FLOOR_RZ, }).applyTo(-3.14) ).toEqual(-3); });
it("ceil", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.CEIL, }).applyTo(3.14) ).toEqual(4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.CEIL, }).applyTo(-3.14) ).toEqual(-3); });
it("ceil (toward zero)", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.CEIL_RI, }).applyTo(3.14) ).toEqual(4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.CEIL_RI, }).applyTo(-3.14) ).toEqual(-4); });
it("half up", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP, }).applyTo(3.49) ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP, }).applyTo(3.5) ).toEqual(4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP, }).applyTo(-3.49) ).toEqual(-3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP, }).applyTo(-3.5) ).toEqual(-3); });
it("half up (toward zero)", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP_RZ, }).applyTo(3.49) ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP_RZ, }).applyTo(3.5) ).toEqual(4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP_RZ, }).applyTo(-3.49) ).toEqual(-3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_UP_RZ, }).applyTo(-3.5) ).toEqual(-4); });
it("half down", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN, }).applyTo(3.5) ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN, }).applyTo(3.51) ).toEqual(4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN, }).applyTo(-3.5) ).toEqual(-4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN, }).applyTo(-3.49) ).toEqual(-3); });
it("half down (toward zero)", () => { expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN_RZ, }).applyTo(3.5) ).toEqual(3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN_RZ, }).applyTo(3.51) ).toEqual(4);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN_RZ, }).applyTo(-3.5) ).toEqual(-3);
expect( vs.number({ integer: vs.NUMBER.INTEGER.HALF_DOWN_RZ, }).applyTo(-3.51) ).toEqual(-4); }); }); it("should cause error(s)", () => { expect(() => { vs.number({ integer: true, }).applyTo(3.14); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number({ integer: true, }).applyTo("3.14"); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number({ integer: true, }).applyTo("3."); }).toThrow(vs.CAUSE.TYPE);
expect(() => { vs.number({ integer: -1 as any, // eslint-disable-line @typescript-eslint/no-explicit-any }).applyTo(0.1); }).toThrow(vs.CAUSE.TYPE); });}
/** * if undefined */function testIfUndefined(): void{ it("should be OK", () => { expect( vs.number({ ifUndefined: 10, }).applyTo(1) ).toEqual(1); }); it("should be adjusted", () => { expect( vs.number({ ifUndefined: 10, }).applyTo(undefined) ).toEqual(10); }); it("should cause error(s)", () => { expect(() => { vs.number().applyTo(undefined); }).toThrow(vs.CAUSE.UNDEFINED); });}
/** * if null */function testIfNull(): void{ it("should be adjusted", () => { expect( vs.number({ ifNull: 123, }).applyTo(null) ).toEqual(123); }); it("should cause error(s)", () => { expect(() => { vs.number().applyTo(null); }).toThrow(vs.CAUSE.NULL); });}
/** * if empty string */function testIfEmptyString(): void{ it("should be adjusted", () => { expect( vs.number({ ifEmptyString: 123, }).applyTo("") ).toEqual(123);
expect( vs.number({ ifEmptyString: null, }).applyTo("") ).toEqual(null); }); it("should cause error(s)", () => { expect(() => { vs.number().applyTo(""); }).toThrow(vs.CAUSE.EMPTY_STRING); });}
/** * only */function testOnly(): void{ it("should be OK", () => { expect( vs.number({ only: [1, 3, 5], }).applyTo(1) ).toEqual(1);
expect( vs.number({ only: [1, 3, 5], }).applyTo(3) ).toEqual(3);
expect( vs.number({ only: [1, 3, 5], }).applyTo(5) ).toEqual(5); }); it("should cause error(s)", () => { expect(() => { vs.number({ only: [1, 3, 5], }).applyTo(2); }).toThrow(vs.CAUSE.ONLY); });}
/** * minimum value */function testMinValue(): void{ it("should be OK", () => { expect( vs.number().applyTo(Number.MIN_SAFE_INTEGER) ).toEqual(Number.MIN_SAFE_INTEGER); expect( vs.number({ minValue: 1, }).applyTo(10) ).toEqual(10); }); it("should be adjusted", () => { expect( vs.number({ minValue: { value: 10, adjusts: true, }, }).applyTo(9) ).toEqual(10); }); it("should cause error(s)", () => { expect(() => { vs.number().applyTo(Number.MIN_SAFE_INTEGER - 1); }).toThrow(vs.CAUSE.MIN_VALUE); expect(() => { vs.number({ minValue: 10, }).applyTo(9); }).toThrow(vs.CAUSE.MIN_VALUE); });}
/** * maximum value */function testMaxValue(): void{ it("should be OK", () => { expect( vs.number().applyTo(Number.MAX_SAFE_INTEGER) ).toEqual(Number.MAX_SAFE_INTEGER); expect( vs.number({ maxValue: 100, }).applyTo(100) ).toEqual(100); }); it("should be adjusted", () => { expect( vs.number({ maxValue: { value: 100, adjusts: true, }, }).applyTo(101) ).toEqual(100); }); it("should cause error(s)", () => { expect(() => { vs.number().applyTo(Number.MAX_SAFE_INTEGER + 1); }).toThrow(vs.CAUSE.MAX_VALUE); expect(() => { vs.number({ maxValue: 100, }).applyTo(101); }).toThrow(vs.CAUSE.MAX_VALUE); });}
/** * converter */function testConverter(): void{ it("should be adjusted", () => { expect( vs.number({ converter: (value) => { return value * 2; }, }).applyTo("1") ).toEqual(2); });}