@kikiutils/mongoose 1.5.0 → 1.6.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/package.json +1 -1
- package/schema-builders/base.cjs +5 -7
- package/schema-builders/base.d.ts +2 -1
- package/schema-builders/base.mjs +5 -7
- package/schema-builders/boolean.cjs +1 -1
- package/schema-builders/boolean.d.ts +8 -8
- package/schema-builders/boolean.mjs +2 -2
- package/schema-builders/date.cjs +1 -1
- package/schema-builders/date.d.ts +11 -11
- package/schema-builders/date.mjs +2 -2
- package/schema-builders/index.cjs +3 -0
- package/schema-builders/index.d.ts +10 -12
- package/schema-builders/index.mjs +3 -1
- package/schema-builders/number.cjs +1 -1
- package/schema-builders/number.d.ts +11 -11
- package/schema-builders/number.mjs +2 -2
- package/schema-builders/ref.cjs +9 -0
- package/schema-builders/ref.d.ts +59 -0
- package/schema-builders/ref.mjs +7 -0
- package/schema-builders/string.cjs +31 -1
- package/schema-builders/string.d.ts +28 -21
- package/schema-builders/string.mjs +28 -2
- package/utils.cjs +2 -0
- package/utils.d.ts +2 -0
- package/utils.mjs +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "kiki-kanri",
|
package/schema-builders/base.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
return () => {
|
|
5
|
-
|
|
3
|
+
const createBaseSchemaBuilderFactory = (type) => {
|
|
4
|
+
return (schema = {}) => {
|
|
5
|
+
schema.type = type;
|
|
6
6
|
return new Proxy(Object.freeze({}), {
|
|
7
7
|
get(_, key, receiver) {
|
|
8
8
|
if (typeof key === 'symbol')
|
|
@@ -11,10 +11,8 @@ const createSchemaBuilder = (type) => {
|
|
|
11
11
|
throw new Error(`Duplicate schema attribute: ${key}`);
|
|
12
12
|
if (isFunctionKeys.has(key))
|
|
13
13
|
return (value) => ((schema[key] = value), receiver);
|
|
14
|
-
if (key === 'length')
|
|
15
|
-
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
16
14
|
if (key === 'nonRequired')
|
|
17
|
-
return schema;
|
|
15
|
+
return { ...schema };
|
|
18
16
|
if (key === 'required')
|
|
19
17
|
return { ...schema, required: true };
|
|
20
18
|
schema[key] = true;
|
|
@@ -32,4 +30,4 @@ const isFunctionKeys = new Set([
|
|
|
32
30
|
'minlength'
|
|
33
31
|
]);
|
|
34
32
|
|
|
35
|
-
exports.
|
|
33
|
+
exports.createBaseSchemaBuilderFactory = createBaseSchemaBuilderFactory;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Schema } from 'mongoose';
|
|
2
|
+
export declare const createBaseSchemaBuilderFactory: <Builder = Readonly<{}>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId["constructor"] | StringConstructor) => (schema?: Record<string, any>) => Builder;
|
package/schema-builders/base.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
return () => {
|
|
3
|
-
|
|
1
|
+
const createBaseSchemaBuilderFactory = (type) => {
|
|
2
|
+
return (schema = {}) => {
|
|
3
|
+
schema.type = type;
|
|
4
4
|
return new Proxy(Object.freeze({}), {
|
|
5
5
|
get(_, key, receiver) {
|
|
6
6
|
if (typeof key === 'symbol')
|
|
@@ -9,10 +9,8 @@ const createSchemaBuilder = (type) => {
|
|
|
9
9
|
throw new Error(`Duplicate schema attribute: ${key}`);
|
|
10
10
|
if (isFunctionKeys.has(key))
|
|
11
11
|
return (value) => ((schema[key] = value), receiver);
|
|
12
|
-
if (key === 'length')
|
|
13
|
-
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
14
12
|
if (key === 'nonRequired')
|
|
15
|
-
return schema;
|
|
13
|
+
return { ...schema };
|
|
16
14
|
if (key === 'required')
|
|
17
15
|
return { ...schema, required: true };
|
|
18
16
|
schema[key] = true;
|
|
@@ -30,4 +28,4 @@ const isFunctionKeys = new Set([
|
|
|
30
28
|
'minlength'
|
|
31
29
|
]);
|
|
32
30
|
|
|
33
|
-
export {
|
|
31
|
+
export { createBaseSchemaBuilderFactory };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { BooleanSchemaDefinition, DefaultType } from 'mongoose';
|
|
2
|
-
type ExtendBooleanSchemaBuilder<Props> = Omit<BooleanSchemaBuilder<Props>, keyof Props>;
|
|
2
|
+
type ExtendBooleanSchemaBuilder<Props, ExtraOmitFields extends string> = Omit<BooleanSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
3
3
|
export interface BooleanSchemaBuilder<Props = {
|
|
4
4
|
type: BooleanSchemaDefinition;
|
|
5
|
-
}, PropsWithRequired = Props & {
|
|
5
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
6
6
|
required: true;
|
|
7
7
|
}> {
|
|
8
8
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<{
|
|
@@ -11,7 +11,7 @@ export interface BooleanSchemaBuilder<Props = {
|
|
|
11
11
|
})]: (Props & {
|
|
12
12
|
default: T;
|
|
13
13
|
})[key];
|
|
14
|
-
}>;
|
|
14
|
+
}, ExtraOmitFields>;
|
|
15
15
|
nonRequired: {
|
|
16
16
|
[key in keyof Props]: Props[key];
|
|
17
17
|
};
|
|
@@ -21,7 +21,7 @@ export interface BooleanSchemaBuilder<Props = {
|
|
|
21
21
|
})]: (Props & {
|
|
22
22
|
private: true;
|
|
23
23
|
})[key];
|
|
24
|
-
}>;
|
|
24
|
+
}, ExtraOmitFields>;
|
|
25
25
|
required: {
|
|
26
26
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
27
27
|
};
|
|
@@ -31,18 +31,18 @@ export interface BooleanSchemaBuilder<Props = {
|
|
|
31
31
|
})]: (Props & {
|
|
32
32
|
sparse: true;
|
|
33
33
|
})[key];
|
|
34
|
-
}>;
|
|
34
|
+
}, ExtraOmitFields>;
|
|
35
35
|
unique: ExtendBooleanSchemaBuilder<{
|
|
36
36
|
[key in keyof (Props & {
|
|
37
37
|
unique: true;
|
|
38
38
|
})]: (Props & {
|
|
39
39
|
unique: true;
|
|
40
40
|
})[key];
|
|
41
|
-
}>;
|
|
41
|
+
}, ExtraOmitFields>;
|
|
42
42
|
}
|
|
43
|
-
export declare const booleanSchemaBuilder: () => BooleanSchemaBuilder<{
|
|
43
|
+
export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<{
|
|
44
44
|
type: BooleanSchemaDefinition;
|
|
45
|
-
}, {
|
|
45
|
+
}, never, {
|
|
46
46
|
type: BooleanSchemaDefinition;
|
|
47
47
|
} & {
|
|
48
48
|
required: true;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
2
|
|
|
3
|
-
const booleanSchemaBuilder =
|
|
3
|
+
const booleanSchemaBuilder = createBaseSchemaBuilderFactory(Boolean);
|
|
4
4
|
|
|
5
5
|
export { booleanSchemaBuilder };
|
package/schema-builders/date.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { DefaultType, DateSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
-
type ExtendDateSchemaBuilder<Props> = Omit<DateSchemaBuilder<Props>, keyof Props>;
|
|
3
|
+
type ExtendDateSchemaBuilder<Props, ExtraOmitFields extends string> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
4
4
|
export interface DateSchemaBuilder<Props = {
|
|
5
5
|
type: DateSchemaDefinition;
|
|
6
|
-
}, PropsWithRequired = Props & {
|
|
6
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
7
|
required: true;
|
|
8
8
|
}> {
|
|
9
9
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<{
|
|
@@ -12,7 +12,7 @@ export interface DateSchemaBuilder<Props = {
|
|
|
12
12
|
})]: (Props & {
|
|
13
13
|
default: T;
|
|
14
14
|
})[key];
|
|
15
|
-
}>;
|
|
15
|
+
}, ExtraOmitFields>;
|
|
16
16
|
enum: <T extends MaybeReadonly<Array<D | null>> | {
|
|
17
17
|
message?: M;
|
|
18
18
|
values: MaybeReadonly<Array<D | null>>;
|
|
@@ -24,21 +24,21 @@ export interface DateSchemaBuilder<Props = {
|
|
|
24
24
|
})]: (Props & {
|
|
25
25
|
enum: T;
|
|
26
26
|
})[key];
|
|
27
|
-
}>;
|
|
27
|
+
}, ExtraOmitFields>;
|
|
28
28
|
max: <T extends D | MaybeReadonly<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<{
|
|
29
29
|
[key in keyof (Props & {
|
|
30
30
|
max: T;
|
|
31
31
|
})]: (Props & {
|
|
32
32
|
max: T;
|
|
33
33
|
})[key];
|
|
34
|
-
}>;
|
|
34
|
+
}, ExtraOmitFields>;
|
|
35
35
|
min: <T extends D | MaybeReadonly<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<{
|
|
36
36
|
[key in keyof (Props & {
|
|
37
37
|
min: T;
|
|
38
38
|
})]: (Props & {
|
|
39
39
|
min: T;
|
|
40
40
|
})[key];
|
|
41
|
-
}>;
|
|
41
|
+
}, ExtraOmitFields>;
|
|
42
42
|
nonRequired: {
|
|
43
43
|
[key in keyof Props]: Props[key];
|
|
44
44
|
};
|
|
@@ -48,7 +48,7 @@ export interface DateSchemaBuilder<Props = {
|
|
|
48
48
|
})]: (Props & {
|
|
49
49
|
private: true;
|
|
50
50
|
})[key];
|
|
51
|
-
}>;
|
|
51
|
+
}, ExtraOmitFields>;
|
|
52
52
|
required: {
|
|
53
53
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
54
54
|
};
|
|
@@ -58,18 +58,18 @@ export interface DateSchemaBuilder<Props = {
|
|
|
58
58
|
})]: (Props & {
|
|
59
59
|
sparse: true;
|
|
60
60
|
})[key];
|
|
61
|
-
}>;
|
|
61
|
+
}, ExtraOmitFields>;
|
|
62
62
|
unique: ExtendDateSchemaBuilder<{
|
|
63
63
|
[key in keyof (Props & {
|
|
64
64
|
unique: true;
|
|
65
65
|
})]: (Props & {
|
|
66
66
|
unique: true;
|
|
67
67
|
})[key];
|
|
68
|
-
}>;
|
|
68
|
+
}, ExtraOmitFields>;
|
|
69
69
|
}
|
|
70
|
-
export declare const dateSchemaBuilder: () => DateSchemaBuilder<{
|
|
70
|
+
export declare const dateSchemaBuilder: (schema?: Record<string, any>) => DateSchemaBuilder<{
|
|
71
71
|
type: DateSchemaDefinition;
|
|
72
|
-
}, {
|
|
72
|
+
}, never, {
|
|
73
73
|
type: DateSchemaDefinition;
|
|
74
74
|
} & {
|
|
75
75
|
required: true;
|
package/schema-builders/date.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
2
|
|
|
3
|
-
const dateSchemaBuilder =
|
|
3
|
+
const dateSchemaBuilder = createBaseSchemaBuilderFactory(Date);
|
|
4
4
|
|
|
5
5
|
export { dateSchemaBuilder };
|
|
@@ -5,18 +5,21 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
const date = require('./date.cjs');
|
|
6
6
|
const boolean = require('./boolean.cjs');
|
|
7
7
|
const number = require('./number.cjs');
|
|
8
|
+
const ref = require('./ref.cjs');
|
|
8
9
|
const string = require('./string.cjs');
|
|
9
10
|
|
|
10
11
|
const schemaBuilders = {
|
|
11
12
|
date: date.dateSchemaBuilder,
|
|
12
13
|
boolean: boolean.booleanSchemaBuilder,
|
|
13
14
|
number: number.numberSchemaBuilder,
|
|
15
|
+
ref: ref.refSchemaBuilder,
|
|
14
16
|
string: string.stringSchemaBuilder
|
|
15
17
|
};
|
|
16
18
|
|
|
17
19
|
exports.dateSchemaBuilder = date.dateSchemaBuilder;
|
|
18
20
|
exports.booleanSchemaBuilder = boolean.booleanSchemaBuilder;
|
|
19
21
|
exports.numberSchemaBuilder = number.numberSchemaBuilder;
|
|
22
|
+
exports.refSchemaBuilder = ref.refSchemaBuilder;
|
|
20
23
|
exports.stringSchemaBuilder = string.stringSchemaBuilder;
|
|
21
24
|
exports.default = schemaBuilders;
|
|
22
25
|
exports.schemaBuilders = schemaBuilders;
|
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
export * from './date';
|
|
2
2
|
export * from './boolean';
|
|
3
3
|
export * from './number';
|
|
4
|
+
export * from './ref';
|
|
4
5
|
export * from './string';
|
|
5
6
|
export declare const schemaBuilders: {
|
|
6
|
-
date: () => import("./date").DateSchemaBuilder<{
|
|
7
|
+
date: (schema?: Record<string, any>) => import("./date").DateSchemaBuilder<{
|
|
7
8
|
type: import("mongoose").DateSchemaDefinition;
|
|
8
|
-
}, {
|
|
9
|
+
}, never, {
|
|
9
10
|
type: import("mongoose").DateSchemaDefinition;
|
|
10
11
|
} & {
|
|
11
12
|
required: true;
|
|
12
13
|
}>;
|
|
13
|
-
boolean: () => import("./boolean").BooleanSchemaBuilder<{
|
|
14
|
+
boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<{
|
|
14
15
|
type: import("mongoose").BooleanSchemaDefinition;
|
|
15
|
-
}, {
|
|
16
|
+
}, never, {
|
|
16
17
|
type: import("mongoose").BooleanSchemaDefinition;
|
|
17
18
|
} & {
|
|
18
19
|
required: true;
|
|
19
20
|
}>;
|
|
20
|
-
number: () => import("./number").NumberSchemaBuilder<{
|
|
21
|
+
number: (schema?: Record<string, any>) => import("./number").NumberSchemaBuilder<{
|
|
21
22
|
type: import("mongoose").NumberSchemaDefinition;
|
|
22
|
-
}, {
|
|
23
|
+
}, never, {
|
|
23
24
|
type: import("mongoose").NumberSchemaDefinition;
|
|
24
25
|
} & {
|
|
25
26
|
required: true;
|
|
26
27
|
}>;
|
|
27
|
-
string: () => import("./
|
|
28
|
-
|
|
29
|
-
}, {
|
|
30
|
-
type: import("mongoose").StringSchemaDefinition;
|
|
31
|
-
} & {
|
|
32
|
-
required: true;
|
|
28
|
+
ref: <T extends string | import("mongoose").Model<any> | ((this: any, doc: any) => string | import("mongoose").Model<any>)>(ref: T) => import("./ref").RefSchemaBuilder<{
|
|
29
|
+
ref: T;
|
|
33
30
|
}>;
|
|
31
|
+
string: () => import("./string").StringSchemaBuilder;
|
|
34
32
|
};
|
|
35
33
|
export default schemaBuilders;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { dateSchemaBuilder } from './date.mjs';
|
|
2
2
|
import { booleanSchemaBuilder } from './boolean.mjs';
|
|
3
3
|
import { numberSchemaBuilder } from './number.mjs';
|
|
4
|
+
import { refSchemaBuilder } from './ref.mjs';
|
|
4
5
|
import { stringSchemaBuilder } from './string.mjs';
|
|
5
6
|
|
|
6
7
|
const schemaBuilders = {
|
|
7
8
|
date: dateSchemaBuilder,
|
|
8
9
|
boolean: booleanSchemaBuilder,
|
|
9
10
|
number: numberSchemaBuilder,
|
|
11
|
+
ref: refSchemaBuilder,
|
|
10
12
|
string: stringSchemaBuilder
|
|
11
13
|
};
|
|
12
14
|
|
|
13
|
-
export { booleanSchemaBuilder, dateSchemaBuilder, schemaBuilders as default, numberSchemaBuilder, schemaBuilders, stringSchemaBuilder };
|
|
15
|
+
export { booleanSchemaBuilder, dateSchemaBuilder, schemaBuilders as default, numberSchemaBuilder, refSchemaBuilder, schemaBuilders, stringSchemaBuilder };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { DefaultType, NumberSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
-
type ExtendNumberSchemaBuilder<Props> = Omit<NumberSchemaBuilder<Props>, keyof Props>;
|
|
3
|
+
type ExtendNumberSchemaBuilder<Props, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
4
4
|
export interface NumberSchemaBuilder<Props = {
|
|
5
5
|
type: NumberSchemaDefinition;
|
|
6
|
-
}, PropsWithRequired = Props & {
|
|
6
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
7
|
required: true;
|
|
8
8
|
}> {
|
|
9
9
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<{
|
|
@@ -12,7 +12,7 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
12
12
|
})]: (Props & {
|
|
13
13
|
default: T;
|
|
14
14
|
})[key];
|
|
15
|
-
}>;
|
|
15
|
+
}, ExtraOmitFields>;
|
|
16
16
|
enum: <T extends MaybeReadonly<Array<N | null>> | {
|
|
17
17
|
message?: M;
|
|
18
18
|
values: MaybeReadonly<Array<N | null>>;
|
|
@@ -24,21 +24,21 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
24
24
|
})]: (Props & {
|
|
25
25
|
enum: T;
|
|
26
26
|
})[key];
|
|
27
|
-
}>;
|
|
27
|
+
}, ExtraOmitFields>;
|
|
28
28
|
max: <T extends N | MaybeReadonly<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<{
|
|
29
29
|
[key in keyof (Props & {
|
|
30
30
|
max: T;
|
|
31
31
|
})]: (Props & {
|
|
32
32
|
max: T;
|
|
33
33
|
})[key];
|
|
34
|
-
}>;
|
|
34
|
+
}, ExtraOmitFields>;
|
|
35
35
|
min: <T extends N | MaybeReadonly<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<{
|
|
36
36
|
[key in keyof (Props & {
|
|
37
37
|
min: T;
|
|
38
38
|
})]: (Props & {
|
|
39
39
|
min: T;
|
|
40
40
|
})[key];
|
|
41
|
-
}>;
|
|
41
|
+
}, ExtraOmitFields>;
|
|
42
42
|
nonRequired: {
|
|
43
43
|
[key in keyof Props]: Props[key];
|
|
44
44
|
};
|
|
@@ -48,7 +48,7 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
48
48
|
})]: (Props & {
|
|
49
49
|
private: true;
|
|
50
50
|
})[key];
|
|
51
|
-
}>;
|
|
51
|
+
}, ExtraOmitFields>;
|
|
52
52
|
required: {
|
|
53
53
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
54
54
|
};
|
|
@@ -58,18 +58,18 @@ export interface NumberSchemaBuilder<Props = {
|
|
|
58
58
|
})]: (Props & {
|
|
59
59
|
sparse: true;
|
|
60
60
|
})[key];
|
|
61
|
-
}>;
|
|
61
|
+
}, ExtraOmitFields>;
|
|
62
62
|
unique: ExtendNumberSchemaBuilder<{
|
|
63
63
|
[key in keyof (Props & {
|
|
64
64
|
unique: true;
|
|
65
65
|
})]: (Props & {
|
|
66
66
|
unique: true;
|
|
67
67
|
})[key];
|
|
68
|
-
}>;
|
|
68
|
+
}, ExtraOmitFields>;
|
|
69
69
|
}
|
|
70
|
-
export declare const numberSchemaBuilder: () => NumberSchemaBuilder<{
|
|
70
|
+
export declare const numberSchemaBuilder: (schema?: Record<string, any>) => NumberSchemaBuilder<{
|
|
71
71
|
type: NumberSchemaDefinition;
|
|
72
|
-
}, {
|
|
72
|
+
}, never, {
|
|
73
73
|
type: NumberSchemaDefinition;
|
|
74
74
|
} & {
|
|
75
75
|
required: true;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
2
|
|
|
3
|
-
const numberSchemaBuilder =
|
|
3
|
+
const numberSchemaBuilder = createBaseSchemaBuilderFactory(Number);
|
|
4
4
|
|
|
5
5
|
export { numberSchemaBuilder };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const mongoose = require('mongoose');
|
|
4
|
+
const base = require('./base.cjs');
|
|
5
|
+
|
|
6
|
+
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(mongoose.Schema.Types.ObjectId);
|
|
7
|
+
const refSchemaBuilder = (ref) => baseBuilderFactory({ ref });
|
|
8
|
+
|
|
9
|
+
exports.refSchemaBuilder = refSchemaBuilder;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { DefaultType, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
2
|
+
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
+
type ExtendRefSchemaBuilder<Props, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
4
|
+
export interface RefSchemaBuilder<Props = {
|
|
5
|
+
type: ObjectIdSchemaDefinition;
|
|
6
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
|
+
required: true;
|
|
8
|
+
}> {
|
|
9
|
+
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
|
|
10
|
+
[key in keyof (Props & {
|
|
11
|
+
default: T;
|
|
12
|
+
})]: (Props & {
|
|
13
|
+
default: T;
|
|
14
|
+
})[key];
|
|
15
|
+
}, ExtraOmitFields>;
|
|
16
|
+
enum: <T extends MaybeReadonly<Array<O | null>> | {
|
|
17
|
+
message?: M;
|
|
18
|
+
values: MaybeReadonly<Array<O | null>>;
|
|
19
|
+
} | {
|
|
20
|
+
[path: string]: O | null;
|
|
21
|
+
}, M extends string, O extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
|
|
22
|
+
[key in keyof (Props & {
|
|
23
|
+
enum: T;
|
|
24
|
+
})]: (Props & {
|
|
25
|
+
enum: T;
|
|
26
|
+
})[key];
|
|
27
|
+
}, ExtraOmitFields>;
|
|
28
|
+
nonRequired: {
|
|
29
|
+
[key in keyof Props]: Props[key];
|
|
30
|
+
};
|
|
31
|
+
private: ExtendRefSchemaBuilder<{
|
|
32
|
+
[key in keyof (Props & {
|
|
33
|
+
private: true;
|
|
34
|
+
})]: (Props & {
|
|
35
|
+
private: true;
|
|
36
|
+
})[key];
|
|
37
|
+
}, ExtraOmitFields>;
|
|
38
|
+
required: {
|
|
39
|
+
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
40
|
+
};
|
|
41
|
+
sparse: ExtendRefSchemaBuilder<{
|
|
42
|
+
[key in keyof (Props & {
|
|
43
|
+
sparse: true;
|
|
44
|
+
})]: (Props & {
|
|
45
|
+
sparse: true;
|
|
46
|
+
})[key];
|
|
47
|
+
}, ExtraOmitFields>;
|
|
48
|
+
unique: ExtendRefSchemaBuilder<{
|
|
49
|
+
[key in keyof (Props & {
|
|
50
|
+
unique: true;
|
|
51
|
+
})]: (Props & {
|
|
52
|
+
unique: true;
|
|
53
|
+
})[key];
|
|
54
|
+
}, ExtraOmitFields>;
|
|
55
|
+
}
|
|
56
|
+
export declare const refSchemaBuilder: <T extends string | Model<any> | ((this: any, doc: any) => string | Model<any>)>(ref: T) => RefSchemaBuilder<{
|
|
57
|
+
ref: T;
|
|
58
|
+
}>;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
3
|
+
|
|
4
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.ObjectId);
|
|
5
|
+
const refSchemaBuilder = (ref) => baseBuilderFactory({ ref });
|
|
6
|
+
|
|
7
|
+
export { refSchemaBuilder };
|
|
@@ -1,7 +1,37 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const net = require('node:net');
|
|
3
4
|
const base = require('./base.cjs');
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
+
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
7
|
+
|
|
8
|
+
const net__default = /*#__PURE__*/_interopDefaultCompat(net);
|
|
9
|
+
|
|
10
|
+
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(String);
|
|
11
|
+
const stringSchemaBuilder = () => {
|
|
12
|
+
const schema = {};
|
|
13
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
14
|
+
return new Proxy(baseBuilder, {
|
|
15
|
+
get(target, key, receiver) {
|
|
16
|
+
if (key === 'ipv4') {
|
|
17
|
+
return (message = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.') => {
|
|
18
|
+
schema.trim = true;
|
|
19
|
+
schema.validate = { message, validator: (value) => net__default.isIPv4(value) };
|
|
20
|
+
return receiver;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (key === 'ipv6') {
|
|
24
|
+
return (message = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.') => {
|
|
25
|
+
schema.trim = true;
|
|
26
|
+
schema.validate = { message, validator: (value) => net__default.isIPv6(value) };
|
|
27
|
+
return receiver;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (key === 'length')
|
|
31
|
+
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
32
|
+
return Reflect.get(target, key, receiver);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
6
36
|
|
|
7
37
|
exports.stringSchemaBuilder = stringSchemaBuilder;
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { DefaultType, StringSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { MaybeReadonly } from '../types/utils';
|
|
3
|
-
type ExtendStringSchemaBuilder<Props> = Omit<StringSchemaBuilder<Props>, keyof Props>;
|
|
3
|
+
type ExtendStringSchemaBuilder<Props, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
4
|
+
type IPSchema<T extends string> = {
|
|
5
|
+
trim: true;
|
|
6
|
+
validate: {
|
|
7
|
+
message: T;
|
|
8
|
+
validator: (value: string) => boolean;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
4
11
|
export interface StringSchemaBuilder<Props = {
|
|
5
12
|
type: StringSchemaDefinition;
|
|
6
|
-
}, PropsWithRequired = Props & {
|
|
13
|
+
}, ExtraOmitFields extends string = never, PropsWithRequired = Props & {
|
|
7
14
|
required: true;
|
|
8
15
|
}> {
|
|
9
16
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
@@ -12,7 +19,7 @@ export interface StringSchemaBuilder<Props = {
|
|
|
12
19
|
})]: (Props & {
|
|
13
20
|
default: T;
|
|
14
21
|
})[key];
|
|
15
|
-
}>;
|
|
22
|
+
}, ExtraOmitFields>;
|
|
16
23
|
enum: <T extends MaybeReadonly<Array<S | null>> | {
|
|
17
24
|
message?: M;
|
|
18
25
|
values: MaybeReadonly<Array<S | null>>;
|
|
@@ -24,7 +31,13 @@ export interface StringSchemaBuilder<Props = {
|
|
|
24
31
|
})]: (Props & {
|
|
25
32
|
enum: T;
|
|
26
33
|
})[key];
|
|
27
|
-
}>;
|
|
34
|
+
}, ExtraOmitFields>;
|
|
35
|
+
ipv4: <T extends string = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.'>(message?: T) => ExtendStringSchemaBuilder<{
|
|
36
|
+
[key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
|
|
37
|
+
}, ExtraOmitFields | 'ipv4' | 'ipv6'>;
|
|
38
|
+
ipv6: <T extends string = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.'>(message?: T) => ExtendStringSchemaBuilder<{
|
|
39
|
+
[key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
|
|
40
|
+
}, ExtraOmitFields | 'ipv4' | 'ipv6'>;
|
|
28
41
|
length: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
29
42
|
[key in keyof (Props & {
|
|
30
43
|
maxlength: T;
|
|
@@ -33,28 +46,28 @@ export interface StringSchemaBuilder<Props = {
|
|
|
33
46
|
maxlength: T;
|
|
34
47
|
minlength: T;
|
|
35
48
|
})[key];
|
|
36
|
-
}>;
|
|
49
|
+
}, ExtraOmitFields>;
|
|
37
50
|
lowercase: ExtendStringSchemaBuilder<{
|
|
38
51
|
[key in keyof (Props & {
|
|
39
52
|
lowercase: true;
|
|
40
53
|
})]: (Props & {
|
|
41
54
|
lowercase: true;
|
|
42
55
|
})[key];
|
|
43
|
-
}>;
|
|
56
|
+
}, ExtraOmitFields>;
|
|
44
57
|
maxlength: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
45
58
|
[key in keyof (Props & {
|
|
46
59
|
maxlength: T;
|
|
47
60
|
})]: (Props & {
|
|
48
61
|
maxlength: T;
|
|
49
62
|
})[key];
|
|
50
|
-
}>;
|
|
63
|
+
}, ExtraOmitFields>;
|
|
51
64
|
minlength: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
|
|
52
65
|
[key in keyof (Props & {
|
|
53
66
|
minlength: T;
|
|
54
67
|
})]: (Props & {
|
|
55
68
|
minlength: T;
|
|
56
69
|
})[key];
|
|
57
|
-
}>;
|
|
70
|
+
}, ExtraOmitFields>;
|
|
58
71
|
nonRequired: {
|
|
59
72
|
[key in keyof Props]: Props[key];
|
|
60
73
|
};
|
|
@@ -64,7 +77,7 @@ export interface StringSchemaBuilder<Props = {
|
|
|
64
77
|
})]: (Props & {
|
|
65
78
|
private: true;
|
|
66
79
|
})[key];
|
|
67
|
-
}>;
|
|
80
|
+
}, ExtraOmitFields>;
|
|
68
81
|
required: {
|
|
69
82
|
[key in keyof PropsWithRequired]: PropsWithRequired[key];
|
|
70
83
|
};
|
|
@@ -74,41 +87,35 @@ export interface StringSchemaBuilder<Props = {
|
|
|
74
87
|
})]: (Props & {
|
|
75
88
|
sparse: true;
|
|
76
89
|
})[key];
|
|
77
|
-
}>;
|
|
90
|
+
}, ExtraOmitFields>;
|
|
78
91
|
text: ExtendStringSchemaBuilder<{
|
|
79
92
|
[key in keyof (Props & {
|
|
80
93
|
text: true;
|
|
81
94
|
})]: (Props & {
|
|
82
95
|
text: true;
|
|
83
96
|
})[key];
|
|
84
|
-
}>;
|
|
97
|
+
}, ExtraOmitFields>;
|
|
85
98
|
trim: ExtendStringSchemaBuilder<{
|
|
86
99
|
[key in keyof (Props & {
|
|
87
100
|
trim: true;
|
|
88
101
|
})]: (Props & {
|
|
89
102
|
trim: true;
|
|
90
103
|
})[key];
|
|
91
|
-
}>;
|
|
104
|
+
}, ExtraOmitFields>;
|
|
92
105
|
unique: ExtendStringSchemaBuilder<{
|
|
93
106
|
[key in keyof (Props & {
|
|
94
107
|
unique: true;
|
|
95
108
|
})]: (Props & {
|
|
96
109
|
unique: true;
|
|
97
110
|
})[key];
|
|
98
|
-
}>;
|
|
111
|
+
}, ExtraOmitFields>;
|
|
99
112
|
uppercase: ExtendStringSchemaBuilder<{
|
|
100
113
|
[key in keyof (Props & {
|
|
101
114
|
uppercase: true;
|
|
102
115
|
})]: (Props & {
|
|
103
116
|
uppercase: true;
|
|
104
117
|
})[key];
|
|
105
|
-
}>;
|
|
118
|
+
}, ExtraOmitFields>;
|
|
106
119
|
}
|
|
107
|
-
export declare const stringSchemaBuilder: () => StringSchemaBuilder
|
|
108
|
-
type: StringSchemaDefinition;
|
|
109
|
-
}, {
|
|
110
|
-
type: StringSchemaDefinition;
|
|
111
|
-
} & {
|
|
112
|
-
required: true;
|
|
113
|
-
}>;
|
|
120
|
+
export declare const stringSchemaBuilder: () => StringSchemaBuilder;
|
|
114
121
|
export {};
|
|
@@ -1,5 +1,31 @@
|
|
|
1
|
-
import
|
|
1
|
+
import net from 'node:net';
|
|
2
|
+
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
|
|
5
|
+
const stringSchemaBuilder = () => {
|
|
6
|
+
const schema = {};
|
|
7
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
8
|
+
return new Proxy(baseBuilder, {
|
|
9
|
+
get(target, key, receiver) {
|
|
10
|
+
if (key === 'ipv4') {
|
|
11
|
+
return (message = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.') => {
|
|
12
|
+
schema.trim = true;
|
|
13
|
+
schema.validate = { message, validator: (value) => net.isIPv4(value) };
|
|
14
|
+
return receiver;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
if (key === 'ipv6') {
|
|
18
|
+
return (message = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.') => {
|
|
19
|
+
schema.trim = true;
|
|
20
|
+
schema.validate = { message, validator: (value) => net.isIPv6(value) };
|
|
21
|
+
return receiver;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (key === 'length')
|
|
25
|
+
return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
|
|
26
|
+
return Reflect.get(target, key, receiver);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
4
30
|
|
|
5
31
|
export { stringSchemaBuilder };
|
package/utils.cjs
CHANGED
|
@@ -276,6 +276,8 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
276
276
|
* This function generates a schema definition for an ObjectId reference field, including
|
|
277
277
|
* attributes such as 'private', 'required', and 'unique'.
|
|
278
278
|
*
|
|
279
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
|
|
280
|
+
*
|
|
279
281
|
* @template R - The type of the reference model name.
|
|
280
282
|
* @template T - An array of attributes to apply to the schema field.
|
|
281
283
|
*
|
package/utils.d.ts
CHANGED
|
@@ -386,6 +386,8 @@ export declare function createCommonMongooseSchemas<T extends {}>(customSchemas?
|
|
|
386
386
|
* This function generates a schema definition for an ObjectId reference field, including
|
|
387
387
|
* attributes such as 'private', 'required', and 'unique'.
|
|
388
388
|
*
|
|
389
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
|
|
390
|
+
*
|
|
389
391
|
* @template R - The type of the reference model name.
|
|
390
392
|
* @template T - An array of attributes to apply to the schema field.
|
|
391
393
|
*
|
package/utils.mjs
CHANGED
|
@@ -265,6 +265,8 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
265
265
|
* This function generates a schema definition for an ObjectId reference field, including
|
|
266
266
|
* attributes such as 'private', 'required', and 'unique'.
|
|
267
267
|
*
|
|
268
|
+
* @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
|
|
269
|
+
*
|
|
268
270
|
* @template R - The type of the reference model name.
|
|
269
271
|
* @template T - An array of attributes to apply to the schema field.
|
|
270
272
|
*
|