@kikiutils/mongoose 9.0.1 → 9.0.3
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 +0 -2
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +1 -2
- package/dist/assertions.d.ts +0 -2
- package/dist/assertions.d.ts.map +1 -1
- package/dist/assertions.js +1 -2
- package/dist/assertions.js.map +1 -1
- package/dist/builders.d.ts +1 -3
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +1 -2
- package/dist/connection.d.ts +0 -2
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1 -2
- package/dist/options.d.ts +0 -2
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +1 -2
- package/dist/plugins/assertions.d.ts +0 -2
- package/dist/plugins/assertions.d.ts.map +1 -1
- package/dist/plugins/assertions.js +5 -6
- package/dist/plugins/assertions.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +6 -4
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.js +6 -6
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -1
- package/dist/plugins/normalize.d.ts +3 -3
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +3 -3
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/_internals.d.ts +18 -0
- package/dist/schema-builders/_internals.d.ts.map +1 -0
- package/dist/schema-builders/_internals.js +36 -0
- package/dist/schema-builders/_internals.js.map +1 -0
- package/dist/schema-builders/base.d.ts +1 -2
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.js +2 -3
- package/dist/schema-builders/base.js.map +1 -1
- package/dist/schema-builders/bigint.d.ts +6 -27
- package/dist/schema-builders/bigint.d.ts.map +1 -1
- package/dist/schema-builders/bigint.js +10 -21
- package/dist/schema-builders/bigint.js.map +1 -1
- package/dist/schema-builders/boolean.d.ts +1 -2
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/boolean.js +1 -2
- package/dist/schema-builders/date.d.ts +5 -11
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/date.js +1 -2
- package/dist/schema-builders/date.js.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +7 -26
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +5 -8
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +6 -27
- package/dist/schema-builders/double.d.ts.map +1 -1
- package/dist/schema-builders/double.js +10 -21
- package/dist/schema-builders/double.js.map +1 -1
- package/dist/schema-builders/int32.d.ts +6 -27
- package/dist/schema-builders/int32.d.ts.map +1 -1
- package/dist/schema-builders/int32.js +10 -21
- package/dist/schema-builders/int32.js.map +1 -1
- package/dist/schema-builders/mixed.d.ts +1 -2
- package/dist/schema-builders/mixed.d.ts.map +1 -1
- package/dist/schema-builders/mixed.js +1 -2
- package/dist/schema-builders/number.d.ts +5 -11
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.js +1 -2
- package/dist/schema-builders/number.js.map +1 -1
- package/dist/schema-builders/object-id.d.ts +3 -9
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/object-id.js +1 -2
- package/dist/schema-builders/object-id.js.map +1 -1
- package/dist/schema-builders/ref.d.ts +3 -9
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.js +1 -2
- package/dist/schema-builders/ref.js.map +1 -1
- package/dist/schema-builders/string.d.ts +6 -12
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.js +1 -2
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/schema-builders/uuid.d.ts +1 -2
- package/dist/schema-builders/uuid.d.ts.map +1 -1
- package/dist/schema-builders/uuid.js +1 -2
- package/dist/types/_internals.d.ts +22 -2
- package/dist/types/_internals.d.ts.map +1 -1
- package/dist/types/_internals.js +1 -0
- package/dist/types/aggregate-paginate.d.ts +2 -3
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/aggregate-paginate.js +1 -0
- package/dist/types/data.d.ts +3 -3
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/data.js +1 -0
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/options.d.ts +6 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/options.js +1 -0
- package/dist/types/paginate.d.ts +0 -2
- package/dist/types/paginate.d.ts.map +1 -1
- package/dist/types/paginate.js +1 -0
- package/package.json +40 -43
- package/src/assertions.ts +0 -1
- package/src/plugins/assertions.ts +4 -4
- package/src/plugins/lean-decimal128-to-string.ts +18 -7
- package/src/plugins/normalize.ts +5 -2
- package/src/schema-builders/_internals.ts +64 -0
- package/src/schema-builders/base.ts +1 -1
- package/src/schema-builders/bigint.ts +33 -67
- package/src/schema-builders/date.ts +7 -7
- package/src/schema-builders/decimal128.ts +25 -41
- package/src/schema-builders/double.ts +35 -67
- package/src/schema-builders/int32.ts +33 -67
- package/src/schema-builders/number.ts +7 -7
- package/src/schema-builders/object-id.ts +2 -5
- package/src/schema-builders/ref.ts +2 -5
- package/src/schema-builders/string.ts +8 -8
- package/src/types/_internals.ts +26 -0
- package/src/types/aggregate-paginate.ts +2 -1
- package/src/types/data.ts +3 -1
- package/src/types/options.ts +6 -2
|
@@ -6,11 +6,27 @@ import type {
|
|
|
6
6
|
} from 'mongoose';
|
|
7
7
|
import type { Merge } from 'type-fest';
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
|
|
9
|
+
import type {
|
|
10
|
+
EnumInput,
|
|
11
|
+
EnumValidator,
|
|
12
|
+
LimitValidator,
|
|
13
|
+
ValidationSchema,
|
|
14
|
+
ValueWithMessage,
|
|
15
|
+
} from '../types/_internals';
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
defaultEnumValidateMessage,
|
|
19
|
+
defaultLimitValidateMessages,
|
|
20
|
+
hasSchemaValidator,
|
|
21
|
+
normalizeEnumValues,
|
|
22
|
+
normalizeValueWithMessage,
|
|
23
|
+
setSchemaValidator,
|
|
24
|
+
} from './_internals';
|
|
11
25
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
26
|
|
|
13
27
|
type BigIntLimit = bigint;
|
|
28
|
+
type BigIntValidationSchema = ValidationSchema<EnumValidator<bigint> | LimitValidator<bigint>>;
|
|
29
|
+
|
|
14
30
|
type ExtendSchemaBuilder<
|
|
15
31
|
Props extends BaseProps,
|
|
16
32
|
ExtraOmitFields extends string,
|
|
@@ -23,21 +39,6 @@ interface BaseProps {
|
|
|
23
39
|
type: typeof Schema.Types.BigInt;
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
interface BigIntEnumValidator {
|
|
27
|
-
enumValues: (bigint | null)[];
|
|
28
|
-
message: string;
|
|
29
|
-
type: 'enum';
|
|
30
|
-
validator: (value: bigint | null | undefined) => boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface BigIntLimitValidator {
|
|
34
|
-
max?: bigint;
|
|
35
|
-
message: string;
|
|
36
|
-
min?: bigint;
|
|
37
|
-
type: 'max' | 'min';
|
|
38
|
-
validator: (value: bigint | null | undefined) => boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
42
|
export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
42
43
|
default: <
|
|
43
44
|
T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
|
|
@@ -48,10 +49,7 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
48
49
|
>;
|
|
49
50
|
|
|
50
51
|
enum: <
|
|
51
|
-
T extends
|
|
52
|
-
| Readonlyable<(N | null)[]>
|
|
53
|
-
| { [path: string]: N | null }
|
|
54
|
-
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
52
|
+
T extends EnumInput<N | null, M>,
|
|
55
53
|
M extends string,
|
|
56
54
|
N extends bigint,
|
|
57
55
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -66,7 +64,7 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
66
64
|
>;
|
|
67
65
|
|
|
68
66
|
max: <
|
|
69
|
-
T extends
|
|
67
|
+
T extends ValueWithMessage<L, S>,
|
|
70
68
|
L extends BigIntLimit,
|
|
71
69
|
S extends string,
|
|
72
70
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -75,7 +73,7 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
75
73
|
>;
|
|
76
74
|
|
|
77
75
|
min: <
|
|
78
|
-
T extends
|
|
76
|
+
T extends ValueWithMessage<L, S>,
|
|
79
77
|
L extends BigIntLimit,
|
|
80
78
|
S extends string,
|
|
81
79
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -90,69 +88,42 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
90
88
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
91
89
|
}
|
|
92
90
|
|
|
93
|
-
interface BigIntValidationSchema {
|
|
94
|
-
validate: (BigIntEnumValidator | BigIntLimitValidator)[];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Constants/Variables
|
|
98
|
-
const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
|
|
99
|
-
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
100
|
-
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
101
|
-
|
|
102
91
|
// Functions
|
|
103
92
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);
|
|
104
93
|
|
|
105
94
|
export function bigintSchemaBuilder() {
|
|
106
|
-
const schema: Record<string, any> = {};
|
|
95
|
+
const schema: Partial<BigIntValidationSchema> & Record<string, any> = {};
|
|
107
96
|
const baseBuilder = baseBuilderFactory(schema);
|
|
108
97
|
return new Proxy(
|
|
109
98
|
baseBuilder,
|
|
110
99
|
{
|
|
111
100
|
get(target, key, receiver) {
|
|
112
101
|
if (key === 'enum') {
|
|
113
|
-
if (schema
|
|
102
|
+
if (hasSchemaValidator(schema, 'enum')) {
|
|
114
103
|
throw new Error('Duplicate schema attribute: enum');
|
|
115
104
|
}
|
|
116
105
|
|
|
117
|
-
return (
|
|
118
|
-
|
|
119
|
-
| { [path: string]: bigint | null }
|
|
120
|
-
| { message?: string; values: Readonlyable<(bigint | null)[]> },
|
|
121
|
-
) => {
|
|
122
|
-
let enumValues: (bigint | null)[];
|
|
123
|
-
let message: string | undefined;
|
|
124
|
-
|
|
125
|
-
if (Array.isArray(value)) enumValues = [...value];
|
|
126
|
-
else if ('values' in value && Array.isArray(value.values)) {
|
|
127
|
-
enumValues = [...value.values];
|
|
128
|
-
if ('message' in value && typeof value.message === 'string') message = value.message;
|
|
129
|
-
} else enumValues = Object.values(value);
|
|
106
|
+
return (value: EnumInput<bigint | null>) => {
|
|
107
|
+
const { enumValues, message } = normalizeEnumValues(value);
|
|
130
108
|
|
|
131
109
|
const allowedValues = new Set(enumValues);
|
|
132
|
-
const validator:
|
|
110
|
+
const validator: EnumValidator<bigint> = {
|
|
133
111
|
enumValues,
|
|
134
112
|
message: message ?? defaultEnumValidateMessage,
|
|
135
113
|
type: 'enum',
|
|
136
114
|
validator: (value) => value === null || value === undefined || allowedValues.has(value),
|
|
137
115
|
};
|
|
138
116
|
|
|
139
|
-
schema
|
|
140
|
-
...schema.validate ?? [],
|
|
141
|
-
validator,
|
|
142
|
-
];
|
|
117
|
+
setSchemaValidator(schema, validator);
|
|
143
118
|
|
|
144
119
|
return receiver;
|
|
145
120
|
};
|
|
146
121
|
}
|
|
147
122
|
|
|
148
123
|
if (key === 'max' || key === 'min') {
|
|
149
|
-
return (value:
|
|
124
|
+
return (value: ValueWithMessage<BigIntLimit>) => {
|
|
150
125
|
const validator = createLimitValidator(key, value);
|
|
151
|
-
|
|
152
|
-
schema.validate = [
|
|
153
|
-
...validators.filter(({ type }) => type !== key),
|
|
154
|
-
validator,
|
|
155
|
-
];
|
|
126
|
+
setSchemaValidator(schema, validator);
|
|
156
127
|
|
|
157
128
|
return receiver;
|
|
158
129
|
};
|
|
@@ -166,19 +137,14 @@ export function bigintSchemaBuilder() {
|
|
|
166
137
|
|
|
167
138
|
function createLimitValidator(
|
|
168
139
|
type: 'max' | 'min',
|
|
169
|
-
value:
|
|
170
|
-
):
|
|
171
|
-
const [limit, message] =
|
|
172
|
-
? value as Readonly<[BigIntLimit, string]>
|
|
173
|
-
: [
|
|
174
|
-
value as BigIntLimit,
|
|
175
|
-
undefined,
|
|
176
|
-
];
|
|
140
|
+
value: ValueWithMessage<BigIntLimit>,
|
|
141
|
+
): LimitValidator<bigint> {
|
|
142
|
+
const [limit, message] = normalizeValueWithMessage(value);
|
|
177
143
|
|
|
178
144
|
if (typeof limit !== 'bigint') throw new TypeError(`BigInt ${type} limit must be a bigint`);
|
|
179
145
|
|
|
180
146
|
return {
|
|
181
|
-
message: message ??
|
|
147
|
+
message: message ?? defaultLimitValidateMessages[type],
|
|
182
148
|
[type]: limit,
|
|
183
149
|
type,
|
|
184
150
|
validator: (value) => {
|
|
@@ -6,7 +6,10 @@ import type {
|
|
|
6
6
|
} from 'mongoose';
|
|
7
7
|
import type { Merge } from 'type-fest';
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
EnumInput,
|
|
11
|
+
ValueWithMessage,
|
|
12
|
+
} from '../types/_internals';
|
|
10
13
|
|
|
11
14
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
15
|
|
|
@@ -35,10 +38,7 @@ export interface DateSchemaBuilder<
|
|
|
35
38
|
>;
|
|
36
39
|
|
|
37
40
|
enum: <
|
|
38
|
-
T extends
|
|
39
|
-
| Readonlyable<(D | null)[]>
|
|
40
|
-
| { [path: string]: D | null }
|
|
41
|
-
| { message?: M; values: Readonlyable<(D | null)[]> },
|
|
41
|
+
T extends EnumInput<D | null, M>,
|
|
42
42
|
D extends NativeDate,
|
|
43
43
|
M extends string,
|
|
44
44
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -53,7 +53,7 @@ export interface DateSchemaBuilder<
|
|
|
53
53
|
>;
|
|
54
54
|
|
|
55
55
|
max: <
|
|
56
|
-
T extends
|
|
56
|
+
T extends ValueWithMessage<D, S>,
|
|
57
57
|
D extends NativeDate,
|
|
58
58
|
S extends string,
|
|
59
59
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -62,7 +62,7 @@ export interface DateSchemaBuilder<
|
|
|
62
62
|
>;
|
|
63
63
|
|
|
64
64
|
min: <
|
|
65
|
-
T extends
|
|
65
|
+
T extends ValueWithMessage<D, S>,
|
|
66
66
|
D extends NativeDate,
|
|
67
67
|
S extends string,
|
|
68
68
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -8,11 +8,23 @@ import type {
|
|
|
8
8
|
} from 'mongoose';
|
|
9
9
|
import type { Merge } from 'type-fest';
|
|
10
10
|
|
|
11
|
-
import type {
|
|
12
|
-
|
|
11
|
+
import type {
|
|
12
|
+
EnumInput,
|
|
13
|
+
LimitValidator,
|
|
14
|
+
ValidationSchema,
|
|
15
|
+
ValueWithMessage,
|
|
16
|
+
} from '../types/_internals';
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
defaultLimitValidateMessages,
|
|
20
|
+
normalizeValueWithMessage,
|
|
21
|
+
setSchemaValidator,
|
|
22
|
+
} from './_internals';
|
|
13
23
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
14
24
|
|
|
15
25
|
type Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };
|
|
26
|
+
type Decimal128ValidationSchema = ValidationSchema<LimitValidator<string, Types.Decimal128>>;
|
|
27
|
+
|
|
16
28
|
type ExtendSchemaBuilder<
|
|
17
29
|
Props extends BaseProps,
|
|
18
30
|
ExtraOmitFields extends string,
|
|
@@ -25,16 +37,8 @@ interface BaseProps {
|
|
|
25
37
|
type: typeof Schema.Types.Decimal128;
|
|
26
38
|
}
|
|
27
39
|
|
|
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
|
-
|
|
36
40
|
export interface Decimal128SchemaBuilder<
|
|
37
|
-
Props extends
|
|
41
|
+
Props extends BaseProps = BaseProps,
|
|
38
42
|
ExtraOmitFields extends string = never,
|
|
39
43
|
> {
|
|
40
44
|
default: <
|
|
@@ -46,10 +50,7 @@ export interface Decimal128SchemaBuilder<
|
|
|
46
50
|
>;
|
|
47
51
|
|
|
48
52
|
enum: <
|
|
49
|
-
T extends
|
|
50
|
-
| Readonlyable<(D | null)[]>
|
|
51
|
-
| { [path: string]: D | null }
|
|
52
|
-
| { message?: M; values: Readonlyable<(D | null)[]> },
|
|
53
|
+
T extends EnumInput<D | null, M>,
|
|
53
54
|
D extends Types.Decimal128,
|
|
54
55
|
M extends string,
|
|
55
56
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -64,7 +65,7 @@ export interface Decimal128SchemaBuilder<
|
|
|
64
65
|
>;
|
|
65
66
|
|
|
66
67
|
max: <
|
|
67
|
-
T extends
|
|
68
|
+
T extends ValueWithMessage<D, S>,
|
|
68
69
|
D extends Decimal128Limit,
|
|
69
70
|
S extends string,
|
|
70
71
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -73,7 +74,7 @@ export interface Decimal128SchemaBuilder<
|
|
|
73
74
|
>;
|
|
74
75
|
|
|
75
76
|
min: <
|
|
76
|
-
T extends
|
|
77
|
+
T extends ValueWithMessage<D, S>,
|
|
77
78
|
D extends Decimal128Limit,
|
|
78
79
|
S extends string,
|
|
79
80
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -119,10 +120,6 @@ export interface Decimal128SchemaBuilder<
|
|
|
119
120
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
120
121
|
}
|
|
121
122
|
|
|
122
|
-
interface Decimal128ValidationSchema {
|
|
123
|
-
validate: Decimal128LimitValidator[];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
123
|
interface ToStringGetterSchema {
|
|
127
124
|
get: (value?: Types.Decimal128) => string | undefined;
|
|
128
125
|
leanDecimal128ToString: true;
|
|
@@ -132,29 +129,20 @@ interface ToStringSetterSchema {
|
|
|
132
129
|
set: (value?: { toString: () => string }) => string | undefined;
|
|
133
130
|
}
|
|
134
131
|
|
|
135
|
-
// Constants/Variables
|
|
136
|
-
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
137
|
-
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
138
|
-
|
|
139
132
|
// Functions
|
|
140
133
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
|
|
141
134
|
|
|
142
135
|
function createLimitValidator(
|
|
143
136
|
type: 'max' | 'min',
|
|
144
|
-
value:
|
|
145
|
-
):
|
|
146
|
-
const [limit, message] =
|
|
147
|
-
? value as Readonly<[Decimal128Limit, string]>
|
|
148
|
-
: [
|
|
149
|
-
value,
|
|
150
|
-
undefined,
|
|
151
|
-
];
|
|
137
|
+
value: ValueWithMessage<Decimal128Limit>,
|
|
138
|
+
): LimitValidator<string, Types.Decimal128> {
|
|
139
|
+
const [limit, message] = normalizeValueWithMessage(value);
|
|
152
140
|
|
|
153
141
|
const decimalLimit = new Decimal(limit.toString());
|
|
154
142
|
if (!decimalLimit.isFinite()) throw new RangeError(`Decimal128 ${type} limit must be finite`);
|
|
155
143
|
|
|
156
144
|
return {
|
|
157
|
-
message: message ??
|
|
145
|
+
message: message ?? defaultLimitValidateMessages[type],
|
|
158
146
|
[type]: decimalLimit.toString(),
|
|
159
147
|
type,
|
|
160
148
|
validator: (value) => {
|
|
@@ -167,20 +155,16 @@ function createLimitValidator(
|
|
|
167
155
|
}
|
|
168
156
|
|
|
169
157
|
export function decimal128SchemaBuilder() {
|
|
170
|
-
const schema: Record<string, any> = {};
|
|
158
|
+
const schema: Partial<Decimal128ValidationSchema> & Record<string, any> = {};
|
|
171
159
|
const baseBuilder = baseBuilderFactory(schema);
|
|
172
160
|
return new Proxy(
|
|
173
161
|
baseBuilder,
|
|
174
162
|
{
|
|
175
163
|
get(target, key, receiver) {
|
|
176
164
|
if (key === 'max' || key === 'min') {
|
|
177
|
-
return (value:
|
|
165
|
+
return (value: ValueWithMessage<Decimal128Limit>) => {
|
|
178
166
|
const validator = createLimitValidator(key, value);
|
|
179
|
-
|
|
180
|
-
schema.validate = [
|
|
181
|
-
...validators.filter(({ type }) => type !== key),
|
|
182
|
-
validator,
|
|
183
|
-
];
|
|
167
|
+
setSchemaValidator(schema, validator);
|
|
184
168
|
|
|
185
169
|
return receiver;
|
|
186
170
|
};
|
|
@@ -6,11 +6,29 @@ import type {
|
|
|
6
6
|
} from 'mongoose';
|
|
7
7
|
import type { Merge } from 'type-fest';
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
|
|
9
|
+
import type {
|
|
10
|
+
EnumInput,
|
|
11
|
+
EnumValidator,
|
|
12
|
+
LimitValidator,
|
|
13
|
+
ValidationSchema,
|
|
14
|
+
ValueWithMessage,
|
|
15
|
+
} from '../types/_internals';
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
defaultEnumValidateMessage,
|
|
19
|
+
defaultLimitValidateMessages,
|
|
20
|
+
hasSchemaValidator,
|
|
21
|
+
normalizeEnumValues,
|
|
22
|
+
normalizeValueWithMessage,
|
|
23
|
+
setSchemaValidator,
|
|
24
|
+
} from './_internals';
|
|
11
25
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
26
|
|
|
13
27
|
type DoubleLimit = number | Types.Double;
|
|
28
|
+
type DoubleValidationSchema = ValidationSchema<
|
|
29
|
+
EnumValidator<number | Types.Double, Types.Double> | LimitValidator<number, Types.Double>
|
|
30
|
+
>;
|
|
31
|
+
|
|
14
32
|
type ExtendSchemaBuilder<
|
|
15
33
|
Props extends BaseProps,
|
|
16
34
|
ExtraOmitFields extends string,
|
|
@@ -23,21 +41,6 @@ interface BaseProps {
|
|
|
23
41
|
type: typeof Schema.Types.Double;
|
|
24
42
|
}
|
|
25
43
|
|
|
26
|
-
interface DoubleEnumValidator {
|
|
27
|
-
enumValues: (null | number | Types.Double)[];
|
|
28
|
-
message: string;
|
|
29
|
-
type: 'enum';
|
|
30
|
-
validator: (value: null | Types.Double | undefined) => boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface DoubleLimitValidator {
|
|
34
|
-
max?: number;
|
|
35
|
-
message: string;
|
|
36
|
-
min?: number;
|
|
37
|
-
type: 'max' | 'min';
|
|
38
|
-
validator: (value: null | Types.Double | undefined) => boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
44
|
export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
42
45
|
default: <
|
|
43
46
|
T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined,
|
|
@@ -48,10 +51,7 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
48
51
|
>;
|
|
49
52
|
|
|
50
53
|
enum: <
|
|
51
|
-
T extends
|
|
52
|
-
| Readonlyable<(N | null)[]>
|
|
53
|
-
| { [path: string]: N | null }
|
|
54
|
-
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
54
|
+
T extends EnumInput<N | null, M>,
|
|
55
55
|
M extends string,
|
|
56
56
|
N extends number | Types.Double,
|
|
57
57
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -66,7 +66,7 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
66
66
|
>;
|
|
67
67
|
|
|
68
68
|
max: <
|
|
69
|
-
T extends
|
|
69
|
+
T extends ValueWithMessage<L, S>,
|
|
70
70
|
L extends DoubleLimit,
|
|
71
71
|
S extends string,
|
|
72
72
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -75,7 +75,7 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
75
75
|
>;
|
|
76
76
|
|
|
77
77
|
min: <
|
|
78
|
-
T extends
|
|
78
|
+
T extends ValueWithMessage<L, S>,
|
|
79
79
|
L extends DoubleLimit,
|
|
80
80
|
S extends string,
|
|
81
81
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -90,34 +90,20 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
90
90
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
interface DoubleValidationSchema {
|
|
94
|
-
validate: (DoubleEnumValidator | DoubleLimitValidator)[];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Constants/Variables
|
|
98
|
-
const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
|
|
99
|
-
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
100
|
-
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
101
|
-
|
|
102
93
|
// Functions
|
|
103
94
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);
|
|
104
95
|
|
|
105
96
|
function createLimitValidator(
|
|
106
97
|
type: 'max' | 'min',
|
|
107
|
-
value:
|
|
108
|
-
):
|
|
109
|
-
const [limit, message] =
|
|
110
|
-
? value as Readonly<[DoubleLimit, string]>
|
|
111
|
-
: [
|
|
112
|
-
value as DoubleLimit,
|
|
113
|
-
undefined,
|
|
114
|
-
];
|
|
98
|
+
value: ValueWithMessage<DoubleLimit>,
|
|
99
|
+
): LimitValidator<number, Types.Double> {
|
|
100
|
+
const [limit, message] = normalizeValueWithMessage(value);
|
|
115
101
|
|
|
116
102
|
const numericLimit = typeof limit === 'number' ? limit : limit.valueOf();
|
|
117
103
|
if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);
|
|
118
104
|
|
|
119
105
|
return {
|
|
120
|
-
message: message ??
|
|
106
|
+
message: message ?? defaultLimitValidateMessages[type],
|
|
121
107
|
[type]: numericLimit,
|
|
122
108
|
type,
|
|
123
109
|
validator: (value) => {
|
|
@@ -129,36 +115,25 @@ function createLimitValidator(
|
|
|
129
115
|
}
|
|
130
116
|
|
|
131
117
|
export function doubleSchemaBuilder() {
|
|
132
|
-
const schema: Record<string, any> = {};
|
|
118
|
+
const schema: Partial<DoubleValidationSchema> & Record<string, any> = {};
|
|
133
119
|
const baseBuilder = baseBuilderFactory(schema);
|
|
134
120
|
return new Proxy(
|
|
135
121
|
baseBuilder,
|
|
136
122
|
{
|
|
137
123
|
get(target, key, receiver) {
|
|
138
124
|
if (key === 'enum') {
|
|
139
|
-
if (schema
|
|
125
|
+
if (hasSchemaValidator(schema, 'enum')) {
|
|
140
126
|
throw new Error('Duplicate schema attribute: enum');
|
|
141
127
|
}
|
|
142
128
|
|
|
143
|
-
return (
|
|
144
|
-
|
|
145
|
-
| { [path: string]: null | number | Types.Double }
|
|
146
|
-
| { message?: string; values: Readonlyable<(null | number | Types.Double)[]> },
|
|
147
|
-
) => {
|
|
148
|
-
let enumValues: (null | number | Types.Double)[];
|
|
149
|
-
let message: string | undefined;
|
|
150
|
-
|
|
151
|
-
if (Array.isArray(value)) enumValues = [...value];
|
|
152
|
-
else if ('values' in value && Array.isArray(value.values)) {
|
|
153
|
-
enumValues = [...value.values];
|
|
154
|
-
if ('message' in value && typeof value.message === 'string') message = value.message;
|
|
155
|
-
} else enumValues = Object.values(value);
|
|
129
|
+
return (value: EnumInput<null | number | Types.Double>) => {
|
|
130
|
+
const { enumValues, message } = normalizeEnumValues(value);
|
|
156
131
|
|
|
157
132
|
const allowedValues = new Set(
|
|
158
133
|
enumValues.map((value) => value === null ? null : value.valueOf()),
|
|
159
134
|
);
|
|
160
135
|
|
|
161
|
-
const validator:
|
|
136
|
+
const validator: EnumValidator<number | Types.Double, Types.Double> = {
|
|
162
137
|
enumValues,
|
|
163
138
|
message: message ?? defaultEnumValidateMessage,
|
|
164
139
|
type: 'enum',
|
|
@@ -169,23 +144,16 @@ export function doubleSchemaBuilder() {
|
|
|
169
144
|
},
|
|
170
145
|
};
|
|
171
146
|
|
|
172
|
-
schema
|
|
173
|
-
...schema.validate ?? [],
|
|
174
|
-
validator,
|
|
175
|
-
];
|
|
147
|
+
setSchemaValidator(schema, validator);
|
|
176
148
|
|
|
177
149
|
return receiver;
|
|
178
150
|
};
|
|
179
151
|
}
|
|
180
152
|
|
|
181
153
|
if (key === 'max' || key === 'min') {
|
|
182
|
-
return (value:
|
|
154
|
+
return (value: ValueWithMessage<DoubleLimit>) => {
|
|
183
155
|
const validator = createLimitValidator(key, value);
|
|
184
|
-
|
|
185
|
-
schema.validate = [
|
|
186
|
-
...validators.filter(({ type }) => type !== key),
|
|
187
|
-
validator,
|
|
188
|
-
];
|
|
156
|
+
setSchemaValidator(schema, validator);
|
|
189
157
|
|
|
190
158
|
return receiver;
|
|
191
159
|
};
|