@kikiutils/mongoose 9.0.2 → 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/assertions.js.map +1 -1
- package/dist/plugins/assertions.js +4 -4
- package/dist/plugins/assertions.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +6 -2
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.js +5 -4
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -1
- package/dist/plugins/normalize.d.ts +3 -1
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +2 -1
- 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/bigint.d.ts +5 -25
- package/dist/schema-builders/bigint.d.ts.map +1 -1
- package/dist/schema-builders/bigint.js +9 -19
- package/dist/schema-builders/bigint.js.map +1 -1
- package/dist/schema-builders/date.d.ts +4 -9
- 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 +6 -24
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +4 -6
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +5 -25
- package/dist/schema-builders/double.d.ts.map +1 -1
- package/dist/schema-builders/double.js +9 -19
- package/dist/schema-builders/double.js.map +1 -1
- package/dist/schema-builders/int32.d.ts +5 -25
- package/dist/schema-builders/int32.d.ts.map +1 -1
- package/dist/schema-builders/int32.js +9 -19
- package/dist/schema-builders/int32.js.map +1 -1
- package/dist/schema-builders/number.d.ts +4 -9
- 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 -7
- 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 -7
- 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 +5 -10
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/types/_internals.d.ts +22 -0
- package/dist/types/_internals.d.ts.map +1 -1
- package/dist/types/aggregate-paginate.d.ts +2 -1
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts +3 -1
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/options.d.ts +6 -2
- package/dist/types/options.d.ts.map +1 -1
- package/package.json +9 -8
- 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/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
|
@@ -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
|
};
|
|
@@ -6,8 +6,22 @@ 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 ExtendSchemaBuilder<
|
|
@@ -20,25 +34,12 @@ type ExtendSchemaBuilder<
|
|
|
20
34
|
|
|
21
35
|
type Int32Limit = number;
|
|
22
36
|
|
|
37
|
+
type Int32ValidationSchema = ValidationSchema<EnumValidator<number> | LimitValidator<number>>;
|
|
38
|
+
|
|
23
39
|
interface BaseProps {
|
|
24
40
|
type: typeof Schema.Types.Int32;
|
|
25
41
|
}
|
|
26
42
|
|
|
27
|
-
interface Int32EnumValidator {
|
|
28
|
-
enumValues: (null | number)[];
|
|
29
|
-
message: string;
|
|
30
|
-
type: 'enum';
|
|
31
|
-
validator: (value: null | number | undefined) => boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface Int32LimitValidator {
|
|
35
|
-
max?: number;
|
|
36
|
-
message: string;
|
|
37
|
-
min?: number;
|
|
38
|
-
type: 'max' | 'min';
|
|
39
|
-
validator: (value: null | number | undefined) => boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
43
|
export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
43
44
|
default: <
|
|
44
45
|
T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
|
|
@@ -49,10 +50,7 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
|
|
|
49
50
|
>;
|
|
50
51
|
|
|
51
52
|
enum: <
|
|
52
|
-
T extends
|
|
53
|
-
| Readonlyable<(N | null)[]>
|
|
54
|
-
| { [path: string]: N | null }
|
|
55
|
-
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
53
|
+
T extends EnumInput<N | null, M>,
|
|
56
54
|
M extends string,
|
|
57
55
|
N extends number,
|
|
58
56
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -67,7 +65,7 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
|
|
|
67
65
|
>;
|
|
68
66
|
|
|
69
67
|
max: <
|
|
70
|
-
T extends
|
|
68
|
+
T extends ValueWithMessage<L, S>,
|
|
71
69
|
L extends Int32Limit,
|
|
72
70
|
S extends string,
|
|
73
71
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -76,7 +74,7 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
|
|
|
76
74
|
>;
|
|
77
75
|
|
|
78
76
|
min: <
|
|
79
|
-
T extends
|
|
77
|
+
T extends ValueWithMessage<L, S>,
|
|
80
78
|
L extends Int32Limit,
|
|
81
79
|
S extends string,
|
|
82
80
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -91,33 +89,19 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
|
|
|
91
89
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
92
90
|
}
|
|
93
91
|
|
|
94
|
-
interface Int32ValidationSchema {
|
|
95
|
-
validate: (Int32EnumValidator | Int32LimitValidator)[];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Constants/Variables
|
|
99
|
-
const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
|
|
100
|
-
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
101
|
-
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
102
|
-
|
|
103
92
|
// Functions
|
|
104
93
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);
|
|
105
94
|
|
|
106
95
|
function createLimitValidator(
|
|
107
96
|
type: 'max' | 'min',
|
|
108
|
-
value:
|
|
109
|
-
):
|
|
110
|
-
const [numericLimit, message] =
|
|
111
|
-
? value as Readonly<[Int32Limit, string]>
|
|
112
|
-
: [
|
|
113
|
-
value as Int32Limit,
|
|
114
|
-
undefined,
|
|
115
|
-
];
|
|
97
|
+
value: ValueWithMessage<Int32Limit>,
|
|
98
|
+
): LimitValidator<number> {
|
|
99
|
+
const [numericLimit, message] = normalizeValueWithMessage(value);
|
|
116
100
|
|
|
117
101
|
if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);
|
|
118
102
|
|
|
119
103
|
return {
|
|
120
|
-
message: message ??
|
|
104
|
+
message: message ?? defaultLimitValidateMessages[type],
|
|
121
105
|
[type]: numericLimit,
|
|
122
106
|
type,
|
|
123
107
|
validator: (value) => {
|
|
@@ -129,56 +113,38 @@ function createLimitValidator(
|
|
|
129
113
|
}
|
|
130
114
|
|
|
131
115
|
export function int32SchemaBuilder() {
|
|
132
|
-
const schema: Record<string, any> = {};
|
|
116
|
+
const schema: Partial<Int32ValidationSchema> & Record<string, any> = {};
|
|
133
117
|
const baseBuilder = baseBuilderFactory(schema);
|
|
134
118
|
return new Proxy(
|
|
135
119
|
baseBuilder,
|
|
136
120
|
{
|
|
137
121
|
get(target, key, receiver) {
|
|
138
122
|
if (key === 'enum') {
|
|
139
|
-
if (schema
|
|
123
|
+
if (hasSchemaValidator(schema, 'enum')) {
|
|
140
124
|
throw new Error('Duplicate schema attribute: enum');
|
|
141
125
|
}
|
|
142
126
|
|
|
143
|
-
return (
|
|
144
|
-
|
|
145
|
-
| { [path: string]: null | number }
|
|
146
|
-
| { message?: string; values: Readonlyable<(null | number)[]> },
|
|
147
|
-
) => {
|
|
148
|
-
let enumValues: (null | number)[];
|
|
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);
|
|
127
|
+
return (value: EnumInput<null | number>) => {
|
|
128
|
+
const { enumValues, message } = normalizeEnumValues(value);
|
|
156
129
|
|
|
157
130
|
const allowedValues = new Set(enumValues);
|
|
158
|
-
const validator:
|
|
131
|
+
const validator: EnumValidator<number> = {
|
|
159
132
|
enumValues,
|
|
160
133
|
message: message ?? defaultEnumValidateMessage,
|
|
161
134
|
type: 'enum',
|
|
162
135
|
validator: (value) => value === null || value === undefined || allowedValues.has(value),
|
|
163
136
|
};
|
|
164
137
|
|
|
165
|
-
schema
|
|
166
|
-
...schema.validate ?? [],
|
|
167
|
-
validator,
|
|
168
|
-
];
|
|
138
|
+
setSchemaValidator(schema, validator);
|
|
169
139
|
|
|
170
140
|
return receiver;
|
|
171
141
|
};
|
|
172
142
|
}
|
|
173
143
|
|
|
174
144
|
if (key === 'max' || key === 'min') {
|
|
175
|
-
return (value:
|
|
145
|
+
return (value: ValueWithMessage<Int32Limit>) => {
|
|
176
146
|
const validator = createLimitValidator(key, value);
|
|
177
|
-
|
|
178
|
-
schema.validate = [
|
|
179
|
-
...validators.filter(({ type }) => type !== key),
|
|
180
|
-
validator,
|
|
181
|
-
];
|
|
147
|
+
setSchemaValidator(schema, validator);
|
|
182
148
|
|
|
183
149
|
return receiver;
|
|
184
150
|
};
|
|
@@ -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 NumberSchemaBuilder<
|
|
|
35
38
|
>;
|
|
36
39
|
|
|
37
40
|
enum: <
|
|
38
|
-
T extends
|
|
39
|
-
| Readonlyable<(N | null)[]>
|
|
40
|
-
| { [path: string]: N | null }
|
|
41
|
-
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
41
|
+
T extends EnumInput<N | null, M>,
|
|
42
42
|
M extends string,
|
|
43
43
|
N extends number,
|
|
44
44
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -53,7 +53,7 @@ export interface NumberSchemaBuilder<
|
|
|
53
53
|
>;
|
|
54
54
|
|
|
55
55
|
max: <
|
|
56
|
-
T extends
|
|
56
|
+
T extends ValueWithMessage<N, S>,
|
|
57
57
|
N extends number,
|
|
58
58
|
S extends string,
|
|
59
59
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -62,7 +62,7 @@ export interface NumberSchemaBuilder<
|
|
|
62
62
|
>;
|
|
63
63
|
|
|
64
64
|
min: <
|
|
65
|
-
T extends
|
|
65
|
+
T extends ValueWithMessage<N, S>,
|
|
66
66
|
N extends number,
|
|
67
67
|
S extends string,
|
|
68
68
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
} from 'mongoose';
|
|
8
8
|
import type { Merge } from 'type-fest';
|
|
9
9
|
|
|
10
|
-
import type {
|
|
10
|
+
import type { EnumInput } from '../types/_internals';
|
|
11
11
|
|
|
12
12
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
13
13
|
|
|
@@ -36,10 +36,7 @@ export interface ObjectIdSchemaBuilder<
|
|
|
36
36
|
>;
|
|
37
37
|
|
|
38
38
|
enum: <
|
|
39
|
-
T extends
|
|
40
|
-
| Readonlyable<(null | O)[]>
|
|
41
|
-
| { [path: string]: null | O }
|
|
42
|
-
| { message?: M; values: Readonlyable<(null | O)[]> },
|
|
39
|
+
T extends EnumInput<null | O, M>,
|
|
43
40
|
M extends string,
|
|
44
41
|
O extends Types.ObjectId,
|
|
45
42
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
} from 'mongoose';
|
|
9
9
|
import type { Merge } from 'type-fest';
|
|
10
10
|
|
|
11
|
-
import type {
|
|
11
|
+
import type { EnumInput } from '../types/_internals';
|
|
12
12
|
|
|
13
13
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
14
14
|
|
|
@@ -37,10 +37,7 @@ export interface RefSchemaBuilder<
|
|
|
37
37
|
>;
|
|
38
38
|
|
|
39
39
|
enum: <
|
|
40
|
-
T extends
|
|
41
|
-
| Readonlyable<(null | O)[]>
|
|
42
|
-
| { [path: string]: null | O }
|
|
43
|
-
| { message?: M; values: Readonlyable<(null | O)[]> },
|
|
40
|
+
T extends EnumInput<null | O, M>,
|
|
44
41
|
M extends string,
|
|
45
42
|
O extends Types.ObjectId,
|
|
46
43
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -11,7 +11,10 @@ import type {
|
|
|
11
11
|
} from 'mongoose';
|
|
12
12
|
import type { Merge } from 'type-fest';
|
|
13
13
|
|
|
14
|
-
import type {
|
|
14
|
+
import type {
|
|
15
|
+
EnumInput,
|
|
16
|
+
ValueWithMessage,
|
|
17
|
+
} from '../types/_internals';
|
|
15
18
|
|
|
16
19
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
17
20
|
|
|
@@ -45,10 +48,7 @@ export interface StringSchemaBuilder<
|
|
|
45
48
|
>;
|
|
46
49
|
|
|
47
50
|
enum: <
|
|
48
|
-
T extends
|
|
49
|
-
| Readonlyable<(null | S)[]>
|
|
50
|
-
| { [path: string]: null | S }
|
|
51
|
-
| { message?: M; values: Readonlyable<(null | S)[]> },
|
|
51
|
+
T extends EnumInput<null | S, M>,
|
|
52
52
|
M extends string,
|
|
53
53
|
S extends string,
|
|
54
54
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -104,7 +104,7 @@ export interface StringSchemaBuilder<
|
|
|
104
104
|
* @returns This instance for chaining.
|
|
105
105
|
*/
|
|
106
106
|
length: <
|
|
107
|
-
T extends
|
|
107
|
+
T extends ValueWithMessage<L, S>,
|
|
108
108
|
L extends number,
|
|
109
109
|
S extends string,
|
|
110
110
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -134,7 +134,7 @@ export interface StringSchemaBuilder<
|
|
|
134
134
|
ExtraOmitFields
|
|
135
135
|
>;
|
|
136
136
|
maxlength: <
|
|
137
|
-
T extends
|
|
137
|
+
T extends ValueWithMessage<L, S>,
|
|
138
138
|
L extends number,
|
|
139
139
|
S extends string,
|
|
140
140
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -143,7 +143,7 @@ export interface StringSchemaBuilder<
|
|
|
143
143
|
>;
|
|
144
144
|
|
|
145
145
|
minlength: <
|
|
146
|
-
T extends
|
|
146
|
+
T extends ValueWithMessage<L, S>,
|
|
147
147
|
L extends number,
|
|
148
148
|
S extends string,
|
|
149
149
|
>(value: T) => ExtendSchemaBuilder<
|
package/src/types/_internals.ts
CHANGED
|
@@ -1 +1,27 @@
|
|
|
1
|
+
export type EnumInput<T, M extends string = string> = EnumOptions<T, M> | Readonlyable<T[]> | Record<string, T>;
|
|
1
2
|
export type Readonlyable<T> = Readonly<T> | T;
|
|
3
|
+
export type ValueWithMessage<T, M extends string = string> = Readonlyable<[T, M]> | T;
|
|
4
|
+
|
|
5
|
+
export interface EnumOptions<T, M extends string = string> {
|
|
6
|
+
message?: M;
|
|
7
|
+
values: Readonlyable<T[]>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface EnumValidator<T, V = T> {
|
|
11
|
+
enumValues: Array<null | T>;
|
|
12
|
+
message: string;
|
|
13
|
+
type: 'enum';
|
|
14
|
+
validator: (value: null | undefined | V) => boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface LimitValidator<L, V = L> {
|
|
18
|
+
max?: L;
|
|
19
|
+
message: string;
|
|
20
|
+
min?: L;
|
|
21
|
+
type: 'max' | 'min';
|
|
22
|
+
validator: (value: null | undefined | V) => boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ValidationSchema<T> {
|
|
26
|
+
validate: T[];
|
|
27
|
+
}
|