@naturalcycles/db-lib 8.50.3 → 8.51.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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyObject, AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Saved, Unsaved } from '@naturalcycles/js-lib';
|
|
1
|
+
import { AnyObject, AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Saved, Unsaved, ZodSchema } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, ObjectSchemaTyped, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { DBDeleteByIdsOperation, DBModelType, DBOperation, DBPatch, DBSaveBatchOperation, RunQueryResult } from '../db.model';
|
|
4
4
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
|
|
@@ -148,7 +148,7 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
148
148
|
*
|
|
149
149
|
* Does NOT mutate the object.
|
|
150
150
|
*/
|
|
151
|
-
validateAndConvert<IN, OUT = IN>(obj: Partial<IN>, schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | undefined, modelType: DBModelType, opt?: CommonDaoOptions): OUT;
|
|
151
|
+
validateAndConvert<IN, OUT = IN>(obj: Partial<IN>, schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | ZodSchema<IN> | undefined, modelType: DBModelType, opt?: CommonDaoOptions): OUT;
|
|
152
152
|
getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
|
|
153
153
|
createTable(schema: JsonSchemaObject<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
|
|
154
154
|
/**
|
|
@@ -838,7 +838,13 @@ class CommonDao {
|
|
|
838
838
|
const objectName = table + (modelType || '');
|
|
839
839
|
let error;
|
|
840
840
|
let convertedValue;
|
|
841
|
-
if (schema instanceof
|
|
841
|
+
if (schema instanceof js_lib_1.ZodSchema) {
|
|
842
|
+
// Zod schema
|
|
843
|
+
const vr = (0, js_lib_1.zSafeValidate)(obj, schema);
|
|
844
|
+
error = vr.error;
|
|
845
|
+
convertedValue = vr.data;
|
|
846
|
+
}
|
|
847
|
+
else if (schema instanceof nodejs_lib_1.AjvSchema) {
|
|
842
848
|
// Ajv schema
|
|
843
849
|
convertedValue = obj; // because Ajv mutates original object
|
|
844
850
|
error = schema.getValidationError(obj, {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommonLogger, ErrorMode, ObjectWithId, Saved } from '@naturalcycles/js-lib';
|
|
1
|
+
import { CommonLogger, ErrorMode, ObjectWithId, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchemaTyped, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDB } from '../common.db';
|
|
4
4
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
@@ -23,7 +23,7 @@ export interface CommonDaoHooks<BM extends Partial<ObjectWithId<ID>>, DBM extend
|
|
|
23
23
|
* Return original `err` to pass the error through (will be thrown in CommonDao).
|
|
24
24
|
* Return modified/new `Error` if needed.
|
|
25
25
|
*/
|
|
26
|
-
onValidationError: (err: JoiValidationError | AjvValidationError) => Error | false;
|
|
26
|
+
onValidationError: (err: JoiValidationError | AjvValidationError | ZodError) => Error | false;
|
|
27
27
|
}
|
|
28
28
|
export declare enum CommonDaoLogLevel {
|
|
29
29
|
/**
|
|
@@ -47,11 +47,11 @@ export interface CommonDaoCfg<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
47
47
|
db: CommonDB;
|
|
48
48
|
table: string;
|
|
49
49
|
/**
|
|
50
|
-
* Joi or
|
|
50
|
+
* Joi, AjvSchema or ZodSchema is supported.
|
|
51
51
|
*/
|
|
52
|
-
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM>;
|
|
53
|
-
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>;
|
|
54
|
-
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>;
|
|
52
|
+
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM> | ZodSchema<DBM>;
|
|
53
|
+
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM> | ZodSchema<BM>;
|
|
54
|
+
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM> | ZodSchema<TM>;
|
|
55
55
|
excludeFromIndexes?: (keyof DBM)[];
|
|
56
56
|
/**
|
|
57
57
|
* Defaults to false.
|
package/package.json
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CommonLogger,
|
|
3
|
+
ErrorMode,
|
|
4
|
+
ObjectWithId,
|
|
5
|
+
Saved,
|
|
6
|
+
ZodError,
|
|
7
|
+
ZodSchema,
|
|
8
|
+
} from '@naturalcycles/js-lib'
|
|
2
9
|
import {
|
|
3
10
|
AjvSchema,
|
|
4
11
|
AjvValidationError,
|
|
@@ -37,7 +44,7 @@ export interface CommonDaoHooks<
|
|
|
37
44
|
* Return original `err` to pass the error through (will be thrown in CommonDao).
|
|
38
45
|
* Return modified/new `Error` if needed.
|
|
39
46
|
*/
|
|
40
|
-
onValidationError: (err: JoiValidationError | AjvValidationError) => Error | false
|
|
47
|
+
onValidationError: (err: JoiValidationError | AjvValidationError | ZodError) => Error | false
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
export enum CommonDaoLogLevel {
|
|
@@ -69,11 +76,11 @@ export interface CommonDaoCfg<
|
|
|
69
76
|
table: string
|
|
70
77
|
|
|
71
78
|
/**
|
|
72
|
-
* Joi or
|
|
79
|
+
* Joi, AjvSchema or ZodSchema is supported.
|
|
73
80
|
*/
|
|
74
|
-
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM>
|
|
75
|
-
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM>
|
|
76
|
-
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM>
|
|
81
|
+
dbmSchema?: ObjectSchemaTyped<DBM> | AjvSchema<DBM> | ZodSchema<DBM>
|
|
82
|
+
bmSchema?: ObjectSchemaTyped<BM> | AjvSchema<BM> | ZodSchema<BM>
|
|
83
|
+
tmSchema?: ObjectSchemaTyped<TM> | AjvSchema<TM> | ZodSchema<TM>
|
|
77
84
|
|
|
78
85
|
excludeFromIndexes?: (keyof DBM)[]
|
|
79
86
|
|
|
@@ -16,6 +16,9 @@ import {
|
|
|
16
16
|
pMap,
|
|
17
17
|
Saved,
|
|
18
18
|
Unsaved,
|
|
19
|
+
ZodSchema,
|
|
20
|
+
ZodValidationError,
|
|
21
|
+
zSafeValidate,
|
|
19
22
|
} from '@naturalcycles/js-lib'
|
|
20
23
|
import {
|
|
21
24
|
_pipeline,
|
|
@@ -1077,7 +1080,7 @@ export class CommonDao<
|
|
|
1077
1080
|
*/
|
|
1078
1081
|
validateAndConvert<IN, OUT = IN>(
|
|
1079
1082
|
obj: Partial<IN>,
|
|
1080
|
-
schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | undefined,
|
|
1083
|
+
schema: ObjectSchemaTyped<IN> | AjvSchema<IN> | ZodSchema<IN> | undefined,
|
|
1081
1084
|
modelType: DBModelType,
|
|
1082
1085
|
opt: CommonDaoOptions = {},
|
|
1083
1086
|
): OUT {
|
|
@@ -1113,10 +1116,15 @@ export class CommonDao<
|
|
|
1113
1116
|
const table = opt.table || this.cfg.table
|
|
1114
1117
|
const objectName = table + (modelType || '')
|
|
1115
1118
|
|
|
1116
|
-
let error: JoiValidationError | AjvValidationError | undefined
|
|
1119
|
+
let error: JoiValidationError | AjvValidationError | ZodValidationError<IN> | undefined
|
|
1117
1120
|
let convertedValue: any
|
|
1118
1121
|
|
|
1119
|
-
if (schema instanceof
|
|
1122
|
+
if (schema instanceof ZodSchema) {
|
|
1123
|
+
// Zod schema
|
|
1124
|
+
const vr = zSafeValidate(obj as IN, schema)
|
|
1125
|
+
error = vr.error
|
|
1126
|
+
convertedValue = vr.data
|
|
1127
|
+
} else if (schema instanceof AjvSchema) {
|
|
1120
1128
|
// Ajv schema
|
|
1121
1129
|
convertedValue = obj // because Ajv mutates original object
|
|
1122
1130
|
|