fiberx-backend-toolkit 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/types/main.d.ts +1 -0
- package/dist/types/main.js +1 -0
- package/dist/types/validator_type.d.ts +15 -0
- package/dist/types/validator_type.js +2 -0
- package/dist/validators/main.d.ts +2 -0
- package/dist/validators/main.js +8 -0
- package/dist/validators/query_validator_util.d.ts +6 -0
- package/dist/validators/query_validator_util.js +110 -0
- package/package.json +1 -1
package/dist/types/main.d.ts
CHANGED
package/dist/types/main.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type QueryValueType = "string" | "number" | "boolean" | "enum" | "date" | "date-range" | "array" | "object";
|
|
2
|
+
export interface QueryFieldSchemaInterface<T = any> {
|
|
3
|
+
key: string;
|
|
4
|
+
type: QueryValueType;
|
|
5
|
+
required?: boolean;
|
|
6
|
+
default?: T;
|
|
7
|
+
enum_values?: readonly any[];
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface QueryValidationResultInterface<T> {
|
|
12
|
+
v_state: boolean;
|
|
13
|
+
v_msg: string;
|
|
14
|
+
v_data?: T;
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.QueryValidatorUtil = void 0;
|
|
7
|
+
const query_validator_util_1 = __importDefault(require("./query_validator_util"));
|
|
8
|
+
exports.QueryValidatorUtil = query_validator_util_1.default;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { QueryFieldSchemaInterface, QueryValidationResultInterface } from "../types/validator_type";
|
|
2
|
+
declare class QueryValidatorUtil {
|
|
3
|
+
private constructor();
|
|
4
|
+
static validate<T extends Record<string, any>>(query: Record<string, any>, schema: QueryFieldSchemaInterface[]): QueryValidationResultInterface<T>;
|
|
5
|
+
}
|
|
6
|
+
export default QueryValidatorUtil;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
7
|
+
class QueryValidatorUtil {
|
|
8
|
+
constructor() { }
|
|
9
|
+
static validate(query, schema) {
|
|
10
|
+
const result = {};
|
|
11
|
+
for (const field of schema) {
|
|
12
|
+
const raw_value = query[field.key];
|
|
13
|
+
// Handle missing value
|
|
14
|
+
if (raw_value === undefined || raw_value === null || raw_value === "") {
|
|
15
|
+
if (field.required) {
|
|
16
|
+
return {
|
|
17
|
+
v_state: false,
|
|
18
|
+
v_msg: `missing_required_query_${field.key}`
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
result[field.key] = field.default ?? null;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
switch (field.type) {
|
|
25
|
+
case "string": {
|
|
26
|
+
result[field.key] = String(raw_value);
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
case "number": {
|
|
30
|
+
const num = Number(raw_value);
|
|
31
|
+
if (isNaN(num)) {
|
|
32
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
33
|
+
}
|
|
34
|
+
if (field.min !== undefined && num < field.min) {
|
|
35
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
36
|
+
}
|
|
37
|
+
if (field.max !== undefined && num > field.max) {
|
|
38
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
39
|
+
}
|
|
40
|
+
result[field.key] = num;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case "boolean": {
|
|
44
|
+
const val = String(raw_value).toLowerCase();
|
|
45
|
+
if (!["true", "false"].includes(val)) {
|
|
46
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
47
|
+
}
|
|
48
|
+
result[field.key] = val === "true";
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
case "enum": {
|
|
52
|
+
if (!field.enum_values?.includes(raw_value)) {
|
|
53
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
54
|
+
}
|
|
55
|
+
result[field.key] = raw_value;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case "date": {
|
|
59
|
+
const parsed = (0, dayjs_1.default)(raw_value);
|
|
60
|
+
if (!parsed.isValid()) {
|
|
61
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
62
|
+
}
|
|
63
|
+
// Return as native Date (recommended for DB usage)
|
|
64
|
+
result[field.key] = parsed.toDate();
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case "date-range": {
|
|
68
|
+
const parts = String(raw_value).split(",");
|
|
69
|
+
if (parts.length !== 2) {
|
|
70
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
71
|
+
}
|
|
72
|
+
const start = (0, dayjs_1.default)(parts[0]);
|
|
73
|
+
const end = (0, dayjs_1.default)(parts[1]);
|
|
74
|
+
if (!start.isValid() || !end.isValid()) {
|
|
75
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
76
|
+
}
|
|
77
|
+
// ✅ Ensure end is after start
|
|
78
|
+
if (!end.isAfter(start)) {
|
|
79
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}_range` };
|
|
80
|
+
}
|
|
81
|
+
result[field.key] = {
|
|
82
|
+
start_date: start.toDate(),
|
|
83
|
+
end_date: end.toDate()
|
|
84
|
+
};
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case "array": {
|
|
88
|
+
if (!Array.isArray(raw_value)) {
|
|
89
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
90
|
+
}
|
|
91
|
+
result[field.key] = raw_value;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case "object": {
|
|
95
|
+
if (typeof raw_value !== "object") {
|
|
96
|
+
return { v_state: false, v_msg: `invalid_query_${field.key}` };
|
|
97
|
+
}
|
|
98
|
+
result[field.key] = raw_value;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
v_state: true,
|
|
105
|
+
v_msg: "valid_query_params",
|
|
106
|
+
v_data: result
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.default = QueryValidatorUtil;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fiberx-backend-toolkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "A TypeScript backend toolkit providing shared domain logic, infrastructure helpers, and utilities for FiberX server-side applications and services.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|