@kikiutils/mongoose 7.1.0 → 9.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 +4 -0
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +2 -1
- package/dist/_internals.js.map +1 -1
- package/dist/builders.d.ts +1 -3
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +8 -9
- 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.map +1 -1
- package/dist/options.js +2 -1
- package/dist/options.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +26 -0
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -0
- package/dist/plugins/lean-decimal128-to-string.js +85 -0
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -0
- package/dist/plugins/normalize.d.ts +24 -14
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +20 -10
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/base.d.ts +1 -1
- 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 +60 -0
- package/dist/schema-builders/bigint.d.ts.map +1 -0
- package/dist/schema-builders/bigint.js +58 -0
- package/dist/schema-builders/bigint.js.map +1 -0
- 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 -2
- 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 +20 -14
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +1 -0
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +60 -0
- package/dist/schema-builders/double.d.ts.map +1 -0
- package/dist/schema-builders/double.js +62 -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 +60 -0
- package/dist/schema-builders/int32.d.ts.map +1 -0
- package/dist/schema-builders/int32.js +58 -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 -2
- 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 -2
- 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 -2
- 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 +35 -25
- package/dist/schema-builders/string.d.ts.map +1 -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/aggregate-paginate.d.ts +24 -8
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +9 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +6 -2
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +17 -12
- package/src/_internals.ts +3 -0
- package/src/builders.ts +18 -32
- package/src/connection.ts +14 -0
- package/src/options.ts +8 -1
- package/src/plugins/lean-decimal128-to-string.ts +149 -0
- package/src/plugins/normalize.ts +39 -15
- package/src/schema-builders/base.ts +6 -1
- package/src/schema-builders/bigint.ts +190 -0
- package/src/schema-builders/boolean.ts +1 -4
- package/src/schema-builders/date.ts +2 -2
- package/src/schema-builders/decimal128.ts +21 -14
- package/src/schema-builders/double.ts +198 -0
- package/src/schema-builders/index.ts +15 -0
- package/src/schema-builders/int32.ts +191 -0
- package/src/schema-builders/mixed.ts +37 -0
- package/src/schema-builders/number.ts +2 -2
- package/src/schema-builders/object-id.ts +2 -2
- package/src/schema-builders/ref.ts +2 -2
- package/src/schema-builders/string.ts +35 -25
- 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 +12 -3
- package/src/types/paginate.ts +6 -2
- package/dist/constants.d.ts +0 -5
- 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 -5
- 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,58 @@
|
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from "./base.js";
|
|
2
|
+
import { Schema } from "mongoose";
|
|
3
|
+
//#region src/schema-builders/bigint.ts
|
|
4
|
+
const defaultEnumValidateMessage = "Path `{PATH}` ({VALUE}) is not a valid enum value.";
|
|
5
|
+
const defaultMinValidateMessage = "Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).";
|
|
6
|
+
const defaultMaxValidateMessage = "Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).";
|
|
7
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);
|
|
8
|
+
function bigintSchemaBuilder() {
|
|
9
|
+
const schema = {};
|
|
10
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
11
|
+
return new Proxy(baseBuilder, { get(target, key, receiver) {
|
|
12
|
+
if (key === "enum") {
|
|
13
|
+
if (schema.validate?.some(({ type }) => type === "enum")) throw new Error("Duplicate schema attribute: enum");
|
|
14
|
+
return (value) => {
|
|
15
|
+
let enumValues;
|
|
16
|
+
let message;
|
|
17
|
+
if (Array.isArray(value)) enumValues = [...value];
|
|
18
|
+
else if ("values" in value && Array.isArray(value.values)) {
|
|
19
|
+
enumValues = [...value.values];
|
|
20
|
+
if ("message" in value && typeof value.message === "string") message = value.message;
|
|
21
|
+
} else enumValues = Object.values(value);
|
|
22
|
+
const allowedValues = new Set(enumValues);
|
|
23
|
+
const validator = {
|
|
24
|
+
enumValues,
|
|
25
|
+
message: message ?? defaultEnumValidateMessage,
|
|
26
|
+
type: "enum",
|
|
27
|
+
validator: (value) => value === null || value === void 0 || allowedValues.has(value)
|
|
28
|
+
};
|
|
29
|
+
schema.validate = [...schema.validate ?? [], validator];
|
|
30
|
+
return receiver;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (key === "max" || key === "min") return (value) => {
|
|
34
|
+
const validator = createLimitValidator(key, value);
|
|
35
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
36
|
+
schema.validate = [...validators.filter(({ type }) => type !== key), validator];
|
|
37
|
+
return receiver;
|
|
38
|
+
};
|
|
39
|
+
return Reflect.get(target, key, receiver);
|
|
40
|
+
} });
|
|
41
|
+
}
|
|
42
|
+
function createLimitValidator(type, value) {
|
|
43
|
+
const [limit, message] = Array.isArray(value) ? value : [value, void 0];
|
|
44
|
+
if (typeof limit !== "bigint") throw new TypeError(`BigInt ${type} limit must be a bigint`);
|
|
45
|
+
return {
|
|
46
|
+
message: message ?? (type === "min" ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
47
|
+
[type]: limit,
|
|
48
|
+
type,
|
|
49
|
+
validator: (value) => {
|
|
50
|
+
if (value === null || value === void 0) return true;
|
|
51
|
+
return type === "min" ? value >= limit : value <= limit;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { bigintSchemaBuilder };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=bigint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bigint.js","names":[],"sources":["../../src/schema-builders/bigint.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype BigIntLimit = bigint;\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n BigIntSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.BigInt;\n}\n\ninterface BigIntEnumValidator {\n enumValues: (bigint | null)[];\n message: string;\n type: 'enum';\n validator: (value: bigint | null | undefined) => boolean;\n}\n\ninterface BigIntLimitValidator {\n max?: bigint;\n message: string;\n min?: bigint;\n type: 'max' | 'min';\n validator: (value: bigint | null | undefined) => boolean;\n}\n\nexport interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,\n D extends bigint,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<(N | null)[]>\n | { [path: string]: N | null }\n | { message?: M; values: Readonlyable<(N | null)[]> },\n M extends string,\n N extends bigint,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, BigIntValidationSchema>,\n 'enum' | ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends L | Readonlyable<[L, S]>,\n L extends BigIntLimit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, BigIntValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends L | Readonlyable<[L, S]>,\n L extends BigIntLimit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, BigIntValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\ninterface BigIntValidationSchema {\n validate: (BigIntEnumValidator | BigIntLimitValidator)[];\n}\n\n// Constants/Variables\nconst defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';\nconst defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';\nconst defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);\n\nexport function bigintSchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'enum') {\n if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {\n throw new Error('Duplicate schema attribute: enum');\n }\n\n return (\n value: Readonlyable<(bigint | null)[]>\n | { [path: string]: bigint | null }\n | { message?: string; values: Readonlyable<(bigint | null)[]> },\n ) => {\n let enumValues: (bigint | null)[];\n let message: string | undefined;\n\n if (Array.isArray(value)) enumValues = [...value];\n else if ('values' in value && Array.isArray(value.values)) {\n enumValues = [...value.values];\n if ('message' in value && typeof value.message === 'string') message = value.message;\n } else enumValues = Object.values(value);\n\n const allowedValues = new Set(enumValues);\n const validator: BigIntEnumValidator = {\n enumValues,\n message: message ?? defaultEnumValidateMessage,\n type: 'enum',\n validator: (value) => value === null || value === undefined || allowedValues.has(value),\n };\n\n schema.validate = [\n ...schema.validate ?? [],\n validator,\n ];\n\n return receiver;\n };\n }\n\n if (key === 'max' || key === 'min') {\n return (value: BigIntLimit | Readonlyable<[BigIntLimit, string]>) => {\n const validator = createLimitValidator(key, value);\n const validators = Array.isArray(schema.validate) ? schema.validate : [];\n schema.validate = [\n ...validators.filter(({ type }) => type !== key),\n validator,\n ];\n\n return receiver;\n };\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as BigIntSchemaBuilder;\n}\n\nfunction createLimitValidator(\n type: 'max' | 'min',\n value: BigIntLimit | Readonlyable<[BigIntLimit, string]>,\n): BigIntLimitValidator {\n const [limit, message] = Array.isArray(value)\n ? value as Readonly<[BigIntLimit, string]>\n : [\n value as BigIntLimit,\n undefined,\n ];\n\n if (typeof limit !== 'bigint') throw new TypeError(`BigInt ${type} limit must be a bigint`);\n\n return {\n message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),\n [type]: limit,\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n return type === 'min' ? value >= limit : value <= limit;\n },\n };\n}\n"],"mappings":";;;AAiGA,MAAM,6BAA6B;AACnC,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,OAAO,MAAM,MAAM;AAE7E,SAAgB,sBAAsB;CAClC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,QAAQ;GAChB,IAAI,OAAO,UAAU,MAAM,EAAE,WAA6B,SAAS,MAAM,GACrE,MAAM,IAAI,MAAM,kCAAkC;GAGtD,QACI,UAGC;IACD,IAAI;IACJ,IAAI;IAEJ,IAAI,MAAM,QAAQ,KAAK,GAAG,aAAa,CAAC,GAAG,KAAK;SAC3C,IAAI,YAAY,SAAS,MAAM,QAAQ,MAAM,MAAM,GAAG;KACvD,aAAa,CAAC,GAAG,MAAM,MAAM;KAC7B,IAAI,aAAa,SAAS,OAAO,MAAM,YAAY,UAAU,UAAU,MAAM;IACjF,OAAO,aAAa,OAAO,OAAO,KAAK;IAEvC,MAAM,gBAAgB,IAAI,IAAI,UAAU;IACxC,MAAM,YAAiC;KACnC;KACA,SAAS,WAAW;KACpB,MAAM;KACN,YAAY,UAAU,UAAU,QAAQ,UAAU,KAAA,KAAa,cAAc,IAAI,KAAK;IAC1F;IAEA,OAAO,WAAW,CACd,GAAG,OAAO,YAAY,CAAC,GACvB,SACJ;IAEA,OAAO;GACX;EACJ;EAEA,IAAI,QAAQ,SAAS,QAAQ,OACzB,QAAQ,UAA6D;GACjE,MAAM,YAAY,qBAAqB,KAAK,KAAK;GACjD,MAAM,aAAa,MAAM,QAAQ,OAAO,QAAQ,IAAI,OAAO,WAAW,CAAC;GACvE,OAAO,WAAW,CACd,GAAG,WAAW,QAAQ,EAAE,WAAW,SAAS,GAAG,GAC/C,SACJ;GAEA,OAAO;EACX;EAGJ,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ;AAEA,SAAS,qBACL,MACA,OACoB;CACpB,MAAM,CAAC,OAAO,WAAW,MAAM,QAAQ,KAAK,IACtC,QACA,CACE,OACA,KAAA,CACJ;CAEJ,IAAI,OAAO,UAAU,UAAU,MAAM,IAAI,UAAU,UAAU,KAAK,wBAAwB;CAE1F,OAAO;EACH,SAAS,YAAY,SAAS,QAAQ,4BAA4B;GACjE,OAAO;EACR;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAElD,OAAO,SAAS,QAAQ,SAAS,QAAQ,SAAS;EACtD;CACJ;AACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.d.ts","names":[],"sources":["../../src/schema-builders/boolean.ts"],"mappings":";;;KAUK,oBACD,cAAc,WACd,kCACA,KACA,qBAAqB,OAAO,kBAC5B,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,
|
|
1
|
+
{"version":3,"file":"boolean.d.ts","names":[],"sources":["../../src/schema-builders/boolean.ts"],"mappings":";;;KAUK,oBACD,cAAc,WACd,kCACA,KACA,qBAAqB,OAAO,kBAC5B,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,qBAAqB,cAAc,YAAY,WAAW;EACvE,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,mBACF,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;qBAGnD,uBAAoB,SAAA,wBAAA,qBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.js","names":[],"sources":["../../src/schema-builders/boolean.ts"],"sourcesContent":["import type {\n BooleanSchemaDefinition,\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n BooleanSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: BooleanSchemaDefinition;\n}\n\nexport interface BooleanSchemaBuilder
|
|
1
|
+
{"version":3,"file":"boolean.js","names":[],"sources":["../../src/schema-builders/boolean.ts"],"sourcesContent":["import type {\n BooleanSchemaDefinition,\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n BooleanSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: BooleanSchemaDefinition;\n}\n\nexport interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends boolean,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nexport const booleanSchemaBuilder = createBaseSchemaBuilderFactory<BooleanSchemaBuilder>(Boolean);\n"],"mappings":";;AA4CA,MAAa,uBAAuB,+BAAqD,OAAO"}
|
|
@@ -14,11 +14,11 @@ export interface DateSchemaBuilder<Props extends {
|
|
|
14
14
|
default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends NativeDate>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
15
15
|
default: T;
|
|
16
16
|
}>, ExtraOmitFields>;
|
|
17
|
-
enum: <T extends Readonlyable<
|
|
17
|
+
enum: <T extends Readonlyable<(D | null)[]> | {
|
|
18
18
|
[path: string]: D | null;
|
|
19
19
|
} | {
|
|
20
20
|
message?: M;
|
|
21
|
-
values: Readonlyable<
|
|
21
|
+
values: Readonlyable<(D | null)[]>;
|
|
22
22
|
}, D extends NativeDate, M extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
23
23
|
enum: T;
|
|
24
24
|
}>, ExtraOmitFields>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"date.d.ts","names":[],"sources":["../../src/schema-builders/date.ts"],"mappings":";;;;KAYK,oBACD,cAAc,WACd,kCACA,KACA,kBAAkB,OAAO,kBACzB,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,kBACb;EAAgB,MAAM;;EAA2B,MAAM;GACvD;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,YACZ,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,
|
|
1
|
+
{"version":3,"file":"date.d.ts","names":[],"sources":["../../src/schema-builders/date.ts"],"mappings":";;;;KAYK,oBACD,cAAc,WACd,kCACA,KACA,kBAAkB,OAAO,kBACzB,wBAAwB;UAGlB;EACN,MAAM;;iBAGO,kBACb;EAAgB,MAAM;;EAA2B,MAAM;GACvD;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,YACZ,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,cAAc;KACX,eAAe;;IAChB,UAAU;IAAG,QAAQ,cAAc;KACvC,UAAU,YACV,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,MAAM;MACrB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,YACV,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,KAAK;MACpB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,YACV,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,KAAK;MACpB;EAGJ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;qBAGnD,oBAAiB,SAAA,wBAAA;EAtD6B,MAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"date.js","names":[],"sources":["../../src/schema-builders/date.ts"],"sourcesContent":["import type {\n DateSchemaDefinition,\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n DateSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: DateSchemaDefinition;\n}\n\nexport interface DateSchemaBuilder<\n Props extends { type: DateSchemaDefinition } = { type: DateSchemaDefinition },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends NativeDate,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<
|
|
1
|
+
{"version":3,"file":"date.js","names":[],"sources":["../../src/schema-builders/date.ts"],"sourcesContent":["import type {\n DateSchemaDefinition,\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n DateSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: DateSchemaDefinition;\n}\n\nexport interface DateSchemaBuilder<\n Props extends { type: DateSchemaDefinition } = { type: DateSchemaDefinition },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends NativeDate,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<(D | null)[]>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<(D | null)[]> },\n D extends NativeDate,\n M extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends D | Readonlyable<[D, S]>,\n D extends NativeDate,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { max: T }>,\n ExtraOmitFields\n >;\n\n min: <\n T extends D | Readonlyable<[D, S]>,\n D extends NativeDate,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { min: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nexport const dateSchemaBuilder = createBaseSchemaBuilderFactory<DateSchemaBuilder>(Date);\n"],"mappings":";;AA+EA,MAAa,oBAAoB,+BAAkD,IAAI"}
|
|
@@ -25,11 +25,11 @@ export interface Decimal128SchemaBuilder<Props extends {
|
|
|
25
25
|
default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.Decimal128>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
26
26
|
default: T;
|
|
27
27
|
}>, ExtraOmitFields>;
|
|
28
|
-
enum: <T extends Readonlyable<
|
|
28
|
+
enum: <T extends Readonlyable<(D | null)[]> | {
|
|
29
29
|
[path: string]: D | null;
|
|
30
30
|
} | {
|
|
31
31
|
message?: M;
|
|
32
|
-
values: Readonlyable<
|
|
32
|
+
values: Readonlyable<(D | null)[]>;
|
|
33
33
|
}, D extends Types.Decimal128, M extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
34
34
|
enum: T;
|
|
35
35
|
}>, ExtraOmitFields>;
|
|
@@ -49,24 +49,29 @@ export interface Decimal128SchemaBuilder<Props extends {
|
|
|
49
49
|
required: true;
|
|
50
50
|
}>;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
* This method allows specifying the number of decimal places and the rounding strategy to be used when
|
|
54
|
-
* saving the field to the database.
|
|
52
|
+
* Configures a setter that rounds decimal values and formats them with a fixed number of decimal places.
|
|
55
53
|
*
|
|
56
|
-
* @
|
|
57
|
-
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Replaces the configured setter on this builder. The setter converts non-nullish values through Decimal.js
|
|
56
|
+
* and returns a decimal string for Mongoose to cast to `Decimal128`. It returns `undefined` for `null` or
|
|
57
|
+
* `undefined`. Decimal.js validates conversion and formatting when the setter processes a value,
|
|
58
|
+
* not when the setter is configured.
|
|
58
59
|
*
|
|
59
|
-
* @
|
|
60
|
+
* @param places - The non-negative integer number of decimal places. Defaults to `2`.
|
|
61
|
+
* @param rounding - The Decimal.js rounding mode. Defaults to `Decimal.ROUND_DOWN`, which rounds toward zero.
|
|
62
|
+
*
|
|
63
|
+
* @returns This instance for chaining.
|
|
60
64
|
*/
|
|
61
65
|
setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendSchemaBuilder<Merge<Props, ToStringSetterSchema>, 'setRoundAndToFixedSetter' | ExtraOmitFields>;
|
|
62
66
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
+
* Configures a getter that converts decimal values to strings.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Replaces the configured getter on this builder. The getter returns the value's string representation,
|
|
71
|
+
* or `undefined` for `null` or `undefined`. It changes the read representation, not the stored BSON type.
|
|
72
|
+
* Marks this path for the lean Decimal128-to-string plugin; the plugin does not invoke this getter.
|
|
67
73
|
*
|
|
68
|
-
* @returns
|
|
69
|
-
* when accessed in the application.
|
|
74
|
+
* @returns This instance for chaining.
|
|
70
75
|
*/
|
|
71
76
|
setToStringGetter: ExtendSchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;
|
|
72
77
|
sparse: ExtendSchemaBuilder<Merge<Props, {
|
|
@@ -81,6 +86,7 @@ interface Decimal128ValidationSchema {
|
|
|
81
86
|
}
|
|
82
87
|
interface ToStringGetterSchema {
|
|
83
88
|
get: (value?: Types.Decimal128) => string | undefined;
|
|
89
|
+
leanDecimal128ToString: true;
|
|
84
90
|
}
|
|
85
91
|
interface ToStringSetterSchema {
|
|
86
92
|
set: (value?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decimal128.d.ts","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"mappings":";;;;;KAcK,kBAAkB,QAAQ,QAAQ,MAAM;EAAe;;KACvD,oBACD,cAAc,WACd,kCACA,KACA,wBAAwB,OAAO,kBAC/B,wBAAwB;UAGlB;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN;EACA;EACA;EACA;EACA,YAAY,cAAc,MAAM;;iBAGnB,wBACb;EAAgB,aAAa,OAAO,MAAM;;EAAiB,aAAa,OAAO,MAAM;GACrF;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,MAAM,YAClB,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,
|
|
1
|
+
{"version":3,"file":"decimal128.d.ts","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"mappings":";;;;;KAcK,kBAAkB,QAAQ,QAAQ,MAAM;EAAe;;KACvD,oBACD,cAAc,WACd,kCACA,KACA,wBAAwB,OAAO,kBAC/B,wBAAwB;UAGlB;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN;EACA;EACA;EACA;EACA,YAAY,cAAc,MAAM;;iBAGnB,wBACb;EAAgB,aAAa,OAAO,MAAM;;EAAiB,aAAa,OAAO,MAAM;GACrF;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,UAAU,MAAM,YAClB,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,cAAc;KACX,eAAe;;IAChB,UAAU;IAAG,QAAQ,cAAc;KACvC,UAAU,MAAM,YAChB,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,MAAM;MACrB;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,iBACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,qCACL;EAGZ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,iBACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,qCACL;EAGZ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;;;;;;;;;;;;;;;EAgBzB,2BAA2B,iBAAiB,WAAW,QAAQ,aAAa,oBACxE,MAAM,OAAO,oDACgB;;;;;;;;;;;EAajC,mBAAmB,oBAAoB,MAAM,OAAO,6CAA6C;EACjG,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;UAGtD;EACN,UAAU;;UAGJ;EACN,MAAM,QAAQ,MAAM;EACpB;;UAGM;EACN,MAAM;IAAU;;;wBAqCJ,2BA0CP"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decimal128.js","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface Decimal128LimitValidator {\n max?: string;\n message: string;\n min?: string;\n type: 'max' | 'min';\n validator: (value: null | Types.Decimal128 | undefined) => boolean;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<
|
|
1
|
+
{"version":3,"file":"decimal128.js","names":[],"sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface Decimal128LimitValidator {\n max?: string;\n message: string;\n min?: string;\n type: 'max' | 'min';\n validator: (value: null | Types.Decimal128 | undefined) => boolean;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<(D | null)[]>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<(D | null)[]> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends D | Readonlyable<[D, S]>,\n D extends Decimal128Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Decimal128ValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends D | Readonlyable<[D, S]>,\n D extends Decimal128Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Decimal128ValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n * Configures a setter that rounds decimal values and formats them with a fixed number of decimal places.\n *\n * @remarks\n * Replaces the configured setter on this builder. The setter converts non-nullish values through Decimal.js\n * and returns a decimal string for Mongoose to cast to `Decimal128`. It returns `undefined` for `null` or\n * `undefined`. Decimal.js validates conversion and formatting when the setter processes a value,\n * not when the setter is configured.\n *\n * @param places - The non-negative integer number of decimal places. Defaults to `2`.\n * @param rounding - The Decimal.js rounding mode. Defaults to `Decimal.ROUND_DOWN`, which rounds toward zero.\n *\n * @returns This instance for chaining.\n */\n setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendSchemaBuilder<\n Merge<Props, ToStringSetterSchema>,\n 'setRoundAndToFixedSetter' | ExtraOmitFields\n >;\n\n /**\n * Configures a getter that converts decimal values to strings.\n *\n * @remarks\n * Replaces the configured getter on this builder. The getter returns the value's string representation,\n * or `undefined` for `null` or `undefined`. It changes the read representation, not the stored BSON type.\n * Marks this path for the lean Decimal128-to-string plugin; the plugin does not invoke this getter.\n *\n * @returns This instance for chaining.\n */\n setToStringGetter: ExtendSchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\ninterface Decimal128ValidationSchema {\n validate: Decimal128LimitValidator[];\n}\n\ninterface ToStringGetterSchema {\n get: (value?: Types.Decimal128) => string | undefined;\n leanDecimal128ToString: true;\n}\n\ninterface ToStringSetterSchema {\n set: (value?: { toString: () => string }) => string | undefined;\n}\n\n// Constants/Variables\nconst defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';\nconst defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);\n\nfunction createLimitValidator(\n type: 'max' | 'min',\n value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>,\n): Decimal128LimitValidator {\n const [limit, message] = Array.isArray(value)\n ? value as Readonly<[Decimal128Limit, string]>\n : [\n value,\n undefined,\n ];\n\n const decimalLimit = new Decimal(limit.toString());\n if (!decimalLimit.isFinite()) throw new RangeError(`Decimal128 ${type} limit must be finite`);\n\n return {\n message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),\n [type]: decimalLimit.toString(),\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n const decimalValue = new Decimal(value.toString());\n return type === 'min' ? decimalValue.gte(decimalLimit) : decimalValue.lte(decimalLimit);\n },\n };\n}\n\nexport function decimal128SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'max' || key === 'min') {\n return (value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>) => {\n const validator = createLimitValidator(key, value);\n const validators = Array.isArray(schema.validate) ? schema.validate : [];\n schema.validate = [\n ...validators.filter(({ type }) => type !== key),\n validator,\n ];\n\n return receiver;\n };\n }\n\n if (key === 'setRoundAndToFixedSetter') {\n return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {\n schema.set = (value?: { toString: () => string }) => {\n if (value !== undefined && value !== null) {\n const decimal = Decimal.isDecimal(value) ? value : new Decimal(value.toString());\n return decimal.toFixed(places, rounding);\n }\n };\n\n return receiver;\n };\n }\n\n if (key === 'setToStringGetter') {\n schema.get = (value?: Types.Decimal128) => value?.toString();\n schema.leanDecimal128ToString = true;\n return receiver;\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Decimal128SchemaBuilder;\n}\n"],"mappings":";;;;AAuIA,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,OAAO,MAAM,UAAU;AAEjF,SAAS,qBACL,MACA,OACwB;CACxB,MAAM,CAAC,OAAO,WAAW,MAAM,QAAQ,KAAK,IACtC,QACA,CACE,OACA,KAAA,CACJ;CAEJ,MAAM,eAAe,IAAI,QAAQ,MAAM,SAAS,CAAC;CACjD,IAAI,CAAC,aAAa,SAAS,GAAG,MAAM,IAAI,WAAW,cAAc,KAAK,sBAAsB;CAE5F,OAAO;EACH,SAAS,YAAY,SAAS,QAAQ,4BAA4B;GACjE,OAAO,aAAa,SAAS;EAC9B;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAElD,MAAM,eAAe,IAAI,QAAQ,MAAM,SAAS,CAAC;GACjD,OAAO,SAAS,QAAQ,aAAa,IAAI,YAAY,IAAI,aAAa,IAAI,YAAY;EAC1F;CACJ;AACJ;AAEA,SAAgB,0BAA0B;CACtC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,SAAS,QAAQ,OACzB,QAAQ,UAAqE;GACzE,MAAM,YAAY,qBAAqB,KAAK,KAAK;GACjD,MAAM,aAAa,MAAM,QAAQ,OAAO,QAAQ,IAAI,OAAO,WAAW,CAAC;GACvE,OAAO,WAAW,CACd,GAAG,WAAW,QAAQ,EAAE,WAAW,SAAS,GAAG,GAC/C,SACJ;GAEA,OAAO;EACX;EAGJ,IAAI,QAAQ,4BACR,QAAQ,SAAiB,GAAG,WAA6B,QAAQ,eAAe;GAC5E,OAAO,OAAO,UAAuC;IACjD,IAAI,UAAU,KAAA,KAAa,UAAU,MAEjC,QADgB,QAAQ,UAAU,KAAK,IAAI,QAAQ,IAAI,QAAQ,MAAM,SAAS,CAAC,EAAA,CAChE,QAAQ,QAAQ,QAAQ;GAE/C;GAEA,OAAO;EACX;EAGJ,IAAI,QAAQ,qBAAqB;GAC7B,OAAO,OAAO,UAA6B,OAAO,SAAS;GAC3D,OAAO,yBAAyB;GAChC,OAAO;EACX;EAEA,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Readonlyable } from "../types/_internals.js";
|
|
2
|
+
import { IndexDirection, IndexOptions, Schema, Types } from "mongoose";
|
|
3
|
+
import { Merge } from "type-fest";
|
|
4
|
+
//#region src/schema-builders/double.d.ts
|
|
5
|
+
type DoubleLimit = number | Types.Double;
|
|
6
|
+
type ExtendSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<DoubleSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
7
|
+
interface BaseProps {
|
|
8
|
+
type: typeof Schema.Types.Double;
|
|
9
|
+
}
|
|
10
|
+
interface DoubleEnumValidator {
|
|
11
|
+
enumValues: (null | number | Types.Double)[];
|
|
12
|
+
message: string;
|
|
13
|
+
type: 'enum';
|
|
14
|
+
validator: (value: null | Types.Double | undefined) => boolean;
|
|
15
|
+
}
|
|
16
|
+
interface DoubleLimitValidator {
|
|
17
|
+
max?: number;
|
|
18
|
+
message: string;
|
|
19
|
+
min?: number;
|
|
20
|
+
type: 'max' | 'min';
|
|
21
|
+
validator: (value: null | Types.Double | undefined) => boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
24
|
+
default: <T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined, D extends Types.Double>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
25
|
+
default: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
27
|
+
enum: <T extends Readonlyable<(N | null)[]> | {
|
|
28
|
+
[path: string]: N | null;
|
|
29
|
+
} | {
|
|
30
|
+
message?: M;
|
|
31
|
+
values: Readonlyable<(N | null)[]>;
|
|
32
|
+
}, M extends string, N extends number | Types.Double>(value: T) => ExtendSchemaBuilder<Merge<Props, DoubleValidationSchema>, 'enum' | ExtraOmitFields>;
|
|
33
|
+
immutable: ExtendSchemaBuilder<Merge<Props, {
|
|
34
|
+
immutable: true;
|
|
35
|
+
}>, ExtraOmitFields>;
|
|
36
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
37
|
+
index: T;
|
|
38
|
+
}>, ExtraOmitFields>;
|
|
39
|
+
max: <T extends L | Readonlyable<[L, S]>, L extends DoubleLimit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, DoubleValidationSchema>, 'max' | ExtraOmitFields>;
|
|
40
|
+
min: <T extends L | Readonlyable<[L, S]>, L extends DoubleLimit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, DoubleValidationSchema>, 'min' | ExtraOmitFields>;
|
|
41
|
+
nonRequired: Props;
|
|
42
|
+
private: ExtendSchemaBuilder<Merge<Props, {
|
|
43
|
+
private: true;
|
|
44
|
+
}>, ExtraOmitFields>;
|
|
45
|
+
required: Merge<Props, {
|
|
46
|
+
required: true;
|
|
47
|
+
}>;
|
|
48
|
+
sparse: ExtendSchemaBuilder<Merge<Props, {
|
|
49
|
+
sparse: true;
|
|
50
|
+
}>, ExtraOmitFields>;
|
|
51
|
+
unique: ExtendSchemaBuilder<Merge<Props, {
|
|
52
|
+
unique: true;
|
|
53
|
+
}>, ExtraOmitFields>;
|
|
54
|
+
}
|
|
55
|
+
interface DoubleValidationSchema {
|
|
56
|
+
validate: (DoubleEnumValidator | DoubleLimitValidator)[];
|
|
57
|
+
}
|
|
58
|
+
export declare function doubleSchemaBuilder(): DoubleSchemaBuilder;
|
|
59
|
+
//#endregion
|
|
60
|
+
//# sourceMappingURL=double.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"double.d.ts","names":[],"sources":["../../src/schema-builders/double.ts"],"mappings":";;;;KAYK,uBAAuB,MAAM;KAC7B,oBACD,cAAc,WACd,kCACA,KACA,oBAAoB,OAAO,kBAC3B,wBAAwB;UAGlB;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN,6BAA6B,MAAM;EACnC;EACA;EACA,YAAY,cAAc,MAAM;;UAG1B;EACN;EACA;EACA;EACA;EACA,YAAY,cAAc,MAAM;;iBAGnB,oBAAoB,cAAc,YAAY,WAAW;EACtE,UACI,YAAY,WAAW,aAAa,wBAAwB,sBAC5D,UAAU,MAAM,QAClB,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,cAAc;KACX,eAAe;;IAChB,UAAU;IAAG,QAAQ,cAAc;KACvC,kBACA,mBAAmB,MAAM,QAC3B,OAAO,MAAM,oBACX,MAAM,OAAO,kCACJ;EAGb,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,aACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,iCACL;EAGZ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,aACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,iCACL;EAGZ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;UAGtD;EACN,WAAW,sBAAsB;;wBAqCrB,uBAkEP"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from "./base.js";
|
|
2
|
+
import { Schema } from "mongoose";
|
|
3
|
+
//#region src/schema-builders/double.ts
|
|
4
|
+
const defaultEnumValidateMessage = "Path `{PATH}` ({VALUE}) is not a valid enum value.";
|
|
5
|
+
const defaultMinValidateMessage = "Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).";
|
|
6
|
+
const defaultMaxValidateMessage = "Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).";
|
|
7
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);
|
|
8
|
+
function createLimitValidator(type, value) {
|
|
9
|
+
const [limit, message] = Array.isArray(value) ? value : [value, void 0];
|
|
10
|
+
const numericLimit = typeof limit === "number" ? limit : limit.valueOf();
|
|
11
|
+
if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);
|
|
12
|
+
return {
|
|
13
|
+
message: message ?? (type === "min" ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
14
|
+
[type]: numericLimit,
|
|
15
|
+
type,
|
|
16
|
+
validator: (value) => {
|
|
17
|
+
if (value === null || value === void 0) return true;
|
|
18
|
+
return type === "min" ? value.valueOf() >= numericLimit : value.valueOf() <= numericLimit;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function doubleSchemaBuilder() {
|
|
23
|
+
const schema = {};
|
|
24
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
25
|
+
return new Proxy(baseBuilder, { get(target, key, receiver) {
|
|
26
|
+
if (key === "enum") {
|
|
27
|
+
if (schema.validate?.some(({ type }) => type === "enum")) throw new Error("Duplicate schema attribute: enum");
|
|
28
|
+
return (value) => {
|
|
29
|
+
let enumValues;
|
|
30
|
+
let message;
|
|
31
|
+
if (Array.isArray(value)) enumValues = [...value];
|
|
32
|
+
else if ("values" in value && Array.isArray(value.values)) {
|
|
33
|
+
enumValues = [...value.values];
|
|
34
|
+
if ("message" in value && typeof value.message === "string") message = value.message;
|
|
35
|
+
} else enumValues = Object.values(value);
|
|
36
|
+
const allowedValues = new Set(enumValues.map((value) => value === null ? null : value.valueOf()));
|
|
37
|
+
const validator = {
|
|
38
|
+
enumValues,
|
|
39
|
+
message: message ?? defaultEnumValidateMessage,
|
|
40
|
+
type: "enum",
|
|
41
|
+
validator: (value) => {
|
|
42
|
+
if (value === null || value === void 0) return true;
|
|
43
|
+
return allowedValues.has(value.valueOf());
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
schema.validate = [...schema.validate ?? [], validator];
|
|
47
|
+
return receiver;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (key === "max" || key === "min") return (value) => {
|
|
51
|
+
const validator = createLimitValidator(key, value);
|
|
52
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
53
|
+
schema.validate = [...validators.filter(({ type }) => type !== key), validator];
|
|
54
|
+
return receiver;
|
|
55
|
+
};
|
|
56
|
+
return Reflect.get(target, key, receiver);
|
|
57
|
+
} });
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { doubleSchemaBuilder };
|
|
61
|
+
|
|
62
|
+
//# sourceMappingURL=double.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"double.js","names":[],"sources":["../../src/schema-builders/double.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport type {\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype DoubleLimit = number | Types.Double;\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n DoubleSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Double;\n}\n\ninterface DoubleEnumValidator {\n enumValues: (null | number | Types.Double)[];\n message: string;\n type: 'enum';\n validator: (value: null | Types.Double | undefined) => boolean;\n}\n\ninterface DoubleLimitValidator {\n max?: number;\n message: string;\n min?: number;\n type: 'max' | 'min';\n validator: (value: null | Types.Double | undefined) => boolean;\n}\n\nexport interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {\n default: <\n T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined,\n D extends Types.Double,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<(N | null)[]>\n | { [path: string]: N | null }\n | { message?: M; values: Readonlyable<(N | null)[]> },\n M extends string,\n N extends number | Types.Double,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, DoubleValidationSchema>,\n 'enum' | ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends L | Readonlyable<[L, S]>,\n L extends DoubleLimit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, DoubleValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends L | Readonlyable<[L, S]>,\n L extends DoubleLimit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, DoubleValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\ninterface DoubleValidationSchema {\n validate: (DoubleEnumValidator | DoubleLimitValidator)[];\n}\n\n// Constants/Variables\nconst defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';\nconst defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';\nconst defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);\n\nfunction createLimitValidator(\n type: 'max' | 'min',\n value: DoubleLimit | Readonlyable<[DoubleLimit, string]>,\n): DoubleLimitValidator {\n const [limit, message] = Array.isArray(value)\n ? value as Readonly<[DoubleLimit, string]>\n : [\n value as DoubleLimit,\n undefined,\n ];\n\n const numericLimit = typeof limit === 'number' ? limit : limit.valueOf();\n if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);\n\n return {\n message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),\n [type]: numericLimit,\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n return type === 'min' ? value.valueOf() >= numericLimit : value.valueOf() <= numericLimit;\n },\n };\n}\n\nexport function doubleSchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'enum') {\n if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {\n throw new Error('Duplicate schema attribute: enum');\n }\n\n return (\n value: Readonlyable<(null | number | Types.Double)[]>\n | { [path: string]: null | number | Types.Double }\n | { message?: string; values: Readonlyable<(null | number | Types.Double)[]> },\n ) => {\n let enumValues: (null | number | Types.Double)[];\n let message: string | undefined;\n\n if (Array.isArray(value)) enumValues = [...value];\n else if ('values' in value && Array.isArray(value.values)) {\n enumValues = [...value.values];\n if ('message' in value && typeof value.message === 'string') message = value.message;\n } else enumValues = Object.values(value);\n\n const allowedValues = new Set(\n enumValues.map((value) => value === null ? null : value.valueOf()),\n );\n\n const validator: DoubleEnumValidator = {\n enumValues,\n message: message ?? defaultEnumValidateMessage,\n type: 'enum',\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n return allowedValues.has(value.valueOf());\n },\n };\n\n schema.validate = [\n ...schema.validate ?? [],\n validator,\n ];\n\n return receiver;\n };\n }\n\n if (key === 'max' || key === 'min') {\n return (value: DoubleLimit | Readonlyable<[DoubleLimit, string]>) => {\n const validator = createLimitValidator(key, value);\n const validators = Array.isArray(schema.validate) ? schema.validate : [];\n schema.validate = [\n ...validators.filter(({ type }) => type !== key),\n validator,\n ];\n\n return receiver;\n };\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as DoubleSchemaBuilder;\n}\n"],"mappings":";;;AAiGA,MAAM,6BAA6B;AACnC,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,OAAO,MAAM,MAAM;AAE7E,SAAS,qBACL,MACA,OACoB;CACpB,MAAM,CAAC,OAAO,WAAW,MAAM,QAAQ,KAAK,IACtC,QACA,CACE,OACA,KAAA,CACJ;CAEJ,MAAM,eAAe,OAAO,UAAU,WAAW,QAAQ,MAAM,QAAQ;CACvE,IAAI,CAAC,OAAO,SAAS,YAAY,GAAG,MAAM,IAAI,WAAW,UAAU,KAAK,sBAAsB;CAE9F,OAAO;EACH,SAAS,YAAY,SAAS,QAAQ,4BAA4B;GACjE,OAAO;EACR;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAElD,OAAO,SAAS,QAAQ,MAAM,QAAQ,KAAK,eAAe,MAAM,QAAQ,KAAK;EACjF;CACJ;AACJ;AAEA,SAAgB,sBAAsB;CAClC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,QAAQ;GAChB,IAAI,OAAO,UAAU,MAAM,EAAE,WAA6B,SAAS,MAAM,GACrE,MAAM,IAAI,MAAM,kCAAkC;GAGtD,QACI,UAGC;IACD,IAAI;IACJ,IAAI;IAEJ,IAAI,MAAM,QAAQ,KAAK,GAAG,aAAa,CAAC,GAAG,KAAK;SAC3C,IAAI,YAAY,SAAS,MAAM,QAAQ,MAAM,MAAM,GAAG;KACvD,aAAa,CAAC,GAAG,MAAM,MAAM;KAC7B,IAAI,aAAa,SAAS,OAAO,MAAM,YAAY,UAAU,UAAU,MAAM;IACjF,OAAO,aAAa,OAAO,OAAO,KAAK;IAEvC,MAAM,gBAAgB,IAAI,IACtB,WAAW,KAAK,UAAU,UAAU,OAAO,OAAO,MAAM,QAAQ,CAAC,CACrE;IAEA,MAAM,YAAiC;KACnC;KACA,SAAS,WAAW;KACpB,MAAM;KACN,YAAY,UAAU;MAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;MAElD,OAAO,cAAc,IAAI,MAAM,QAAQ,CAAC;KAC5C;IACJ;IAEA,OAAO,WAAW,CACd,GAAG,OAAO,YAAY,CAAC,GACvB,SACJ;IAEA,OAAO;GACX;EACJ;EAEA,IAAI,QAAQ,SAAS,QAAQ,OACzB,QAAQ,UAA6D;GACjE,MAAM,YAAY,qBAAqB,KAAK,KAAK;GACjD,MAAM,aAAa,MAAM,QAAQ,OAAO,QAAQ,IAAI,OAAO,WAAW,CAAC;GACvE,OAAO,WAAW,CACd,GAAG,WAAW,QAAQ,EAAE,WAAW,SAAS,GAAG,GAC/C,SACJ;GAEA,OAAO;EACX;EAGJ,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ"}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { BigIntSchemaBuilder, bigintSchemaBuilder } from "./bigint.js";
|
|
1
2
|
import { BooleanSchemaBuilder, booleanSchemaBuilder } from "./boolean.js";
|
|
2
3
|
import { DateSchemaBuilder, dateSchemaBuilder } from "./date.js";
|
|
3
4
|
import { Decimal128SchemaBuilder, decimal128SchemaBuilder } from "./decimal128.js";
|
|
5
|
+
import { DoubleSchemaBuilder, doubleSchemaBuilder } from "./double.js";
|
|
6
|
+
import { Int32SchemaBuilder, int32SchemaBuilder } from "./int32.js";
|
|
7
|
+
import { MixedSchemaBuilder, mixedSchemaBuilder } from "./mixed.js";
|
|
4
8
|
import { NumberSchemaBuilder, numberSchemaBuilder } from "./number.js";
|
|
5
9
|
import { ObjectIdSchemaBuilder, objectIdSchemaBuilder } from "./object-id.js";
|
|
6
10
|
import { RefSchemaBuilder, refSchemaBuilder } from "./ref.js";
|
|
7
11
|
import { StringSchemaBuilder, stringSchemaBuilder } from "./string.js";
|
|
8
|
-
|
|
12
|
+
import { UUIDSchemaBuilder, uuidSchemaBuilder } from "./uuid.js";
|
|
13
|
+
export { BigIntSchemaBuilder, BooleanSchemaBuilder, DateSchemaBuilder, Decimal128SchemaBuilder, DoubleSchemaBuilder, Int32SchemaBuilder, MixedSchemaBuilder, NumberSchemaBuilder, ObjectIdSchemaBuilder, RefSchemaBuilder, StringSchemaBuilder, UUIDSchemaBuilder, bigintSchemaBuilder as bigint, bigintSchemaBuilder, booleanSchemaBuilder as boolean, booleanSchemaBuilder, dateSchemaBuilder as date, dateSchemaBuilder, decimal128SchemaBuilder as decimal128, decimal128SchemaBuilder, doubleSchemaBuilder as double, doubleSchemaBuilder, int32SchemaBuilder as int32, int32SchemaBuilder, mixedSchemaBuilder as mixed, mixedSchemaBuilder, numberSchemaBuilder as number, numberSchemaBuilder, objectIdSchemaBuilder as objectId, objectIdSchemaBuilder, refSchemaBuilder as ref, refSchemaBuilder, stringSchemaBuilder as string, stringSchemaBuilder, uuidSchemaBuilder as uuid, uuidSchemaBuilder };
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { bigintSchemaBuilder } from "./bigint.js";
|
|
1
2
|
import { booleanSchemaBuilder } from "./boolean.js";
|
|
2
3
|
import { dateSchemaBuilder } from "./date.js";
|
|
3
4
|
import { decimal128SchemaBuilder } from "./decimal128.js";
|
|
5
|
+
import { doubleSchemaBuilder } from "./double.js";
|
|
6
|
+
import { int32SchemaBuilder } from "./int32.js";
|
|
7
|
+
import { mixedSchemaBuilder } from "./mixed.js";
|
|
4
8
|
import { numberSchemaBuilder } from "./number.js";
|
|
5
9
|
import { objectIdSchemaBuilder } from "./object-id.js";
|
|
6
10
|
import { refSchemaBuilder } from "./ref.js";
|
|
7
11
|
import { stringSchemaBuilder } from "./string.js";
|
|
8
|
-
|
|
12
|
+
import { uuidSchemaBuilder } from "./uuid.js";
|
|
13
|
+
export { bigintSchemaBuilder as bigint, bigintSchemaBuilder, booleanSchemaBuilder as boolean, booleanSchemaBuilder, dateSchemaBuilder as date, dateSchemaBuilder, decimal128SchemaBuilder as decimal128, decimal128SchemaBuilder, doubleSchemaBuilder as double, doubleSchemaBuilder, int32SchemaBuilder as int32, int32SchemaBuilder, mixedSchemaBuilder as mixed, mixedSchemaBuilder, numberSchemaBuilder as number, numberSchemaBuilder, objectIdSchemaBuilder as objectId, objectIdSchemaBuilder, refSchemaBuilder as ref, refSchemaBuilder, stringSchemaBuilder as string, stringSchemaBuilder, uuidSchemaBuilder as uuid, uuidSchemaBuilder };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Readonlyable } from "../types/_internals.js";
|
|
2
|
+
import { DefaultType, IndexDirection, IndexOptions, Schema } from "mongoose";
|
|
3
|
+
import { Merge } from "type-fest";
|
|
4
|
+
//#region src/schema-builders/int32.d.ts
|
|
5
|
+
type ExtendSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<Int32SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
6
|
+
type Int32Limit = number;
|
|
7
|
+
interface BaseProps {
|
|
8
|
+
type: typeof Schema.Types.Int32;
|
|
9
|
+
}
|
|
10
|
+
interface Int32EnumValidator {
|
|
11
|
+
enumValues: (null | number)[];
|
|
12
|
+
message: string;
|
|
13
|
+
type: 'enum';
|
|
14
|
+
validator: (value: null | number | undefined) => boolean;
|
|
15
|
+
}
|
|
16
|
+
interface Int32LimitValidator {
|
|
17
|
+
max?: number;
|
|
18
|
+
message: string;
|
|
19
|
+
min?: number;
|
|
20
|
+
type: 'max' | 'min';
|
|
21
|
+
validator: (value: null | number | undefined) => boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
24
|
+
default: <T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined, D extends number>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
25
|
+
default: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
27
|
+
enum: <T extends Readonlyable<(N | null)[]> | {
|
|
28
|
+
[path: string]: N | null;
|
|
29
|
+
} | {
|
|
30
|
+
message?: M;
|
|
31
|
+
values: Readonlyable<(N | null)[]>;
|
|
32
|
+
}, M extends string, N extends number>(value: T) => ExtendSchemaBuilder<Merge<Props, Int32ValidationSchema>, 'enum' | ExtraOmitFields>;
|
|
33
|
+
immutable: ExtendSchemaBuilder<Merge<Props, {
|
|
34
|
+
immutable: true;
|
|
35
|
+
}>, ExtraOmitFields>;
|
|
36
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<Merge<Props, {
|
|
37
|
+
index: T;
|
|
38
|
+
}>, ExtraOmitFields>;
|
|
39
|
+
max: <T extends L | Readonlyable<[L, S]>, L extends Int32Limit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, Int32ValidationSchema>, 'max' | ExtraOmitFields>;
|
|
40
|
+
min: <T extends L | Readonlyable<[L, S]>, L extends Int32Limit, S extends string>(value: T) => ExtendSchemaBuilder<Merge<Props, Int32ValidationSchema>, 'min' | ExtraOmitFields>;
|
|
41
|
+
nonRequired: Props;
|
|
42
|
+
private: ExtendSchemaBuilder<Merge<Props, {
|
|
43
|
+
private: true;
|
|
44
|
+
}>, ExtraOmitFields>;
|
|
45
|
+
required: Merge<Props, {
|
|
46
|
+
required: true;
|
|
47
|
+
}>;
|
|
48
|
+
sparse: ExtendSchemaBuilder<Merge<Props, {
|
|
49
|
+
sparse: true;
|
|
50
|
+
}>, ExtraOmitFields>;
|
|
51
|
+
unique: ExtendSchemaBuilder<Merge<Props, {
|
|
52
|
+
unique: true;
|
|
53
|
+
}>, ExtraOmitFields>;
|
|
54
|
+
}
|
|
55
|
+
interface Int32ValidationSchema {
|
|
56
|
+
validate: (Int32EnumValidator | Int32LimitValidator)[];
|
|
57
|
+
}
|
|
58
|
+
export declare function int32SchemaBuilder(): Int32SchemaBuilder;
|
|
59
|
+
//#endregion
|
|
60
|
+
//# sourceMappingURL=int32.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int32.d.ts","names":[],"sources":["../../src/schema-builders/int32.ts"],"mappings":";;;;KAYK,oBACD,cAAc,WACd,kCACA,KACA,mBAAmB,OAAO,kBAC1B,wBAAwB;KAGvB;UAEK;EACN,aAAa,OAAO,MAAM;;UAGpB;EACN;EACA;EACA;EACA,YAAY;;UAGN;EACN;EACA;EACA;EACA;EACA,YAAY;;iBAGC,mBAAmB,cAAc,YAAY,WAAW;EACrE,UACI,YAAY,WAAW,aAAa,YAAY,yBAAyB,YAAY,uBACrF,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UACE,cAAc;KACX,eAAe;;IAChB,UAAU;IAAG,QAAQ,cAAc;KACvC,kBACA,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,iCACJ;EAGb,WAAW,oBAAoB,MAAM;IAAS;MAAoB;EAClE,QAAQ,oBAAoB,iBAAiB,cAAc,OAAO,MAAM,oBACpE,MAAM;IAAS,OAAO;MACtB;EAGJ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,YACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,gCACL;EAGZ,MACI,UAAU,IAAI,cAAc,GAAG,KAC/B,UAAU,YACV,kBACF,OAAO,MAAM,oBACX,MAAM,OAAO,gCACL;EAGZ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;;UAGtD;EACN,WAAW,qBAAqB;;wBAoCpB,sBA2DP"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createBaseSchemaBuilderFactory } from "./base.js";
|
|
2
|
+
import { Schema } from "mongoose";
|
|
3
|
+
//#region src/schema-builders/int32.ts
|
|
4
|
+
const defaultEnumValidateMessage = "Path `{PATH}` ({VALUE}) is not a valid enum value.";
|
|
5
|
+
const defaultMinValidateMessage = "Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).";
|
|
6
|
+
const defaultMaxValidateMessage = "Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).";
|
|
7
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);
|
|
8
|
+
function createLimitValidator(type, value) {
|
|
9
|
+
const [numericLimit, message] = Array.isArray(value) ? value : [value, void 0];
|
|
10
|
+
if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);
|
|
11
|
+
return {
|
|
12
|
+
message: message ?? (type === "min" ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
13
|
+
[type]: numericLimit,
|
|
14
|
+
type,
|
|
15
|
+
validator: (value) => {
|
|
16
|
+
if (value === null || value === void 0) return true;
|
|
17
|
+
return type === "min" ? value >= numericLimit : value <= numericLimit;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function int32SchemaBuilder() {
|
|
22
|
+
const schema = {};
|
|
23
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
24
|
+
return new Proxy(baseBuilder, { get(target, key, receiver) {
|
|
25
|
+
if (key === "enum") {
|
|
26
|
+
if (schema.validate?.some(({ type }) => type === "enum")) throw new Error("Duplicate schema attribute: enum");
|
|
27
|
+
return (value) => {
|
|
28
|
+
let enumValues;
|
|
29
|
+
let message;
|
|
30
|
+
if (Array.isArray(value)) enumValues = [...value];
|
|
31
|
+
else if ("values" in value && Array.isArray(value.values)) {
|
|
32
|
+
enumValues = [...value.values];
|
|
33
|
+
if ("message" in value && typeof value.message === "string") message = value.message;
|
|
34
|
+
} else enumValues = Object.values(value);
|
|
35
|
+
const allowedValues = new Set(enumValues);
|
|
36
|
+
const validator = {
|
|
37
|
+
enumValues,
|
|
38
|
+
message: message ?? defaultEnumValidateMessage,
|
|
39
|
+
type: "enum",
|
|
40
|
+
validator: (value) => value === null || value === void 0 || allowedValues.has(value)
|
|
41
|
+
};
|
|
42
|
+
schema.validate = [...schema.validate ?? [], validator];
|
|
43
|
+
return receiver;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (key === "max" || key === "min") return (value) => {
|
|
47
|
+
const validator = createLimitValidator(key, value);
|
|
48
|
+
const validators = Array.isArray(schema.validate) ? schema.validate : [];
|
|
49
|
+
schema.validate = [...validators.filter(({ type }) => type !== key), validator];
|
|
50
|
+
return receiver;
|
|
51
|
+
};
|
|
52
|
+
return Reflect.get(target, key, receiver);
|
|
53
|
+
} });
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { int32SchemaBuilder };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=int32.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int32.js","names":[],"sources":["../../src/schema-builders/int32.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Int32SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ntype Int32Limit = number;\n\ninterface BaseProps {\n type: typeof Schema.Types.Int32;\n}\n\ninterface Int32EnumValidator {\n enumValues: (null | number)[];\n message: string;\n type: 'enum';\n validator: (value: null | number | undefined) => boolean;\n}\n\ninterface Int32LimitValidator {\n max?: number;\n message: string;\n min?: number;\n type: 'max' | 'min';\n validator: (value: null | number | undefined) => boolean;\n}\n\nexport interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,\n D extends number,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<(N | null)[]>\n | { [path: string]: N | null }\n | { message?: M; values: Readonlyable<(N | null)[]> },\n M extends string,\n N extends number,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Int32ValidationSchema>,\n 'enum' | ExtraOmitFields\n >;\n\n immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n max: <\n T extends L | Readonlyable<[L, S]>,\n L extends Int32Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Int32ValidationSchema>,\n 'max' | ExtraOmitFields\n >;\n\n min: <\n T extends L | Readonlyable<[L, S]>,\n L extends Int32Limit,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, Int32ValidationSchema>,\n 'min' | ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\ninterface Int32ValidationSchema {\n validate: (Int32EnumValidator | Int32LimitValidator)[];\n}\n\n// Constants/Variables\nconst defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';\nconst defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';\nconst defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);\n\nfunction createLimitValidator(\n type: 'max' | 'min',\n value: Int32Limit | Readonlyable<[Int32Limit, string]>,\n): Int32LimitValidator {\n const [numericLimit, message] = Array.isArray(value)\n ? value as Readonly<[Int32Limit, string]>\n : [\n value as Int32Limit,\n undefined,\n ];\n\n if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);\n\n return {\n message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),\n [type]: numericLimit,\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n\n return type === 'min' ? value >= numericLimit : value <= numericLimit;\n },\n };\n}\n\nexport function int32SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'enum') {\n if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {\n throw new Error('Duplicate schema attribute: enum');\n }\n\n return (\n value: Readonlyable<(null | number)[]>\n | { [path: string]: null | number }\n | { message?: string; values: Readonlyable<(null | number)[]> },\n ) => {\n let enumValues: (null | number)[];\n let message: string | undefined;\n\n if (Array.isArray(value)) enumValues = [...value];\n else if ('values' in value && Array.isArray(value.values)) {\n enumValues = [...value.values];\n if ('message' in value && typeof value.message === 'string') message = value.message;\n } else enumValues = Object.values(value);\n\n const allowedValues = new Set(enumValues);\n const validator: Int32EnumValidator = {\n enumValues,\n message: message ?? defaultEnumValidateMessage,\n type: 'enum',\n validator: (value) => value === null || value === undefined || allowedValues.has(value),\n };\n\n schema.validate = [\n ...schema.validate ?? [],\n validator,\n ];\n\n return receiver;\n };\n }\n\n if (key === 'max' || key === 'min') {\n return (value: Int32Limit | Readonlyable<[Int32Limit, string]>) => {\n const validator = createLimitValidator(key, value);\n const validators = Array.isArray(schema.validate) ? schema.validate : [];\n schema.validate = [\n ...validators.filter(({ type }) => type !== key),\n validator,\n ];\n\n return receiver;\n };\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Int32SchemaBuilder;\n}\n"],"mappings":";;;AAkGA,MAAM,6BAA6B;AACnC,MAAM,4BAA4B;AAClC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,OAAO,MAAM,KAAK;AAE5E,SAAS,qBACL,MACA,OACmB;CACnB,MAAM,CAAC,cAAc,WAAW,MAAM,QAAQ,KAAK,IAC7C,QACA,CACE,OACA,KAAA,CACJ;CAEJ,IAAI,CAAC,OAAO,SAAS,YAAY,GAAG,MAAM,IAAI,WAAW,SAAS,KAAK,sBAAsB;CAE7F,OAAO;EACH,SAAS,YAAY,SAAS,QAAQ,4BAA4B;GACjE,OAAO;EACR;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAElD,OAAO,SAAS,QAAQ,SAAS,eAAe,SAAS;EAC7D;CACJ;AACJ;AAEA,SAAgB,qBAAqB;CACjC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,QAAQ;GAChB,IAAI,OAAO,UAAU,MAAM,EAAE,WAA6B,SAAS,MAAM,GACrE,MAAM,IAAI,MAAM,kCAAkC;GAGtD,QACI,UAGC;IACD,IAAI;IACJ,IAAI;IAEJ,IAAI,MAAM,QAAQ,KAAK,GAAG,aAAa,CAAC,GAAG,KAAK;SAC3C,IAAI,YAAY,SAAS,MAAM,QAAQ,MAAM,MAAM,GAAG;KACvD,aAAa,CAAC,GAAG,MAAM,MAAM;KAC7B,IAAI,aAAa,SAAS,OAAO,MAAM,YAAY,UAAU,UAAU,MAAM;IACjF,OAAO,aAAa,OAAO,OAAO,KAAK;IAEvC,MAAM,gBAAgB,IAAI,IAAI,UAAU;IACxC,MAAM,YAAgC;KAClC;KACA,SAAS,WAAW;KACpB,MAAM;KACN,YAAY,UAAU,UAAU,QAAQ,UAAU,KAAA,KAAa,cAAc,IAAI,KAAK;IAC1F;IAEA,OAAO,WAAW,CACd,GAAG,OAAO,YAAY,CAAC,GACvB,SACJ;IAEA,OAAO;GACX;EACJ;EAEA,IAAI,QAAQ,SAAS,QAAQ,OACzB,QAAQ,UAA2D;GAC/D,MAAM,YAAY,qBAAqB,KAAK,KAAK;GACjD,MAAM,aAAa,MAAM,QAAQ,OAAO,QAAQ,IAAI,OAAO,WAAW,CAAC;GACvE,OAAO,WAAW,CACd,GAAG,WAAW,QAAQ,EAAE,WAAW,SAAS,GAAG,GAC/C,SACJ;GAEA,OAAO;EACX;EAGJ,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ"}
|