/* eslint-disable no-empty-function */ import { t, typeAsserts } from '../../index'; describe('string', () => { it('string', () => { expect.hasAssertions(); const schema = t.string(); expect(schema.validate('str')).toBe(true); expect(() => typeAsserts(schema, 'str')).not.toThrow(); expect(schema.validate(1)).toBe(false); expect(() => { typeAsserts(schema, 1); }).toThrow('param should be string, but got 1'); expect(schema.validate(Symbol.for('1'))).toBe(false); expect(() => { typeAsserts(schema, Symbol.for('1')); }).toThrow('param should be string, but got a(n) symbol'); expect(schema.validate(null)).toBe(false); expect(() => { typeAsserts(schema, null); }).toThrow('param should be string, but got null'); expect(schema.validate(undefined)).toBe(false); expect(() => { typeAsserts(schema, undefined); }).toThrow('param should be string, but got undefined'); expect(schema.validate({})).toBe(false); expect(() => { typeAsserts(schema, {}); }).toThrow('param should be string, but got a(n) object'); expect(schema.validate([])).toBe(false); expect(() => { typeAsserts(schema, []); }).toThrow('param should be string, but got a(n) array'); expect(schema.validate(() => {})).toBe(false); expect(() => { typeAsserts(schema, () => {}); }).toThrow('param should be string, but got a(n) function'); expect(schema.validate(BigInt(1))).toBe(false); expect(() => { typeAsserts(schema, BigInt(1)); }).toThrow('param should be string, but got a(n) bigint'); }); it('optional string', () => { expect.hasAssertions(); const schema = t.string().optional(); expect(schema.validate('str')).toBe(true); expect(schema.validate(undefined)).toBe(true); expect(() => typeAsserts(schema, 'str')).not.toThrow(); expect(() => typeAsserts(schema, undefined)).not.toThrow(); expect(schema.validate(1)).toBe(false); expect(() => { typeAsserts(schema, 1); }).toThrow('param should be string or undefined, but got 1'); expect(schema.validate(Symbol(1))).toBe(false); expect(() => { typeAsserts(schema, Symbol(1)); }).toThrow('param should be string or undefined, but got a(n) symbol'); expect(schema.validate(null)).toBe(false); expect(() => { typeAsserts(schema, null); }).toThrow('param should be string or undefined, but got null'); expect(schema.validate({})).toBe(false); expect(() => { typeAsserts(schema, {}); }).toThrow('param should be string or undefined, but got a(n) object'); expect(schema.validate(() => {})).toBe(false); expect(() => { typeAsserts(schema, () => {}); }).toThrow('param should be string or undefined, but got a(n) function'); expect(schema.validate(BigInt(1))).toBe(false); expect(() => { typeAsserts(schema, BigInt(1)); }).toThrow('param should be string or undefined, but got a(n) bigint'); }); describe('string pattern', () => { it('string params', () => { expect.hasAssertions(); const abc = t.string().pattern('abc'); expect(abc.validate('abc')).toBe(true); expect(abc.validate('abd')).toBe(false); const wss = t.string().pattern('^wss?://'); expect(wss.validate('wss://echo.socket.com/abc')).toBe(true); expect(wss.validate('ws://echo.socket.com/abc')).toBe(true); expect(wss.validate('wws://echo.socket.com/abc')).toBe(false); expect(wss.validate('wss:echo.socket.com/abc')).toBe(false); expect(wss.validate('http://echo.socket.com/abc')).toBe(false); expect(wss.validate('https://echo.socket.com/abc')).toBe(false); expect(() => { typeAsserts(wss, 'xxx'); }).toThrow(`param should match pattern "^wss?://"`); }); it('string params2', () => { expect.hasAssertions(); const abc = t.string().pattern(/abc/); expect(abc.validate('abc')).toBe(true); expect(abc.validate('abd')).toBe(false); const wss = t.string().pattern(/^wss?:\/\//); expect(wss.validate('wss://echo.socket.com/abc')).toBe(true); expect(wss.validate('ws://echo.socket.com/abc')).toBe(true); expect(wss.validate('wws://echo.socket.com/abc')).toBe(false); expect(wss.validate('wss:echo.socket.com/abc')).toBe(false); expect(wss.validate('http://echo.socket.com/abc')).toBe(false); expect(wss.validate('https://echo.socket.com/abc')).toBe(false); expect(() => { typeAsserts(wss, 'xxx'); }).toThrow(`param should match pattern "/^wss?:\\/\\//"`); }); }); it('minLength', () => { expect.hasAssertions(); const schema = t.string().minLength(3); expect(schema.validate('123')).toBe(true); expect(() => { typeAsserts(schema, '123'); }).not.toThrow(); expect(schema.validate('1234')).toBe(true); expect(() => { typeAsserts(schema, '1234'); }).not.toThrow(); expect(schema.validate('')).toBe(false); expect(() => { typeAsserts(schema, ''); }).toThrow('the length of param should be greater than or equal to 3'); expect(schema.validate('12')).toBe(false); expect(() => { typeAsserts(schema, '12'); }).toThrow('the length of param should be greater than or equal to 3'); }); it('maxLength', () => { expect.hasAssertions(); const schema = t.string().maxLength(3); expect(schema.validate('')).toBe(true); expect(() => { typeAsserts(schema, ''); }).not.toThrow(); expect(schema.validate('12')).toBe(true); expect(() => { typeAsserts(schema, '12'); }).not.toThrow(); expect(schema.validate('123')).toBe(true); expect(() => { typeAsserts(schema, '123'); }).not.toThrow(); expect(schema.validate('1234')).toBe(false); expect(() => { typeAsserts(schema, '1234'); }).toThrow('the length of param should be less than or equal to 3'); expect(schema.validate('12345')).toBe(false); expect(() => { typeAsserts(schema, '12345'); }).toThrow('the length of param should be less than or equal to 3'); }); });