JsonRPC/templates/ts_base.ts
2023-11-30 00:13:10 +01:00

45 lines
1.1 KiB
TypeScript

function form_verficiation_error_message(type?: string, field?: string) {
let msg = "Parameter verification failed! ";
if (type && field) {
msg += `At ${type}.${field}! `;
} else if (type) {
msg += `At type ${type}! `;
} else if (field) {
msg += `At field ${field}! `;
}
return msg;
}
export class VerificationError extends Error {
constructor(
public readonly type?: string,
public readonly field?: string,
public readonly value?: any
) {
super(form_verficiation_error_message(type, field));
}
}
export function apply_int(data: any) {
data = Math.floor(Number(data));
if (Number.isNaN(data)) throw new VerificationError("int", undefined, data);
return data;
}
export function apply_float(data: any) {
data = Number(data);
if (Number.isNaN(data))
throw new VerificationError("float", 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) { }