@dnax/core 0.0.9 → 0.1.0
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/driver/mongo/rest.ts +33 -21
- package/index.ts +9 -2
- package/lib/schema.ts +41 -18
- package/package.json +2 -1
- package/types/index.ts +10 -1
package/driver/mongo/rest.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { Actions, Collection, Tenant } from "./../../types/index";
|
|
|
6
6
|
import { isArray, omit } from "radash";
|
|
7
7
|
import { ClientSession, MongoClient, ObjectId } from "mongodb";
|
|
8
8
|
import { contextError, fn, toJson } from "../../utils";
|
|
9
|
-
import
|
|
9
|
+
import v, { type Schema } from "joi";
|
|
10
10
|
import type { Context } from "hono";
|
|
11
11
|
|
|
12
12
|
import type {
|
|
@@ -79,35 +79,47 @@ class useRest {
|
|
|
79
79
|
): { valid: boolean; output: any; error: any } {
|
|
80
80
|
let col = getCollection(collection, this.#tenant_id);
|
|
81
81
|
let valid = true;
|
|
82
|
-
let output = data;
|
|
83
|
-
let
|
|
82
|
+
let output = { ...data };
|
|
83
|
+
let errorMxg;
|
|
84
84
|
/* let issue = err?.issues[0] || {};
|
|
85
85
|
let msg = `Field : ${issue?.path[0]?.key} - ${issue?.message}`;
|
|
86
86
|
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
87
|
+
|
|
88
|
+
if (col?.schema) {
|
|
89
|
+
let schema = col.schema as Schema;
|
|
90
|
+
|
|
91
|
+
if (options.partial) {
|
|
92
|
+
let partialSchema = schema.fork(
|
|
93
|
+
Object.keys(schema.describe().keys),
|
|
94
|
+
(sc) => {
|
|
95
|
+
return sc.optional();
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
const { error, value } = partialSchema.validate(data);
|
|
99
|
+
} else {
|
|
100
|
+
const { error, value } = schema.validate(data);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const { error, value } = schema.validate(data);
|
|
104
|
+
|
|
105
|
+
if (error) {
|
|
106
|
+
valid = false;
|
|
107
|
+
errorMxg = error?.message;
|
|
108
|
+
} else {
|
|
109
|
+
output = value;
|
|
110
|
+
data = {
|
|
111
|
+
...data,
|
|
112
|
+
...output,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
console.log(value);
|
|
101
116
|
}
|
|
102
|
-
} catch (err: any) {
|
|
103
|
-
error = err?.message;
|
|
104
|
-
valid = false;
|
|
105
117
|
}
|
|
106
118
|
|
|
107
119
|
return {
|
|
108
120
|
valid: valid,
|
|
109
121
|
output,
|
|
110
|
-
error:
|
|
122
|
+
error: errorMxg,
|
|
111
123
|
};
|
|
112
124
|
}
|
|
113
125
|
|
package/index.ts
CHANGED
|
@@ -2,10 +2,17 @@ import "@colors/colors";
|
|
|
2
2
|
import { MongoClient, useRest } from "./driver/mongo";
|
|
3
3
|
import { toBson } from "./driver/mongo/utils";
|
|
4
4
|
import { runApp } from "./app";
|
|
5
|
+
import Joi from "joi";
|
|
5
6
|
import define from "./define";
|
|
6
7
|
import moment from "moment";
|
|
7
8
|
import * as utils from "./utils";
|
|
8
|
-
|
|
9
|
+
|
|
9
10
|
import * as ai from "./ai";
|
|
11
|
+
/**
|
|
12
|
+
* v is internal data validation and based of Joi validation.
|
|
13
|
+
* Note : v is an alias of Joi object API .
|
|
14
|
+
* @see {@link https://joi.dev/api}
|
|
15
|
+
*/
|
|
16
|
+
const v = Joi;
|
|
10
17
|
|
|
11
|
-
export { runApp, define,
|
|
18
|
+
export { runApp, define, utils, useRest, ai, v };
|
package/lib/schema.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import type { Schema } from "hono";
|
|
1
2
|
import type { Collection } from "../types";
|
|
2
|
-
import
|
|
3
|
+
import v, { type AnySchema } from "joi";
|
|
3
4
|
function buildSchema(col: Collection) {
|
|
4
|
-
let propertySchema = {};
|
|
5
|
+
let propertySchema = {} as { [key: string]: AnySchema };
|
|
5
6
|
|
|
6
7
|
if (col?.media) {
|
|
7
|
-
propertySchema["_file"] = v.object(
|
|
8
|
+
propertySchema["_file"] = v.object();
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
col?.fields?.map((f) => {
|
|
11
12
|
if (f?.type == "array") {
|
|
12
|
-
propertySchema[f.name] = v.array(
|
|
13
|
+
propertySchema[f.name] = v.array();
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
if (f?.type == "uuid" && f?.random?.toNumber) {
|
|
@@ -31,17 +32,17 @@ function buildSchema(col: Collection) {
|
|
|
31
32
|
propertySchema[f.name] = v.number();
|
|
32
33
|
}
|
|
33
34
|
if (f.type == "integer") {
|
|
34
|
-
propertySchema[f.name] = v.
|
|
35
|
+
propertySchema[f.name] = v.number().integer();
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
if (f?.type == "boolean") {
|
|
38
39
|
propertySchema[f.name] = v.boolean();
|
|
39
40
|
}
|
|
40
41
|
if (f?.type == "json") {
|
|
41
|
-
propertySchema[f.name] = v.object(
|
|
42
|
+
propertySchema[f.name] = v.object();
|
|
42
43
|
}
|
|
43
44
|
if (f?.type == "email") {
|
|
44
|
-
propertySchema[f.name] = v.
|
|
45
|
+
propertySchema[f.name] = v.string().email();
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
if (f?.type == "relationship" && f.relationType == "ref-to-one") {
|
|
@@ -49,7 +50,7 @@ function buildSchema(col: Collection) {
|
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
if (f?.type == "relationship" && f.relationType == "ref-to-many") {
|
|
52
|
-
propertySchema[f.name] = v.array(
|
|
53
|
+
propertySchema[f.name] = v.array();
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
if (f?.type?.match(/(date)/)) {
|
|
@@ -61,20 +62,26 @@ function buildSchema(col: Collection) {
|
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
if (f?.type == "url") {
|
|
64
|
-
propertySchema[f.name] = v.
|
|
65
|
+
propertySchema[f.name] = v.string().uri();
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
if (f?.type == "ipv4") {
|
|
68
|
-
propertySchema[f.name] = v.
|
|
69
|
+
propertySchema[f.name] = v.string().ip({
|
|
70
|
+
version: ["ipv4"],
|
|
71
|
+
});
|
|
69
72
|
}
|
|
70
73
|
if (f?.type == "ipv6") {
|
|
71
|
-
propertySchema[f.name] = v.
|
|
74
|
+
propertySchema[f.name] = v.string().ip({
|
|
75
|
+
version: ["ipv6"],
|
|
76
|
+
});
|
|
72
77
|
}
|
|
73
78
|
|
|
74
79
|
if (f?.type == "geojson") {
|
|
75
80
|
propertySchema[f.name] = v.object({
|
|
76
|
-
type: v
|
|
77
|
-
|
|
81
|
+
type: v
|
|
82
|
+
.string()
|
|
83
|
+
.valid(["Point", "LineString", "Polygon", "MultiPoint"]),
|
|
84
|
+
coordinats: v.array().items(v.number()),
|
|
78
85
|
});
|
|
79
86
|
}
|
|
80
87
|
|
|
@@ -99,18 +106,34 @@ function buildSchema(col: Collection) {
|
|
|
99
106
|
items = [...new Set(items)];
|
|
100
107
|
|
|
101
108
|
if (f?.enum?.multiple) {
|
|
102
|
-
propertySchema[f.name] = v.
|
|
109
|
+
propertySchema[f.name] = v.string().valid([...items]);
|
|
103
110
|
} else {
|
|
104
|
-
propertySchema[f.name] = v.
|
|
111
|
+
propertySchema[f.name] = v.string().valid([...items]);
|
|
105
112
|
}
|
|
106
113
|
}
|
|
107
114
|
|
|
108
|
-
if (f?.
|
|
109
|
-
propertySchema[f.name] =
|
|
115
|
+
if (f?.validate?.schema) {
|
|
116
|
+
propertySchema[f.name] = f.validate.schema;
|
|
117
|
+
}
|
|
118
|
+
if (f?.defaultValue) {
|
|
119
|
+
propertySchema[f.name] = propertySchema[f.name]?.default(f.defaultValue);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!f?.validate?.schema) {
|
|
123
|
+
if (f?.nullable) {
|
|
124
|
+
propertySchema[f.name] = propertySchema[f.name]?.allow([
|
|
125
|
+
null,
|
|
126
|
+
undefined,
|
|
127
|
+
]);
|
|
128
|
+
} else {
|
|
129
|
+
propertySchema[f.name] = propertySchema[f.name].required();
|
|
130
|
+
}
|
|
110
131
|
}
|
|
111
132
|
});
|
|
112
133
|
|
|
113
|
-
|
|
134
|
+
let buildSchema = v.object(propertySchema).unknown();
|
|
135
|
+
|
|
136
|
+
return buildSchema;
|
|
114
137
|
}
|
|
115
138
|
|
|
116
139
|
export { buildSchema };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dnax/core",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"module": "index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"devDependencies": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"generate-unique-id": "^2.0.3",
|
|
24
24
|
"hono": "^4.4.3",
|
|
25
25
|
"hono-sessions": "^0.5.8",
|
|
26
|
+
"joi": "^17.13.3",
|
|
26
27
|
"json-joy": "^16.8.0",
|
|
27
28
|
"jsonwebtoken": "^9.0.2",
|
|
28
29
|
"mingo": "^6.4.15",
|
package/types/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AnySchema } from "joi";
|
|
1
2
|
import { updateParams } from "./../driver/mongo/@types";
|
|
2
3
|
import * as v from "valibot";
|
|
3
4
|
import type { Db, MongoClient } from "mongodb";
|
|
@@ -100,9 +101,17 @@ export type Field = {
|
|
|
100
101
|
unique?: boolean;
|
|
101
102
|
index?: boolean | "text" | "2dsphere";
|
|
102
103
|
description?: string;
|
|
104
|
+
|
|
103
105
|
sparse?: boolean;
|
|
104
106
|
relationType?: "ref-to-one" | "ref-to-many";
|
|
105
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Overwrite custom validation schema
|
|
109
|
+
* use {v} from "@dnax/core"} to build your own validation
|
|
110
|
+
*
|
|
111
|
+
*/
|
|
112
|
+
validate?: {
|
|
113
|
+
schema?: AnySchema;
|
|
114
|
+
};
|
|
106
115
|
relationTo?: string;
|
|
107
116
|
};
|
|
108
117
|
|