Finish implementation of typescript generator.
The implementation is still untested and will have issues
This commit is contained in:
@ -1,44 +1,87 @@
|
||||
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;
|
||||
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));
|
||||
}
|
||||
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;
|
||||
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;
|
||||
data = Number(data);
|
||||
if (Number.isNaN(data)) throw new VerificationError("float", undefined, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
export function apply_string(data: any) {
|
||||
return String(data);
|
||||
return String(data);
|
||||
}
|
||||
|
||||
export function apply_boolean(data: any) {
|
||||
return Boolean(data);
|
||||
return Boolean(data);
|
||||
}
|
||||
|
||||
export function apply_void(data: any) { }
|
||||
export function apply_map(
|
||||
data: any,
|
||||
apply_key: (data: any) => any,
|
||||
apply_value: (data: any) => any,
|
||||
) {
|
||||
if (typeof data !== "object")
|
||||
throw new VerificationError("map", undefined, data);
|
||||
|
||||
let res = {};
|
||||
for (const key in data) {
|
||||
let key_ = apply_key(key);
|
||||
let value_ = apply_value(data[key]);
|
||||
res[key_] = value_;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
export function apply_array(data: any, apply_value: (data: any) => any) {
|
||||
if (!Array.isArray(data))
|
||||
throw new VerificationError("array", undefined, data);
|
||||
return data.map((item) => apply_value(item));
|
||||
}
|
||||
|
||||
export function apply_required(
|
||||
data: any,
|
||||
apply_value: (data: any) => any,
|
||||
): any {
|
||||
if (typeof data === "undefined" || data === null) {
|
||||
throw new VerificationError("required", undefined, data);
|
||||
}
|
||||
return apply_value(data);
|
||||
}
|
||||
|
||||
export function apply_optional(
|
||||
data: any,
|
||||
apply_value: (data: any) => any,
|
||||
): any {
|
||||
if (typeof data === "undefined" || data === null) {
|
||||
return data;
|
||||
} else {
|
||||
return apply_value(data);
|
||||
}
|
||||
}
|
||||
|
||||
export function apply_void(data: any) {}
|
||||
|
Reference in New Issue
Block a user