JsonRPC/templates/ts_base.ts

44 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-01-02 22:02:47 +00:00
export class VerificationError extends Error {
constructor(
public readonly type?: string,
public readonly field?: string,
public readonly value?: any
) {
super("Parameter verification failed!");
}
}
export function verify_number(data: any) {
if (typeof data !== "number") throw new VerificationError("number", undefined, data);
return data;
}
export function strip_number(data: any) {
return data;
}
export function verify_string(data: any) {
if (typeof data !== "string") throw new VerificationError("string", undefined, data);
return data;
}
export function strip_string(data: any) {
return data;
}
export function verify_boolean(data: any) {
if (typeof data !== "boolean") throw new VerificationError("boolean", undefined, data);
return data;
}
export function strip_boolean(data: any) {
return data;
}
//TODO: Should it return data? it is kinda undefined actually...
export function verify_void(data: any) {}
export function strip_void(data: any) {
return undefined;
}