2023-01-12 16:47:06 +00:00
|
|
|
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
|
|
|
|
) {
|
2023-01-12 16:47:06 +00:00
|
|
|
super(form_verficiation_error_message(type, field));
|
2022-01-02 22:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 19:01:12 +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);
|
2022-04-13 19:01:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-12 16:47:06 +00:00
|
|
|
export function apply_void(data: any) { }
|