JsonRPC/templates/ts_base.ts
2022-01-03 17:11:53 +00:00

26 lines
662 B
TypeScript

export class VerificationError extends Error {
constructor(
public readonly type?: string,
public readonly field?: string,
public readonly value?: any
) {
super("Parameter verification failed! " +(type ? "Expected " + type + "! " :"") + (field ? "At: " + field + "! " : ""));
}
}
export function apply_number(data: any) {
data = Number(data);
if(Number.isNaN(data)) throw new VerificationError("number", undefined, data);
return data;
}
export function apply_string(data: any) {
return String(data);
}
export function apply_boolean(data: any) {
return Boolean(data);
}
export function apply_void(data: any) {}