@kikiutils/mongoose 7.0.2 → 7.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/dist/_internals.d.ts +1 -3
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js.map +1 -1
- package/dist/assertions.d.ts +1 -3
- package/dist/assertions.d.ts.map +1 -1
- package/dist/assertions.js.map +1 -1
- package/dist/builders.d.ts +3 -5
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js.map +1 -1
- package/dist/constants.d.ts +1 -3
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js.map +1 -1
- package/dist/helpers.d.ts +1 -3
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/options.d.ts +1 -3
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js.map +1 -1
- package/dist/plugins/assertions.d.ts +1 -3
- package/dist/plugins/assertions.d.ts.map +1 -1
- package/dist/plugins/assertions.js.map +1 -1
- package/dist/plugins/normalize.d.ts +2 -4
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +4 -2
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/base.d.ts +1 -3
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.js +1 -1
- package/dist/schema-builders/base.js.map +1 -1
- package/dist/schema-builders/boolean.d.ts +2 -4
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/boolean.js.map +1 -1
- package/dist/schema-builders/date.d.ts +2 -4
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/date.js.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +18 -5
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +24 -1
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/number.d.ts +2 -4
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.js.map +1 -1
- package/dist/schema-builders/object-id.d.ts +2 -4
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/object-id.js.map +1 -1
- package/dist/schema-builders/ref.d.ts +2 -4
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.js.map +1 -1
- package/dist/schema-builders/string.d.ts +32 -6
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.js +13 -1
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/types/_internals.d.ts +1 -2
- package/dist/types/_internals.d.ts.map +1 -1
- package/dist/types/aggregate-paginate.d.ts +6 -7
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts +2 -4
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -12
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +2 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +9 -10
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +18 -21
- package/src/schema-builders/base.ts +2 -0
- package/src/schema-builders/decimal128.ts +81 -2
- package/src/schema-builders/string.ts +66 -3
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.0
|
|
4
|
+
"version": "7.1.0",
|
|
5
5
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
@@ -74,31 +74,28 @@
|
|
|
74
74
|
"test:watch": "vitest watch --coverage",
|
|
75
75
|
"typecheck": "tsc -b --noEmit"
|
|
76
76
|
},
|
|
77
|
+
"peerDependenciesMeta": {},
|
|
77
78
|
"dependencies": {
|
|
78
79
|
"decimal.js": "^10.6.0",
|
|
79
|
-
"es-toolkit": "^1.
|
|
80
|
-
"mongoose": "^9.
|
|
81
|
-
"mongoose-aggregate-paginate-v2": "^1.1.
|
|
82
|
-
"mongoose-paginate-v2": "^1.9.
|
|
83
|
-
"type-fest": "^5.
|
|
80
|
+
"es-toolkit": "^1.52.0",
|
|
81
|
+
"mongoose": "^9.10.2",
|
|
82
|
+
"mongoose-aggregate-paginate-v2": "^1.1.5",
|
|
83
|
+
"mongoose-paginate-v2": "^1.9.5",
|
|
84
|
+
"type-fest": "^5.10.0"
|
|
84
85
|
},
|
|
85
86
|
"devDependencies": {
|
|
86
|
-
"@antfu/eslint-config": "^
|
|
87
|
-
"@kikiutils/eslint-config": "^9.1
|
|
88
|
-
"@kikiutils/tsconfigs": "^
|
|
89
|
-
"@types/node": "^
|
|
90
|
-
"@vitest/coverage-v8": "^
|
|
87
|
+
"@antfu/eslint-config": "^9.5.1",
|
|
88
|
+
"@kikiutils/eslint-config": "^9.2.1",
|
|
89
|
+
"@kikiutils/tsconfigs": "^7.0.0",
|
|
90
|
+
"@types/node": "^26.6.3",
|
|
91
|
+
"@vitest/coverage-v8": "^5.0.2",
|
|
91
92
|
"changelogen": "^0.6.2",
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"
|
|
93
|
+
"eslint": "^10.11.0",
|
|
94
|
+
"knip": "^6.38.0",
|
|
95
|
+
"publint": "^0.3.24",
|
|
96
|
+
"tsdown": "^0.23.0",
|
|
95
97
|
"typescript": "^6.0.3",
|
|
96
|
-
"unplugin-unused": "^0.
|
|
97
|
-
"vitest": "^
|
|
98
|
-
},
|
|
99
|
-
"pnpm": {
|
|
100
|
-
"onlyBuiltDependencies": [
|
|
101
|
-
"esbuild"
|
|
102
|
-
]
|
|
98
|
+
"unplugin-unused": "^0.6.0",
|
|
99
|
+
"vitest": "^5.0.2"
|
|
103
100
|
}
|
|
104
101
|
}
|
|
@@ -7,6 +7,7 @@ type BaseSchemaType =
|
|
|
7
7
|
| Schema.Types.ObjectId['constructor']
|
|
8
8
|
| StringConstructor;
|
|
9
9
|
|
|
10
|
+
// Constants/Variables
|
|
10
11
|
const isFunctionKeys = new Set([
|
|
11
12
|
'default',
|
|
12
13
|
'enum',
|
|
@@ -17,6 +18,7 @@ const isFunctionKeys = new Set([
|
|
|
17
18
|
'minlength',
|
|
18
19
|
]);
|
|
19
20
|
|
|
21
|
+
// Functions
|
|
20
22
|
export function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BaseSchemaType) {
|
|
21
23
|
return (schema: Record<string, any> = {}) => {
|
|
22
24
|
schema.type = type;
|
|
@@ -12,6 +12,7 @@ import type { Readonlyable } from '../types/_internals';
|
|
|
12
12
|
|
|
13
13
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
14
14
|
|
|
15
|
+
type Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };
|
|
15
16
|
type ExtendSchemaBuilder<
|
|
16
17
|
Props extends BaseProps,
|
|
17
18
|
ExtraOmitFields extends string,
|
|
@@ -24,6 +25,14 @@ interface BaseProps {
|
|
|
24
25
|
type: typeof Schema.Types.Decimal128;
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
interface Decimal128LimitValidator {
|
|
29
|
+
max?: string;
|
|
30
|
+
message: string;
|
|
31
|
+
min?: string;
|
|
32
|
+
type: 'max' | 'min';
|
|
33
|
+
validator: (value: null | Types.Decimal128 | undefined) => boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
27
36
|
export interface Decimal128SchemaBuilder<
|
|
28
37
|
Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },
|
|
29
38
|
ExtraOmitFields extends string = never,
|
|
@@ -54,6 +63,24 @@ export interface Decimal128SchemaBuilder<
|
|
|
54
63
|
ExtraOmitFields
|
|
55
64
|
>;
|
|
56
65
|
|
|
66
|
+
max: <
|
|
67
|
+
T extends D | Readonlyable<[D, S]>,
|
|
68
|
+
D extends Decimal128Limit,
|
|
69
|
+
S extends string,
|
|
70
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
71
|
+
Merge<Props, Decimal128ValidationSchema>,
|
|
72
|
+
'max' | ExtraOmitFields
|
|
73
|
+
>;
|
|
74
|
+
|
|
75
|
+
min: <
|
|
76
|
+
T extends D | Readonlyable<[D, S]>,
|
|
77
|
+
D extends Decimal128Limit,
|
|
78
|
+
S extends string,
|
|
79
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
80
|
+
Merge<Props, Decimal128ValidationSchema>,
|
|
81
|
+
'min' | ExtraOmitFields
|
|
82
|
+
>;
|
|
83
|
+
|
|
57
84
|
nonRequired: Props;
|
|
58
85
|
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
59
86
|
required: Merge<Props, { required: true }>;
|
|
@@ -87,16 +114,52 @@ export interface Decimal128SchemaBuilder<
|
|
|
87
114
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
88
115
|
}
|
|
89
116
|
|
|
117
|
+
interface Decimal128ValidationSchema {
|
|
118
|
+
validate: Decimal128LimitValidator[];
|
|
119
|
+
}
|
|
120
|
+
|
|
90
121
|
interface ToStringGetterSchema {
|
|
91
122
|
get: (value?: Types.Decimal128) => string | undefined;
|
|
92
123
|
}
|
|
93
124
|
|
|
94
125
|
interface ToStringSetterSchema {
|
|
95
|
-
set: (value?: { toString: () => string }) => string;
|
|
126
|
+
set: (value?: { toString: () => string }) => string | undefined;
|
|
96
127
|
}
|
|
97
128
|
|
|
129
|
+
// Constants/Variables
|
|
130
|
+
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
131
|
+
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
132
|
+
|
|
133
|
+
// Functions
|
|
98
134
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
|
|
99
135
|
|
|
136
|
+
function createLimitValidator(
|
|
137
|
+
type: 'max' | 'min',
|
|
138
|
+
value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>,
|
|
139
|
+
): Decimal128LimitValidator {
|
|
140
|
+
const [limit, message] = Array.isArray(value)
|
|
141
|
+
? value as Readonly<[Decimal128Limit, string]>
|
|
142
|
+
: [
|
|
143
|
+
value,
|
|
144
|
+
undefined,
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
const decimalLimit = new Decimal(limit.toString());
|
|
148
|
+
if (!decimalLimit.isFinite()) throw new RangeError(`Decimal128 ${type} limit must be finite`);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
152
|
+
[type]: decimalLimit.toString(),
|
|
153
|
+
type,
|
|
154
|
+
validator: (value) => {
|
|
155
|
+
if (value === null || value === undefined) return true;
|
|
156
|
+
|
|
157
|
+
const decimalValue = new Decimal(value.toString());
|
|
158
|
+
return type === 'min' ? decimalValue.gte(decimalLimit) : decimalValue.lte(decimalLimit);
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
100
163
|
export function decimal128SchemaBuilder() {
|
|
101
164
|
const schema: Record<string, any> = {};
|
|
102
165
|
const baseBuilder = baseBuilderFactory(schema);
|
|
@@ -104,10 +167,26 @@ export function decimal128SchemaBuilder() {
|
|
|
104
167
|
baseBuilder,
|
|
105
168
|
{
|
|
106
169
|
get(target, key, receiver) {
|
|
170
|
+
if (key === 'max' || key === 'min') {
|
|
171
|
+
return (value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>) => {
|
|
172
|
+
const validator = createLimitValidator(key, value);
|
|
173
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
174
|
+
schema.validate = [
|
|
175
|
+
...validators.filter(({ type }) => type !== key),
|
|
176
|
+
validator,
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
return receiver;
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
107
183
|
if (key === 'setRoundAndToFixedSetter') {
|
|
108
184
|
return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {
|
|
109
185
|
schema.set = (value?: { toString: () => string }) => {
|
|
110
|
-
if (value
|
|
186
|
+
if (value !== undefined && value !== null) {
|
|
187
|
+
const decimal = Decimal.isDecimal(value) ? value : new Decimal(value.toString());
|
|
188
|
+
return decimal.toFixed(places, rounding);
|
|
189
|
+
}
|
|
111
190
|
};
|
|
112
191
|
|
|
113
192
|
return receiver;
|
|
@@ -73,7 +73,7 @@ export interface StringSchemaBuilder<
|
|
|
73
73
|
*/
|
|
74
74
|
ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
75
75
|
Merge<Props, IpSchema<T>>,
|
|
76
|
-
'ipv4' | 'ipv6' | ExtraOmitFields
|
|
76
|
+
'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
|
|
77
77
|
>;
|
|
78
78
|
|
|
79
79
|
/**
|
|
@@ -87,7 +87,7 @@ export interface StringSchemaBuilder<
|
|
|
87
87
|
*/
|
|
88
88
|
ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
89
89
|
Merge<Props, IpSchema<T>>,
|
|
90
|
-
'ipv4' | 'ipv6' | ExtraOmitFields
|
|
90
|
+
'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
|
|
91
91
|
>;
|
|
92
92
|
|
|
93
93
|
/**
|
|
@@ -108,6 +108,24 @@ export interface StringSchemaBuilder<
|
|
|
108
108
|
>;
|
|
109
109
|
|
|
110
110
|
lowercase: ExtendSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Adds regular expression validation to the string schema.
|
|
114
|
+
* The validation message can be customized.
|
|
115
|
+
*
|
|
116
|
+
* @param regex - The regular expression that the string must match.
|
|
117
|
+
* @param message - Optional custom validation message.
|
|
118
|
+
*
|
|
119
|
+
* @returns A schema builder with the Mongoose `match` option configured.
|
|
120
|
+
*/
|
|
121
|
+
match: <
|
|
122
|
+
T extends RegExp,
|
|
123
|
+
M extends string | undefined = undefined,
|
|
124
|
+
>(regex: T,
|
|
125
|
+
message?: M) => ExtendSchemaBuilder<
|
|
126
|
+
Merge<Props, { match: M extends string ? Readonly<[T, M]> : T }>,
|
|
127
|
+
ExtraOmitFields
|
|
128
|
+
>;
|
|
111
129
|
maxlength: <
|
|
112
130
|
T extends L | Readonlyable<[L, S]>,
|
|
113
131
|
L extends number,
|
|
@@ -134,11 +152,32 @@ export interface StringSchemaBuilder<
|
|
|
134
152
|
trim: ExtendSchemaBuilder<Merge<Props, { trim: true }>, ExtraOmitFields>;
|
|
135
153
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
136
154
|
uppercase: ExtendSchemaBuilder<Merge<Props, { uppercase: true }>, ExtraOmitFields>;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Adds WHATWG URL validation to the string schema. Credentials and non-HTTP schemes are allowed.
|
|
158
|
+
* The input is parsed only; no DNS lookup or normalization is performed. The validation message can be customized.
|
|
159
|
+
*
|
|
160
|
+
* @param message - Custom validation error message. Defaults to the standard URL validation message.
|
|
161
|
+
*
|
|
162
|
+
* @returns A schema builder with URL validation enabled.
|
|
163
|
+
*/
|
|
164
|
+
url: <T extends string = typeof defaultUrlValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
165
|
+
Merge<Props, UrlSchema<T>>,
|
|
166
|
+
'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
|
|
167
|
+
>;
|
|
137
168
|
}
|
|
138
169
|
|
|
139
|
-
|
|
170
|
+
interface UrlSchema<T extends string> {
|
|
171
|
+
validate: { message: T; validator: (value: string) => boolean };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Constants/Variables
|
|
140
175
|
const defaultIpv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`';
|
|
141
176
|
const defaultIpv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`';
|
|
177
|
+
const defaultUrlValidateMessage = '`{VALUE}` is not a valid URL for path `{PATH}`';
|
|
178
|
+
|
|
179
|
+
// Functions
|
|
180
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
|
|
142
181
|
|
|
143
182
|
export function stringSchemaBuilder() {
|
|
144
183
|
const schema: Record<string, any> = {};
|
|
@@ -178,6 +217,30 @@ export function stringSchemaBuilder() {
|
|
|
178
217
|
};
|
|
179
218
|
}
|
|
180
219
|
|
|
220
|
+
if (key === 'match') {
|
|
221
|
+
return (regex: RegExp, message?: string) => {
|
|
222
|
+
schema.match = message === undefined
|
|
223
|
+
? regex
|
|
224
|
+
: [
|
|
225
|
+
regex,
|
|
226
|
+
message,
|
|
227
|
+
];
|
|
228
|
+
|
|
229
|
+
return receiver;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (key === 'url') {
|
|
234
|
+
return (message: string = defaultUrlValidateMessage) => {
|
|
235
|
+
schema.validate = {
|
|
236
|
+
message,
|
|
237
|
+
validator: (value: string) => URL.canParse(value),
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
return receiver;
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
181
244
|
return Reflect.get(target, key, receiver);
|
|
182
245
|
},
|
|
183
246
|
},
|