JsonRPC/templates/ts_base.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

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;
}
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(form_verficiation_error_message(type, field));
2022-01-02 22:02:47 +00:00
}
}
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) {
2022-01-03 17:11:53 +00:00
data = Number(data);
if (Number.isNaN(data))
throw new VerificationError("float", undefined, data);
2022-01-02 22:02:47 +00:00
return data;
}
2022-01-03 17:11:53 +00:00
export function apply_string(data: any) {
return String(data);
2022-01-02 22:02:47 +00:00
}
2022-01-03 17:11:53 +00:00
export function apply_boolean(data: any) {
return Boolean(data);
2022-01-02 22:02:47 +00:00
}
export function apply_void(data: any) { }