Files
trezor-suite/packages/schema-utils/tests/errors.test.ts
Tomáš Martykán 0c035c26a6 refactor(connect): validation in API methods
refactor(connect): validation in ethereumSignTypedData
refactor(connect): validation in `bitcoin/refTx`
refactor(connect): simple API methods
refactor(connect): additional device API methods
fix(connect): missing validation in `requestLogin` async
refactor(connect): additional ethereum API methods
refactor(connect): validation in `signMessage/verifyMessage` API
refactor(connect): validation in methods with bundled params
refactor(connect): validation in altcoin `GetAddress` APIs

refactor(connect): validation in `GetPublicKey` APIs

refactor(connect): validation in `authenticateDevice`, `eos`, `binance`, `ethereum` APIs

refactor(connect): validation in altcoin `signTransaction` APIs
refactor(connect): validation in `cardano` APIs
refactor(connect): validate with schema in authorizeCoinjoin API
fix(connect): fix types, re-enable assert in `typedCall`
fix(connect): minor lint and type issues
2024-01-05 15:36:39 +01:00

102 lines
3.7 KiB
TypeScript

import { Type, Assert } from '../src/index';
import { InvalidParameter } from '../src/errors';
describe('Assert', () => {
it('should not throw an error if matches schema', () => {
const schema = Type.Object({ type: Type.String() });
const value = { type: 'string' };
expect(() => Assert(schema, value)).not.toThrow();
});
it('acts as a type guard', () => {
const schema = Type.Object({ type: Type.String() });
const value = { type: 'string' };
Assert(schema, value);
// @ts-expect-error
value.type = 123;
});
it('should throw if type is mismateched', () => {
const schema = Type.Object({ type: Type.Number() });
const value = { type: 'string' };
expect(() => Assert(schema, value)).toThrow(InvalidParameter);
// Check error message
try {
Assert(schema, value);
} catch (e) {
expect(e.message).toEqual('Invalid parameter "type" (= "string"): Expected number');
}
});
it('should throw if required field is missing', () => {
const schema = Type.Object({ type: Type.Number() });
const value = {};
expect(() => Assert(schema, value)).toThrow(InvalidParameter);
// Check error message
try {
Assert(schema, value);
} catch (e) {
expect(e.message).toEqual('Invalid parameter "type" (= undefined): Required property');
}
});
it('should throw in case of custom types', () => {
const schema = Type.Object({ type: Type.Uint() });
const value = { type: 'xyz' };
expect(() => Assert(schema, value)).toThrow(InvalidParameter);
// Check error message
try {
Assert(schema, value);
} catch (e) {
expect(e.message).toEqual('Invalid parameter "type" (= "xyz"): Expected kind "Uint"');
}
});
it('should throw when conditions are not met', () => {
const schema = Type.Object({ type: Type.String({ minLength: 3 }) });
const value = { type: 'A' };
expect(() => Assert(schema, value)).toThrow(InvalidParameter);
// Check error message
try {
Assert(schema, value);
} catch (e) {
expect(e.message).toEqual(
'Invalid parameter "type" (= "A"): Expected string length greater or equal to 3',
);
}
});
it('should throw in case with unions of objects', () => {
const schema = Type.Union([
Type.Object({ type: Type.String() }),
Type.Object({ something: Type.String() }),
]);
const value = { type: 'string' };
expect(() => Assert(schema, value)).not.toThrow();
const badValue = { type: 123 };
expect(() => Assert(schema, badValue)).toThrow(InvalidParameter);
});
it('should also accept null if optional', () => {
const schema = Type.Object({ type: Type.Optional(Type.String()) });
const value = { type: null };
expect(() => Assert(schema, value)).not.toThrow();
});
it('should also accept null if optional on nested object', () => {
const schema = Type.Object({ type: Type.Object({ deeper: Type.Optional(Type.String()) }) });
const value = { type: { deeper: null } };
expect(() => Assert(schema, value)).not.toThrow();
});
it('should also accept null if optional on union of objects', () => {
const schema = Type.Union([
Type.Object({ type: Type.Optional(Type.String()) }),
Type.Object({ something: Type.String() }),
]);
const value = { type: null };
expect(() => Assert(schema, value)).not.toThrow();
});
});