/* eslint-disable no-empty-function */ import { t, typeAsserts } from '../../index'; describe('array', () => { it('string array', () => { expect.hasAssertions(); const schema = t.array(t.string()); expect(schema.validate(['str'])).toBe(true); expect(schema.validate({})).toBe(false); expect(() => { typeAsserts(schema, {}); }).toThrow('param should be array, but got a(n) object'); expect(schema.validate(1)).toBe(false); expect(() => { typeAsserts(schema, 1); }).toThrow('param should be array, but got 1'); expect(schema.validate('str')).toBe(false); expect(() => { typeAsserts(schema, 1); }).toThrow('param should be array, but got 1'); expect(schema.validate([1])).toBe(false); expect(() => { typeAsserts(schema, [1]); }).toThrow('param[0] should be string, but got 1'); expect(schema.validate([true])).toBe(false); expect(() => { typeAsserts(schema, [true]); }).toThrow('param[0] should be string, but got true'); expect(schema.validate([false])).toBe(false); expect(() => { typeAsserts(schema, [false]); }).toThrow('param[0] should be string, but got false'); expect(schema.validate([Symbol(1)])).toBe(false); expect(() => { typeAsserts(schema, [Symbol(1)]); }).toThrow('param[0] should be string, but got a(n) symbol'); expect(schema.validate([null])).toBe(false); expect(() => { typeAsserts(schema, [null]); }).toThrow('param[0] should be string, but got null'); expect(schema.validate([undefined])).toBe(false); expect(() => { typeAsserts(schema, [undefined]); }).toThrow('param[0] should be string, but got undefined'); expect(schema.validate([BigInt(1)])).toBe(false); expect(() => { typeAsserts(schema, [BigInt(1)]); }).toThrow('param[0] should be string, but got a(n) bigint'); expect(schema.validate([{}])).toBe(false); expect(() => { typeAsserts(schema, [{}]); }).toThrow('param[0] should be string, but got a(n) object'); expect(schema.validate([() => {}])).toBe(false); expect(() => { typeAsserts(schema, [() => {}]); }).toThrow('param[0] should be string, but got a(n) function'); }); it('number array', () => { expect.hasAssertions(); const schema = t.array(t.number()); expect(schema.validate([1])).toBe(true); expect(() => { typeAsserts(schema, [1]); }).not.toThrow(); expect(schema.validate(['str'])).toBe(false); expect(() => { typeAsserts(schema, ['str']); }).toThrow('param[0] should be number, but got "str"'); expect(schema.validate([true])).toBe(false); expect(() => { typeAsserts(schema, [true]); }).toThrow('param[0] should be number, but got true'); expect(schema.validate([false])).toBe(false); expect(() => { typeAsserts(schema, [false]); }).toThrow('param[0] should be number, but got false'); expect(schema.validate([Symbol(1)])).toBe(false); expect(() => { typeAsserts(schema, [Symbol(1)]); }).toThrow('param[0] should be number, but got a(n) symbol'); expect(schema.validate([null])).toBe(false); expect(() => { typeAsserts(schema, [null]); }).toThrow('param[0] should be number, but got null'); expect(schema.validate([undefined])).toBe(false); expect(() => { typeAsserts(schema, [undefined]); }).toThrow('param[0] should be number, but got undefined'); expect(schema.validate([BigInt(1)])).toBe(false); expect(() => { typeAsserts(schema, [BigInt(1)]); }).toThrow('param[0] should be number, but got a(n) bigint'); expect(schema.validate([{}])).toBe(false); expect(() => { typeAsserts(schema, [{}]); }).toThrow('param[0] should be number, but got a(n) object'); expect(schema.validate([() => {}])).toBe(false); expect(() => { typeAsserts(schema, [() => {}]); }).toThrow('param[0] should be number, but got a(n) function'); }); it('boolean array', () => { expect.hasAssertions(); const schema = t.array(t.boolean()); expect(schema.validate([true])).toBe(true); expect(() => { typeAsserts(schema, [true]); }).not.toThrow(); expect(schema.validate([false])).toBe(true); expect(() => { typeAsserts(schema, [false]); }).not.toThrow(); expect(schema.validate([1])).toBe(false); expect(() => { typeAsserts(schema, [1]); }).toThrow('param[0] should be boolean, but got 1'); expect(schema.validate(['str'])).toBe(false); expect(() => { typeAsserts(schema, ['str']); }).toThrow('param[0] should be boolean, but got "str"'); expect(schema.validate([Symbol(1)])).toBe(false); expect(() => { typeAsserts(schema, [Symbol(1)]); }).toThrow('param[0] should be boolean, but got a(n) symbol'); expect(schema.validate([null])).toBe(false); expect(() => { typeAsserts(schema, [null]); }).toThrow('param[0] should be boolean, but got null'); expect(schema.validate([undefined])).toBe(false); expect(() => { typeAsserts(schema, [undefined]); }).toThrow('param[0] should be boolean, but got undefined'); expect(schema.validate([BigInt(1)])).toBe(false); expect(() => { typeAsserts(schema, [BigInt(1)]); }).toThrow('param[0] should be boolean, but got a(n) bigint'); expect(schema.validate([{}])).toBe(false); expect(() => { typeAsserts(schema, [{}]); }).toThrow('param[0] should be boolean, but got a(n) object'); expect(schema.validate([() => {}])).toBe(false); expect(() => { typeAsserts(schema, [() => {}]); }).toThrow('param[0] should be boolean, but got a(n) function'); }); it('undefined array', () => { expect.hasAssertions(); const schema = t.array(t.undefined()); expect(schema.validate([true])).toBe(false); expect(() => { typeAsserts(schema, [true]); }).toThrow('param[0] should be undefined, but got true'); expect(schema.validate([undefined])).toBe(true); expect(() => { typeAsserts(schema, [undefined]); }).not.toThrow(); expect(schema.validate([true])).toBe(false); expect(() => { typeAsserts(schema, [true]); }).toThrow('param[0] should be undefined, but got true'); expect(schema.validate([false])).toBe(false); expect(() => { typeAsserts(schema, [false]); }).toThrow('param[0] should be undefined, but got false'); expect(schema.validate([1])).toBe(false); expect(() => { typeAsserts(schema, [1]); }).toThrow('param[0] should be undefined, but got 1'); expect(schema.validate(['str'])).toBe(false); expect(() => { typeAsserts(schema, ['str']); }).toThrow('param[0] should be undefined, but got "str"'); expect(schema.validate([Symbol(1)])).toBe(false); expect(() => { typeAsserts(schema, [Symbol(1)]); }).toThrow('param[0] should be undefined, but got a(n) symbol'); expect(schema.validate([null])).toBe(false); expect(() => { typeAsserts(schema, [null]); }).toThrow('param[0] should be undefined, but got null'); expect(schema.validate([BigInt(1)])).toBe(false); expect(() => { typeAsserts(schema, [BigInt(1)]); }).toThrow('param[0] should be undefined, but got a(n) bigint'); expect(schema.validate([{}])).toBe(false); expect(() => { typeAsserts(schema, [{}]); }).toThrow('param[0] should be undefined, but got a(n) object'); expect(schema.validate([() => {}])).toBe(false); expect(() => { typeAsserts(schema, [() => {}]); }).toThrow('param[0] should be undefined, but got a(n) function'); }); it('null array', () => { expect.hasAssertions(); const schema = t.array(t.null()); expect(schema.validate([null])).toBe(true); expect(() => { typeAsserts(schema, [null]); }).not.toThrow(); expect(schema.validate([true])).toBe(false); expect(schema.validate([false])).toBe(false); expect(schema.validate([1])).toBe(false); expect(schema.validate(['str'])).toBe(false); expect(schema.validate([Symbol(1)])).toBe(false); expect(schema.validate([undefined])).toBe(false); expect(schema.validate([BigInt(1)])).toBe(false); expect(schema.validate([{}])).toBe(false); expect(schema.validate([() => {}])).toBe(false); }); it('object array', () => { expect.hasAssertions(); const schema = t.array(t.object()); expect(schema.validate([{}])).toBe(true); expect(() => { typeAsserts(schema, [{}]); }).not.toThrow(); expect(schema.validate([true])).toBe(false); expect(schema.validate([false])).toBe(false); expect(schema.validate([1])).toBe(false); expect(schema.validate(['str'])).toBe(false); expect(schema.validate([Symbol(1)])).toBe(false); expect(schema.validate([undefined])).toBe(false); expect(schema.validate([BigInt(1)])).toBe(false); expect(schema.validate([null])).toBe(false); expect(schema.validate([() => {}])).toBe(false); }); it('object.property array', () => { expect.hasAssertions(); const schema = t.array( t.object({ a: t.string(), b: t.number() }) ); expect(schema.validate([{ a: 'str', b: 1 }])).toBe(true); expect(() => { typeAsserts(schema, [{ a: 'str', b: 1 }]); }).not.toThrow(); expect(schema.validate([{}])).toBe(false); expect(schema.validate([{ a: 'str' }])).toBe(false); expect(schema.validate([{ b: 1 }])).toBe(false); }); it('primitive item error message', () => { expect.hasAssertions(); const schema = t.array(t.string()); expect(() => typeAsserts(schema, [1])).toThrow(`param[0] should be string, but got 1`); expect(() => typeAsserts(schema, ['str', 1])).toThrow(`param[1] should be string, but got 1`); }); it('object item error message', () => { expect.hasAssertions(); const schema = t.array( t.object({ a: t.object({ b: t.string() }) }) ); expect(() => { typeAsserts(schema, [{ a: { b: 123 } }]); }).toThrow(`param[0].a.b should be string, but got 123`); expect(() => typeAsserts(schema, [{ a: { b: 'str' } }, { a: { b: 123 } }])).toThrow( `param[1].a.b should be string, but got 123` ); }); it('minItems', () => { expect.hasAssertions(); const schema = t.array(t.string()).minItems(3); expect(schema.validate([])).toBe(false); expect(schema.validate(['str'])).toBe(false); expect(schema.validate(['str', 'str', 'str'])).toBe(true); expect(schema.validate(['str', 'str', 'str', 'str'])).toBe(true); }); it('maxItems', () => { expect.hasAssertions(); const schema = t.array(t.string()).maxItems(13); expect(schema.validate([])).toBe(true); expect(schema.validate(['str'])).toBe(true); expect(schema.validate(Array(13).fill('str'))).toBe(true); expect(schema.validate(Array(14).fill('str'))).toBe(false); }); it('optional', () => { expect.hasAssertions(); const schema = t.array(t.string()).optional(); expect(schema.validate(undefined)).toBe(true); expect(() => { typeAsserts(schema, undefined); }).not.toThrow(); }); it('item default', () => { expect.hasAssertions(); const schema = t.array(t.string().default('default-string')); const data: unknown[] = [undefined, undefined, 'str']; schema.validate(data); expect(data).toStrictEqual(['default-string', 'default-string', 'str']); }); it('item fallback', () => { expect.hasAssertions(); const schema = t.array(t.string().fallback('fallback-string')); const data: unknown[] = [1, 1, 'str']; expect(schema.validate(data)).toBe(true); expect(data).toStrictEqual(['fallback-string', 'fallback-string', 'str']); }); it('uniqueItems', () => { expect.hasAssertions(); const schema = t.array(t.string()).uniqueItems(); const data: unknown = ['1', '2', '2', '1', '1']; expect(schema.validate(data)).toBe(false); expect(() => { typeAsserts(schema, data); }).toThrow(`param should NOT have duplicate items (param[1] and param[2] are identical) param should NOT have duplicate items (param[0] and param[3] are identical) param should NOT have duplicate items (param[0] and param[4] are identical)`); const data2: unknown = ['1', '2']; expect(() => typeAsserts(schema, data2)).not.toThrow(); expect(schema.validate(data2)).toBe(true); }); it('length', () => { expect.hasAssertions(); const schema = t.array().length(2); expect(schema.validate(['ss', 'xx'])).toBe(true); expect(schema.validate(['ss'])).toBe(false); expect(schema.validate(['ss', 'xx', 'yy'])).toBe(false); expect(schema.validate([])).toBe(false); expect(() => typeAsserts(schema, [])).toThrow(`the length of param should be equal to 2`); expect(() => typeAsserts(schema, [1, 2, 3])).toThrow(`the length of param should be equal to 2`); }); });