@kikiutils/mongoose 7.0.3 → 8.0.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/README.md +8 -8
- package/dist/_internals.d.ts +5 -2
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +2 -1
- package/dist/_internals.js.map +1 -1
- package/dist/assertions.d.ts +1 -2
- package/dist/assertions.d.ts.map +1 -1
- package/dist/builders.d.ts +3 -4
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +2 -3
- package/dist/builders.js.map +1 -1
- package/dist/connection.d.ts +5 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +10 -0
- package/dist/connection.js.map +1 -0
- package/dist/options.d.ts +1 -2
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +2 -1
- package/dist/options.js.map +1 -1
- package/dist/plugins/assertions.d.ts +1 -2
- package/dist/plugins/assertions.d.ts.map +1 -1
- package/dist/plugins/normalize.d.ts +23 -17
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +19 -12
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/base.d.ts +2 -3
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.js.map +1 -1
- package/dist/schema-builders/bigint.d.ts +48 -0
- package/dist/schema-builders/bigint.d.ts.map +1 -0
- package/dist/schema-builders/bigint.js +36 -0
- package/dist/schema-builders/bigint.js.map +1 -0
- package/dist/schema-builders/boolean.d.ts +2 -3
- 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 -3
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +33 -15
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +23 -0
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +48 -0
- package/dist/schema-builders/double.d.ts.map +1 -0
- package/dist/schema-builders/double.js +37 -0
- package/dist/schema-builders/double.js.map +1 -0
- package/dist/schema-builders/index.d.ts +6 -1
- package/dist/schema-builders/index.js +6 -1
- package/dist/schema-builders/int32.d.ts +48 -0
- package/dist/schema-builders/int32.d.ts.map +1 -0
- package/dist/schema-builders/int32.js +36 -0
- package/dist/schema-builders/int32.js.map +1 -0
- package/dist/schema-builders/mixed.d.ts +34 -0
- package/dist/schema-builders/mixed.d.ts.map +1 -0
- package/dist/schema-builders/mixed.js +8 -0
- package/dist/schema-builders/mixed.js.map +1 -0
- package/dist/schema-builders/number.d.ts +2 -3
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/object-id.d.ts +2 -3
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/ref.d.ts +2 -3
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/string.d.ts +56 -19
- 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/schema-builders/uuid.d.ts +34 -0
- package/dist/schema-builders/uuid.d.ts.map +1 -0
- package/dist/schema-builders/uuid.js +8 -0
- package/dist/schema-builders/uuid.js.map +1 -0
- package/dist/types/_internals.d.ts +1 -2
- package/dist/types/_internals.d.ts.map +1 -1
- package/dist/types/aggregate-paginate.d.ts +30 -14
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts +2 -3
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -14
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +4 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +15 -11
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +23 -18
- package/src/_internals.ts +3 -0
- package/src/builders.ts +3 -10
- package/src/connection.ts +14 -0
- package/src/options.ts +8 -1
- package/src/plugins/normalize.ts +23 -15
- package/src/schema-builders/base.ts +8 -1
- package/src/schema-builders/bigint.ts +134 -0
- package/src/schema-builders/boolean.ts +1 -4
- package/src/schema-builders/decimal128.ts +92 -12
- package/src/schema-builders/double.ts +135 -0
- package/src/schema-builders/index.ts +15 -0
- package/src/schema-builders/int32.ts +135 -0
- package/src/schema-builders/mixed.ts +37 -0
- package/src/schema-builders/string.ts +90 -17
- package/src/schema-builders/uuid.ts +48 -0
- package/src/types/aggregate-paginate.ts +29 -8
- package/src/types/index.ts +0 -5
- package/src/types/options.ts +3 -0
- package/src/types/paginate.ts +6 -2
- package/dist/constants.d.ts +0 -6
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -6
- package/dist/constants.js.map +0 -1
- package/dist/helpers.d.ts +0 -6
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js +0 -9
- package/dist/helpers.js.map +0 -1
- package/src/constants.ts +0 -3
- package/src/helpers.ts +0 -5
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
IndexDirection,
|
|
4
|
+
IndexOptions,
|
|
5
|
+
Types,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import type { Readonlyable } from '../types/_internals';
|
|
10
|
+
|
|
11
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
|
+
|
|
13
|
+
type DoubleLimit = number | Types.Double;
|
|
14
|
+
type ExtendSchemaBuilder<
|
|
15
|
+
Props extends BaseProps,
|
|
16
|
+
ExtraOmitFields extends string,
|
|
17
|
+
> = Omit<
|
|
18
|
+
DoubleSchemaBuilder<Props, ExtraOmitFields>,
|
|
19
|
+
ExtraOmitFields | keyof Props
|
|
20
|
+
>;
|
|
21
|
+
|
|
22
|
+
interface BaseProps {
|
|
23
|
+
type: typeof Schema.Types.Double;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface DoubleLimitValidator {
|
|
27
|
+
max?: number;
|
|
28
|
+
message: string;
|
|
29
|
+
min?: number;
|
|
30
|
+
type: 'max' | 'min';
|
|
31
|
+
validator: (value: null | Types.Double | undefined) => boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
35
|
+
default: <
|
|
36
|
+
T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined,
|
|
37
|
+
D extends Types.Double,
|
|
38
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
39
|
+
Merge<Props, { default: T }>,
|
|
40
|
+
ExtraOmitFields
|
|
41
|
+
>;
|
|
42
|
+
|
|
43
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
44
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
45
|
+
Merge<Props, { index: T }>,
|
|
46
|
+
ExtraOmitFields
|
|
47
|
+
>;
|
|
48
|
+
|
|
49
|
+
max: <
|
|
50
|
+
T extends L | Readonlyable<[L, S]>,
|
|
51
|
+
L extends DoubleLimit,
|
|
52
|
+
S extends string,
|
|
53
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
54
|
+
Merge<Props, DoubleValidationSchema>,
|
|
55
|
+
'max' | ExtraOmitFields
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
min: <
|
|
59
|
+
T extends L | Readonlyable<[L, S]>,
|
|
60
|
+
L extends DoubleLimit,
|
|
61
|
+
S extends string,
|
|
62
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
63
|
+
Merge<Props, DoubleValidationSchema>,
|
|
64
|
+
'min' | ExtraOmitFields
|
|
65
|
+
>;
|
|
66
|
+
|
|
67
|
+
nonRequired: Props;
|
|
68
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
69
|
+
required: Merge<Props, { required: true }>;
|
|
70
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
71
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface DoubleValidationSchema {
|
|
75
|
+
validate: DoubleLimitValidator[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Constants/Variables
|
|
79
|
+
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
80
|
+
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
81
|
+
|
|
82
|
+
// Functions
|
|
83
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);
|
|
84
|
+
|
|
85
|
+
function createLimitValidator(
|
|
86
|
+
type: 'max' | 'min',
|
|
87
|
+
value: DoubleLimit | Readonlyable<[DoubleLimit, string]>,
|
|
88
|
+
): DoubleLimitValidator {
|
|
89
|
+
const [limit, message] = Array.isArray(value)
|
|
90
|
+
? value as Readonly<[DoubleLimit, string]>
|
|
91
|
+
: [
|
|
92
|
+
value as DoubleLimit,
|
|
93
|
+
undefined,
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
const numericLimit = typeof limit === 'number' ? limit : limit.valueOf();
|
|
97
|
+
if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
101
|
+
[type]: numericLimit,
|
|
102
|
+
type,
|
|
103
|
+
validator: (value) => {
|
|
104
|
+
if (value === null || value === undefined) return true;
|
|
105
|
+
|
|
106
|
+
return type === 'min' ? value.valueOf() >= numericLimit : value.valueOf() <= numericLimit;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function doubleSchemaBuilder() {
|
|
112
|
+
const schema: Record<string, any> = {};
|
|
113
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
114
|
+
return new Proxy(
|
|
115
|
+
baseBuilder,
|
|
116
|
+
{
|
|
117
|
+
get(target, key, receiver) {
|
|
118
|
+
if (key === 'max' || key === 'min') {
|
|
119
|
+
return (value: DoubleLimit | Readonlyable<[DoubleLimit, string]>) => {
|
|
120
|
+
const validator = createLimitValidator(key, value);
|
|
121
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
122
|
+
schema.validate = [
|
|
123
|
+
...validators.filter(({ type }) => type !== key),
|
|
124
|
+
validator,
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
return receiver;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Reflect.get(target, key, receiver);
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
) as DoubleSchemaBuilder;
|
|
135
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export { bigintSchemaBuilder as bigint } from './bigint';
|
|
2
|
+
export * from './bigint';
|
|
3
|
+
|
|
1
4
|
export { booleanSchemaBuilder as boolean } from './boolean';
|
|
2
5
|
export * from './boolean';
|
|
3
6
|
|
|
@@ -7,6 +10,15 @@ export * from './date';
|
|
|
7
10
|
export { decimal128SchemaBuilder as decimal128 } from './decimal128';
|
|
8
11
|
export * from './decimal128';
|
|
9
12
|
|
|
13
|
+
export { doubleSchemaBuilder as double } from './double';
|
|
14
|
+
export * from './double';
|
|
15
|
+
|
|
16
|
+
export { int32SchemaBuilder as int32 } from './int32';
|
|
17
|
+
export * from './int32';
|
|
18
|
+
|
|
19
|
+
export { mixedSchemaBuilder as mixed } from './mixed';
|
|
20
|
+
export * from './mixed';
|
|
21
|
+
|
|
10
22
|
export { numberSchemaBuilder as number } from './number';
|
|
11
23
|
export * from './number';
|
|
12
24
|
|
|
@@ -18,3 +30,6 @@ export * from './ref';
|
|
|
18
30
|
|
|
19
31
|
export { stringSchemaBuilder as string } from './string';
|
|
20
32
|
export * from './string';
|
|
33
|
+
|
|
34
|
+
export { uuidSchemaBuilder as uuid } from './uuid';
|
|
35
|
+
export * from './uuid';
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
DefaultType,
|
|
4
|
+
IndexDirection,
|
|
5
|
+
IndexOptions,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import type { Readonlyable } from '../types/_internals';
|
|
10
|
+
|
|
11
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
|
+
|
|
13
|
+
type ExtendSchemaBuilder<
|
|
14
|
+
Props extends BaseProps,
|
|
15
|
+
ExtraOmitFields extends string,
|
|
16
|
+
> = Omit<
|
|
17
|
+
Int32SchemaBuilder<Props, ExtraOmitFields>,
|
|
18
|
+
ExtraOmitFields | keyof Props
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
type Int32Limit = number;
|
|
22
|
+
|
|
23
|
+
interface BaseProps {
|
|
24
|
+
type: typeof Schema.Types.Int32;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface Int32LimitValidator {
|
|
28
|
+
max?: number;
|
|
29
|
+
message: string;
|
|
30
|
+
min?: number;
|
|
31
|
+
type: 'max' | 'min';
|
|
32
|
+
validator: (value: null | number | undefined) => boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
36
|
+
default: <
|
|
37
|
+
T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
|
|
38
|
+
D extends number,
|
|
39
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
40
|
+
Merge<Props, { default: T }>,
|
|
41
|
+
ExtraOmitFields
|
|
42
|
+
>;
|
|
43
|
+
|
|
44
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
45
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
46
|
+
Merge<Props, { index: T }>,
|
|
47
|
+
ExtraOmitFields
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
max: <
|
|
51
|
+
T extends L | Readonlyable<[L, S]>,
|
|
52
|
+
L extends Int32Limit,
|
|
53
|
+
S extends string,
|
|
54
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
55
|
+
Merge<Props, Int32ValidationSchema>,
|
|
56
|
+
'max' | ExtraOmitFields
|
|
57
|
+
>;
|
|
58
|
+
|
|
59
|
+
min: <
|
|
60
|
+
T extends L | Readonlyable<[L, S]>,
|
|
61
|
+
L extends Int32Limit,
|
|
62
|
+
S extends string,
|
|
63
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
64
|
+
Merge<Props, Int32ValidationSchema>,
|
|
65
|
+
'min' | ExtraOmitFields
|
|
66
|
+
>;
|
|
67
|
+
|
|
68
|
+
nonRequired: Props;
|
|
69
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
70
|
+
required: Merge<Props, { required: true }>;
|
|
71
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
72
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface Int32ValidationSchema {
|
|
76
|
+
validate: Int32LimitValidator[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Constants/Variables
|
|
80
|
+
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
81
|
+
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
82
|
+
|
|
83
|
+
// Functions
|
|
84
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);
|
|
85
|
+
|
|
86
|
+
function createLimitValidator(
|
|
87
|
+
type: 'max' | 'min',
|
|
88
|
+
value: Int32Limit | Readonlyable<[Int32Limit, string]>,
|
|
89
|
+
): Int32LimitValidator {
|
|
90
|
+
const [numericLimit, message] = Array.isArray(value)
|
|
91
|
+
? value as Readonly<[Int32Limit, string]>
|
|
92
|
+
: [
|
|
93
|
+
value as Int32Limit,
|
|
94
|
+
undefined,
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
101
|
+
[type]: numericLimit,
|
|
102
|
+
type,
|
|
103
|
+
validator: (value) => {
|
|
104
|
+
if (value === null || value === undefined) return true;
|
|
105
|
+
|
|
106
|
+
return type === 'min' ? value >= numericLimit : value <= numericLimit;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function int32SchemaBuilder() {
|
|
112
|
+
const schema: Record<string, any> = {};
|
|
113
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
114
|
+
return new Proxy(
|
|
115
|
+
baseBuilder,
|
|
116
|
+
{
|
|
117
|
+
get(target, key, receiver) {
|
|
118
|
+
if (key === 'max' || key === 'min') {
|
|
119
|
+
return (value: Int32Limit | Readonlyable<[Int32Limit, string]>) => {
|
|
120
|
+
const validator = createLimitValidator(key, value);
|
|
121
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
122
|
+
schema.validate = [
|
|
123
|
+
...validators.filter(({ type }) => type !== key),
|
|
124
|
+
validator,
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
return receiver;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Reflect.get(target, key, receiver);
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
) as Int32SchemaBuilder;
|
|
135
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
IndexDirection,
|
|
4
|
+
IndexOptions,
|
|
5
|
+
} from 'mongoose';
|
|
6
|
+
import type { Merge } from 'type-fest';
|
|
7
|
+
|
|
8
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
9
|
+
|
|
10
|
+
type ExtendSchemaBuilder<
|
|
11
|
+
Props extends BaseProps,
|
|
12
|
+
ExtraOmitFields extends string,
|
|
13
|
+
> = Omit<
|
|
14
|
+
MixedSchemaBuilder<Props, ExtraOmitFields>,
|
|
15
|
+
ExtraOmitFields | keyof Props
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
interface BaseProps {
|
|
19
|
+
type: typeof Schema.Types.Mixed;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MixedSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
23
|
+
default: <T>(value: T) => ExtendSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
|
|
24
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
25
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
26
|
+
Merge<Props, { index: T }>,
|
|
27
|
+
ExtraOmitFields
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
nonRequired: Props;
|
|
31
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
32
|
+
required: Merge<Props, { required: true }>;
|
|
33
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
34
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const mixedSchemaBuilder = createBaseSchemaBuilderFactory<MixedSchemaBuilder>(Schema.Types.Mixed);
|
|
@@ -63,40 +63,45 @@ export interface StringSchemaBuilder<
|
|
|
63
63
|
>;
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
* Adds IPv4 validation
|
|
67
|
-
* Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
|
|
66
|
+
* Adds IPv4 address validation and enables input trimming.
|
|
68
67
|
*
|
|
69
|
-
* @
|
|
70
|
-
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* Replaces the configured custom validator and sets `trim` to `true` on this builder.
|
|
71
70
|
*
|
|
72
|
-
* @
|
|
71
|
+
* @param message - The validation error message. Defaults to the built-in IPv4 validation message.
|
|
72
|
+
*
|
|
73
|
+
* @returns This instance for chaining.
|
|
73
74
|
*/
|
|
74
75
|
ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
75
76
|
Merge<Props, IpSchema<T>>,
|
|
76
|
-
'ipv4' | 'ipv6' | ExtraOmitFields
|
|
77
|
+
'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
|
|
77
78
|
>;
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
|
-
* Adds IPv6 validation
|
|
81
|
-
*
|
|
81
|
+
* Adds IPv6 address validation and enables input trimming.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* Replaces the configured custom validator and sets `trim` to `true` on this builder.
|
|
82
85
|
*
|
|
83
|
-
* @param message - The
|
|
84
|
-
* IPv6 validation message.
|
|
86
|
+
* @param message - The validation error message. Defaults to the built-in IPv6 validation message.
|
|
85
87
|
*
|
|
86
|
-
* @returns
|
|
88
|
+
* @returns This instance for chaining.
|
|
87
89
|
*/
|
|
88
90
|
ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
89
91
|
Merge<Props, IpSchema<T>>,
|
|
90
|
-
'ipv4' | 'ipv6' | ExtraOmitFields
|
|
92
|
+
'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
|
|
91
93
|
>;
|
|
92
94
|
|
|
93
95
|
/**
|
|
94
|
-
* Sets
|
|
95
|
-
*
|
|
96
|
+
* Sets equal minimum and maximum string lengths.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* Assigns the same value to `minlength` and `maxlength`, replacing any previously configured limits.
|
|
96
100
|
*
|
|
97
|
-
* @param value -
|
|
101
|
+
* @param value - The exact length in UTF-16 code units, or a tuple containing the length and a validation
|
|
102
|
+
* error message.
|
|
98
103
|
*
|
|
99
|
-
* @returns
|
|
104
|
+
* @returns This instance for chaining.
|
|
100
105
|
*/
|
|
101
106
|
length: <
|
|
102
107
|
T extends L | Readonlyable<[L, S]>,
|
|
@@ -108,6 +113,26 @@ export interface StringSchemaBuilder<
|
|
|
108
113
|
>;
|
|
109
114
|
|
|
110
115
|
lowercase: ExtendSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Configures regular expression validation for the string field.
|
|
119
|
+
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* Replaces any previously configured `match` option.
|
|
122
|
+
*
|
|
123
|
+
* @param regex - The regular expression used by Mongoose to validate the string.
|
|
124
|
+
* @param message - The validation error message. If omitted, Mongoose uses its default message.
|
|
125
|
+
*
|
|
126
|
+
* @returns This instance for chaining.
|
|
127
|
+
*/
|
|
128
|
+
match: <
|
|
129
|
+
T extends RegExp,
|
|
130
|
+
M extends string | undefined = undefined,
|
|
131
|
+
>(regex: T,
|
|
132
|
+
message?: M) => ExtendSchemaBuilder<
|
|
133
|
+
Merge<Props, { match: M extends string ? Readonly<[T, M]> : T }>,
|
|
134
|
+
ExtraOmitFields
|
|
135
|
+
>;
|
|
111
136
|
maxlength: <
|
|
112
137
|
T extends L | Readonlyable<[L, S]>,
|
|
113
138
|
L extends number,
|
|
@@ -134,11 +159,35 @@ export interface StringSchemaBuilder<
|
|
|
134
159
|
trim: ExtendSchemaBuilder<Merge<Props, { trim: true }>, ExtraOmitFields>;
|
|
135
160
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
136
161
|
uppercase: ExtendSchemaBuilder<Merge<Props, { uppercase: true }>, ExtraOmitFields>;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Adds WHATWG URL parseability validation.
|
|
165
|
+
*
|
|
166
|
+
* @remarks
|
|
167
|
+
* Accepts URLs with credentials and non-HTTP schemes. Performs no DNS lookup or stored-value normalization,
|
|
168
|
+
* and does not enable trimming. Replaces the configured custom validator on this builder.
|
|
169
|
+
*
|
|
170
|
+
* @param message - The validation error message. Defaults to the built-in URL validation message.
|
|
171
|
+
*
|
|
172
|
+
* @returns This instance for chaining.
|
|
173
|
+
*/
|
|
174
|
+
url: <T extends string = typeof defaultUrlValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
175
|
+
Merge<Props, UrlSchema<T>>,
|
|
176
|
+
'ipv4' | 'ipv6' | 'url' | ExtraOmitFields
|
|
177
|
+
>;
|
|
137
178
|
}
|
|
138
179
|
|
|
139
|
-
|
|
180
|
+
interface UrlSchema<T extends string> {
|
|
181
|
+
validate: { message: T; validator: (value: string) => boolean };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Constants/Variables
|
|
140
185
|
const defaultIpv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`';
|
|
141
186
|
const defaultIpv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`';
|
|
187
|
+
const defaultUrlValidateMessage = '`{VALUE}` is not a valid URL for path `{PATH}`';
|
|
188
|
+
|
|
189
|
+
// Functions
|
|
190
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
|
|
142
191
|
|
|
143
192
|
export function stringSchemaBuilder() {
|
|
144
193
|
const schema: Record<string, any> = {};
|
|
@@ -178,6 +227,30 @@ export function stringSchemaBuilder() {
|
|
|
178
227
|
};
|
|
179
228
|
}
|
|
180
229
|
|
|
230
|
+
if (key === 'match') {
|
|
231
|
+
return (regex: RegExp, message?: string) => {
|
|
232
|
+
schema.match = message === undefined
|
|
233
|
+
? regex
|
|
234
|
+
: [
|
|
235
|
+
regex,
|
|
236
|
+
message,
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
return receiver;
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (key === 'url') {
|
|
244
|
+
return (message: string = defaultUrlValidateMessage) => {
|
|
245
|
+
schema.validate = {
|
|
246
|
+
message,
|
|
247
|
+
validator: (value: string) => URL.canParse(value),
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
return receiver;
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
181
254
|
return Reflect.get(target, key, receiver);
|
|
182
255
|
},
|
|
183
256
|
},
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
IndexDirection,
|
|
4
|
+
IndexOptions,
|
|
5
|
+
Types,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
10
|
+
|
|
11
|
+
type ExtendSchemaBuilder<
|
|
12
|
+
Props extends BaseProps,
|
|
13
|
+
ExtraOmitFields extends string,
|
|
14
|
+
> = Omit<
|
|
15
|
+
UUIDSchemaBuilder<Props, ExtraOmitFields>,
|
|
16
|
+
ExtraOmitFields | keyof Props
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
interface BaseProps {
|
|
20
|
+
type: typeof Schema.Types.UUID;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface UUIDSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
24
|
+
default: <
|
|
25
|
+
T extends
|
|
26
|
+
| ((this: any, doc: any) => null | string | Types.UUID | undefined)
|
|
27
|
+
| null
|
|
28
|
+
| string
|
|
29
|
+
| Types.UUID
|
|
30
|
+
| undefined,
|
|
31
|
+
>(
|
|
32
|
+
value: T,
|
|
33
|
+
) => ExtendSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
|
|
34
|
+
|
|
35
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
36
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
37
|
+
Merge<Props, { index: T }>,
|
|
38
|
+
ExtraOmitFields
|
|
39
|
+
>;
|
|
40
|
+
|
|
41
|
+
nonRequired: Props;
|
|
42
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
43
|
+
required: Merge<Props, { required: true }>;
|
|
44
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
45
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const uuidSchemaBuilder = createBaseSchemaBuilderFactory<UUIDSchemaBuilder>(Schema.Types.UUID);
|
|
@@ -49,9 +49,13 @@ export interface AggregatePaginateOptions {
|
|
|
49
49
|
page?: number;
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
52
|
+
* Whether to apply pagination to the aggregation results.
|
|
53
53
|
*
|
|
54
|
-
* @
|
|
54
|
+
* @remarks
|
|
55
|
+
* If `false`, the pagination plugin adds no pagination `$skip` or `$limit` stages. Existing pipeline stages
|
|
56
|
+
* still apply.
|
|
57
|
+
*
|
|
58
|
+
* @defaultValue `true`, unless overridden by plugin-wide defaults.
|
|
55
59
|
*/
|
|
56
60
|
pagination?: boolean;
|
|
57
61
|
sort?: object | string;
|
|
@@ -59,17 +63,34 @@ export interface AggregatePaginateOptions {
|
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
export interface AggregatePaginateQueryPopulateOptions {
|
|
62
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* The query conditions used to filter populated documents.
|
|
68
|
+
*/
|
|
63
69
|
match?: any;
|
|
64
|
-
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The model or model name used to populate the referenced documents.
|
|
73
|
+
*/
|
|
65
74
|
model?: Model<any> | string;
|
|
66
|
-
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The query options applied when retrieving populated documents, such as sorting and limits.
|
|
78
|
+
*/
|
|
67
79
|
options?: any;
|
|
68
|
-
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The path or space-delimited paths to populate.
|
|
83
|
+
*/
|
|
69
84
|
path: string;
|
|
70
|
-
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The nested population options applied to the populated documents.
|
|
88
|
+
*/
|
|
71
89
|
populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[];
|
|
72
|
-
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The field projection used when retrieving populated documents.
|
|
93
|
+
*/
|
|
73
94
|
select?: any;
|
|
74
95
|
}
|
|
75
96
|
|
package/src/types/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
Connection,
|
|
3
2
|
HydratedDocument,
|
|
4
3
|
QueryWithHelpers,
|
|
5
4
|
Types,
|
|
@@ -55,7 +54,3 @@ export type SetFieldsToObjectId<T, K extends keyof T> = SetFieldType<T, K, Types
|
|
|
55
54
|
// @ts-expect-error Ignore this error.
|
|
56
55
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
57
56
|
export interface BaseMongooseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {}
|
|
58
|
-
|
|
59
|
-
export interface MongooseConnections {
|
|
60
|
-
default?: Connection;
|
|
61
|
-
}
|
package/src/types/options.ts
CHANGED
package/src/types/paginate.ts
CHANGED
|
@@ -91,9 +91,13 @@ export interface PaginateOptions {
|
|
|
91
91
|
page?: number;
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
*
|
|
94
|
+
* Whether to apply pagination to the query results.
|
|
95
95
|
*
|
|
96
|
-
* @
|
|
96
|
+
* @remarks
|
|
97
|
+
* If `false`, the pagination plugin does not apply its page-based `skip` or `limit`. Explicit query options
|
|
98
|
+
* and other query conditions still apply.
|
|
99
|
+
*
|
|
100
|
+
* @defaultValue `true`, unless overridden by plugin-wide defaults.
|
|
97
101
|
*/
|
|
98
102
|
pagination?: boolean;
|
|
99
103
|
populate?: PopulateOptions | PopulateOptions[] | string | string[];
|
package/dist/constants.d.ts
DELETED
package/dist/constants.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","names":[],"sources":["../src/constants.ts"],"mappings":";;cAEa,qBAAqB"}
|
package/dist/constants.js
DELETED
package/dist/constants.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","names":[],"sources":["../src/constants.ts"],"sourcesContent":["import type { MongooseConnections } from './types';\n\nexport const mongooseConnections: MongooseConnections = {};\n"],"mappings":";AAEA,MAAa,sBAA2C,CAAC"}
|
package/dist/helpers.d.ts
DELETED
package/dist/helpers.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","names":[],"sources":["../src/helpers.ts"],"mappings":";;iBAEgB,oBAAoB,aAAa,MAAM"}
|