@kikiutils/mongoose 9.0.2 → 9.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assertions.js.map +1 -1
- package/dist/plugins/assertions.js +4 -4
- package/dist/plugins/assertions.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +6 -2
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.js +5 -4
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -1
- package/dist/plugins/normalize.d.ts +3 -1
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +2 -1
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/_internals.d.ts +18 -0
- package/dist/schema-builders/_internals.d.ts.map +1 -0
- package/dist/schema-builders/_internals.js +36 -0
- package/dist/schema-builders/_internals.js.map +1 -0
- package/dist/schema-builders/bigint.d.ts +5 -25
- package/dist/schema-builders/bigint.d.ts.map +1 -1
- package/dist/schema-builders/bigint.js +9 -19
- package/dist/schema-builders/bigint.js.map +1 -1
- package/dist/schema-builders/date.d.ts +4 -9
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/date.js.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +6 -24
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +4 -6
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +5 -25
- package/dist/schema-builders/double.d.ts.map +1 -1
- package/dist/schema-builders/double.js +9 -19
- package/dist/schema-builders/double.js.map +1 -1
- package/dist/schema-builders/int32.d.ts +5 -25
- package/dist/schema-builders/int32.d.ts.map +1 -1
- package/dist/schema-builders/int32.js +9 -19
- package/dist/schema-builders/int32.js.map +1 -1
- package/dist/schema-builders/number.d.ts +4 -9
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.js.map +1 -1
- package/dist/schema-builders/object-id.d.ts +2 -7
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/object-id.js.map +1 -1
- package/dist/schema-builders/ref.d.ts +2 -7
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.js.map +1 -1
- package/dist/schema-builders/string.d.ts +5 -10
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/types/_internals.d.ts +22 -0
- package/dist/types/_internals.d.ts.map +1 -1
- package/dist/types/aggregate-paginate.d.ts +2 -1
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts +3 -1
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/options.d.ts +6 -2
- package/dist/types/options.d.ts.map +1 -1
- package/package.json +23 -29
- package/src/assertions.ts +0 -1
- package/src/plugins/assertions.ts +4 -4
- package/src/plugins/lean-decimal128-to-string.ts +18 -7
- package/src/plugins/normalize.ts +5 -2
- package/src/schema-builders/_internals.ts +64 -0
- package/src/schema-builders/bigint.ts +33 -67
- package/src/schema-builders/date.ts +7 -7
- package/src/schema-builders/decimal128.ts +25 -41
- package/src/schema-builders/double.ts +35 -67
- package/src/schema-builders/int32.ts +33 -67
- package/src/schema-builders/number.ts +7 -7
- package/src/schema-builders/object-id.ts +2 -5
- package/src/schema-builders/ref.ts +2 -5
- package/src/schema-builders/string.ts +8 -8
- package/src/types/_internals.ts +26 -0
- package/src/types/aggregate-paginate.ts +2 -1
- package/src/types/data.ts +3 -1
- package/src/types/options.ts +6 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","names":[],"sources":["../../src/schema-builders/string.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"string.d.ts","names":[],"sources":["../../src/schema-builders/string.ts"],"mappings":";;;KAoBK,oBACD,cAAc,WACd,kCACA,KACA,oBAAoB,OAAO,kBAC3B,wBAAwB;UAGlB;EACN,MAAM;;UAGA,SAAS;EACf;EACA;IAAY,SAAS;IAAG,YAAY;;;iBAGvB,oBACb;EAAgB,MAAM;;EAA6B,MAAM;GACzD;EAEA,UACI,YAAY,WAAW,aAAa,YAAY,MAAM,YAAY,WAClE,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,SAAS;MACxB;EAGJ,OACI,UAAU,iBAAiB,GAAG,IAC9B,kBACA,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;;;;;;;;;;;EAaJ,OAAO,0BAA0B,4BAA4B,UAAU,MAAM,oBACzE,MAAM,OAAO,SAAS,+BACI;;;;;;;;;;;EAa9B,OAAO,0BAA0B,4BAA4B,UAAU,MAAM,oBACzE,MAAM,OAAO,SAAS,+BACI;;;;;;;;;;;;EAc9B,SACI,UAAU,iBAAiB,GAAG,IAC9B,kBACA,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,WAAW;IAAG,WAAW;MACxC;EAGJ,WAAW,oBAAoB,MAAM;IAAS;MAAoB;;;;;;;;;;;;EAalE,QACI,UAAU,QACV,0CACF,OAAO,GACL,UAAU,MAAM,oBAChB,MAAM;IAAS,OAAO,mBAAmB,UAAU,GAAG,MAAM;MAC5D;EAEJ,YACI,UAAU,iBAAiB,GAAG,IAC9B,kBACA,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,WAAW;MAC1B;EAGJ,YACI,UAAU,iBAAiB,GAAG,IAC9B,kBACA,kBACF,OAAO,MAAM,oBACX,MAAM;IAAS,WAAW;MAC1B;EAGJ,aAAa;EACb,SAAS,oBAAoB,MAAM;IAAS;MAAkB;EAC9D,UAAU,MAAM;IAAS;;EACzB,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,MAAM,oBAAoB,MAAM;IAAS;MAAe;EACxD,MAAM,oBAAoB,MAAM;IAAS;MAAe;EACxD,QAAQ,oBAAoB,MAAM;IAAS;MAAiB;EAC5D,WAAW,oBAAoB,MAAM;IAAS;MAAoB;;;;;;;;;;;;EAalE,MAAM,0BAA0B,2BAA2B,UAAU,MAAM,oBACvE,MAAM,OAAO,UAAU,+BACG;;UAIxB,UAAU;EAChB;IAAY,SAAS;IAAG,YAAY;;;cAIlC;cACA;cACA;wBAKU,uBAiEP"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.js","names":[],"sources":["../../src/schema-builders/string.ts"],"sourcesContent":["import {\n isIPv4,\n isIPv6,\n} from 'node:net';\n\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n StringSchemaDefinition,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"string.js","names":[],"sources":["../../src/schema-builders/string.ts"],"sourcesContent":["import {\n isIPv4,\n isIPv6,\n} from 'node:net';\n\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n StringSchemaDefinition,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type {\n EnumInput,\n ValueWithMessage,\n} from '../types/_internals';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\ntype ExtendSchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n StringSchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: StringSchemaDefinition;\n}\n\ninterface IpSchema<T extends string> {\n trim: true;\n validate: { message: T; validator: (value: string) => boolean };\n}\n\nexport interface StringSchemaBuilder<\n Props extends { type: StringSchemaDefinition } = { type: StringSchemaDefinition },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends EnumInput<null | S, M>,\n M extends string,\n S 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 /**\n * Adds IPv4 address validation and enables input trimming.\n *\n * @remarks\n * Replaces the configured custom validator and sets `trim` to `true` on this builder.\n *\n * @param message - The validation error message. Defaults to the built-in IPv4 validation message.\n *\n * @returns This instance for chaining.\n */\n ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendSchemaBuilder<\n Merge<Props, IpSchema<T>>,\n 'ipv4' | 'ipv6' | 'url' | ExtraOmitFields\n >;\n\n /**\n * Adds IPv6 address validation and enables input trimming.\n *\n * @remarks\n * Replaces the configured custom validator and sets `trim` to `true` on this builder.\n *\n * @param message - The validation error message. Defaults to the built-in IPv6 validation message.\n *\n * @returns This instance for chaining.\n */\n ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendSchemaBuilder<\n Merge<Props, IpSchema<T>>,\n 'ipv4' | 'ipv6' | 'url' | ExtraOmitFields\n >;\n\n /**\n * Sets equal minimum and maximum string lengths.\n *\n * @remarks\n * Assigns the same value to `minlength` and `maxlength`, replacing any previously configured limits.\n *\n * @param value - The exact length in UTF-16 code units, or a tuple containing the length and a validation\n * error message.\n *\n * @returns This instance for chaining.\n */\n length: <\n T extends ValueWithMessage<L, S>,\n L extends number,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { maxlength: T; minlength: T }>,\n ExtraOmitFields\n >;\n\n lowercase: ExtendSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;\n\n /**\n * Configures regular expression validation for the string field.\n *\n * @remarks\n * Replaces any previously configured `match` option.\n *\n * @param regex - The regular expression used by Mongoose to validate the string.\n * @param message - The validation error message. If omitted, Mongoose uses its default message.\n *\n * @returns This instance for chaining.\n */\n match: <\n T extends RegExp,\n M extends string | undefined = undefined,\n >(regex: T,\n message?: M) => ExtendSchemaBuilder<\n Merge<Props, { match: M extends string ? Readonly<[T, M]> : T }>,\n ExtraOmitFields\n >;\n maxlength: <\n T extends ValueWithMessage<L, S>,\n L extends number,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { maxlength: T }>,\n ExtraOmitFields\n >;\n\n minlength: <\n T extends ValueWithMessage<L, S>,\n L extends number,\n S extends string,\n >(value: T) => ExtendSchemaBuilder<\n Merge<Props, { minlength: 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 text: ExtendSchemaBuilder<Merge<Props, { text: true }>, ExtraOmitFields>;\n trim: ExtendSchemaBuilder<Merge<Props, { trim: true }>, ExtraOmitFields>;\n unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n uppercase: ExtendSchemaBuilder<Merge<Props, { uppercase: true }>, ExtraOmitFields>;\n\n /**\n * Adds WHATWG URL parseability validation.\n *\n * @remarks\n * Accepts URLs with credentials and non-HTTP schemes. Performs no DNS lookup or stored-value normalization,\n * and does not enable trimming. Replaces the configured custom validator on this builder.\n *\n * @param message - The validation error message. Defaults to the built-in URL validation message.\n *\n * @returns This instance for chaining.\n */\n url: <T extends string = typeof defaultUrlValidateMessage>(message?: T) => ExtendSchemaBuilder<\n Merge<Props, UrlSchema<T>>,\n 'ipv4' | 'ipv6' | 'url' | ExtraOmitFields\n >;\n}\n\ninterface UrlSchema<T extends string> {\n validate: { message: T; validator: (value: string) => boolean };\n}\n\n// Constants/Variables\nconst defaultIpv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`';\nconst defaultIpv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`';\nconst defaultUrlValidateMessage = '`{VALUE}` is not a valid URL for path `{PATH}`';\n\n// Functions\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(String);\n\nexport function stringSchemaBuilder() {\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 === 'ipv4') {\n return (message: string = defaultIpv4ValidateMessage) => {\n schema.trim = true;\n schema.validate = {\n message,\n validator: (value: string) => isIPv4(value),\n };\n\n return receiver;\n };\n }\n\n if (key === 'ipv6') {\n return (message: string = defaultIpv6ValidateMessage) => {\n schema.trim = true;\n schema.validate = {\n message,\n validator: (value: string) => isIPv6(value),\n };\n\n return receiver;\n };\n }\n\n if (key === 'length') {\n return (value: any) => {\n schema.maxlength = schema.minlength = value;\n return receiver;\n };\n }\n\n if (key === 'match') {\n return (regex: RegExp, message?: string) => {\n schema.match = message === undefined\n ? regex\n : [\n regex,\n message,\n ];\n\n return receiver;\n };\n }\n\n if (key === 'url') {\n return (message: string = defaultUrlValidateMessage) => {\n schema.validate = {\n message,\n validator: (value: string) => URL.canParse(value),\n };\n\n return receiver;\n };\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as StringSchemaBuilder;\n}\n"],"mappings":";;;AAwLA,MAAM,6BAA6B;AACnC,MAAM,6BAA6B;AACnC,MAAM,4BAA4B;AAGlC,MAAM,qBAAqB,+BAA+B,MAAM;AAEhE,SAAgB,sBAAsB;CAClC,MAAM,SAA8B,CAAC;CACrC,MAAM,cAAc,mBAAmB,MAAM;CAC7C,OAAO,IAAI,MACP,aACA,EACI,IAAI,QAAQ,KAAK,UAAU;EACvB,IAAI,QAAQ,QACR,QAAQ,UAAkB,+BAA+B;GACrD,OAAO,OAAO;GACd,OAAO,WAAW;IACd;IACA,YAAY,UAAkB,OAAO,KAAK;GAC9C;GAEA,OAAO;EACX;EAGJ,IAAI,QAAQ,QACR,QAAQ,UAAkB,+BAA+B;GACrD,OAAO,OAAO;GACd,OAAO,WAAW;IACd;IACA,YAAY,UAAkB,OAAO,KAAK;GAC9C;GAEA,OAAO;EACX;EAGJ,IAAI,QAAQ,UACR,QAAQ,UAAe;GACnB,OAAO,YAAY,OAAO,YAAY;GACtC,OAAO;EACX;EAGJ,IAAI,QAAQ,SACR,QAAQ,OAAe,YAAqB;GACxC,OAAO,QAAQ,YAAY,KAAA,IACrB,QACA,CACE,OACA,OACJ;GAEJ,OAAO;EACX;EAGJ,IAAI,QAAQ,OACR,QAAQ,UAAkB,8BAA8B;GACpD,OAAO,WAAW;IACd;IACA,YAAY,UAAkB,IAAI,SAAS,KAAK;GACpD;GAEA,OAAO;EACX;EAGJ,OAAO,QAAQ,IAAI,QAAQ,KAAK,QAAQ;CAC5C,EACJ,CACJ;AACJ"}
|
|
@@ -1,2 +1,24 @@
|
|
|
1
|
+
export type EnumInput<T, M extends string = string> = EnumOptions<T, M> | Readonlyable<T[]> | Record<string, T>;
|
|
1
2
|
export type Readonlyable<T> = Readonly<T> | T;
|
|
3
|
+
export type ValueWithMessage<T, M extends string = string> = Readonlyable<[T, M]> | T;
|
|
4
|
+
export interface EnumOptions<T, M extends string = string> {
|
|
5
|
+
message?: M;
|
|
6
|
+
values: Readonlyable<T[]>;
|
|
7
|
+
}
|
|
8
|
+
export interface EnumValidator<T, V = T> {
|
|
9
|
+
enumValues: Array<null | T>;
|
|
10
|
+
message: string;
|
|
11
|
+
type: 'enum';
|
|
12
|
+
validator: (value: null | undefined | V) => boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface LimitValidator<L, V = L> {
|
|
15
|
+
max?: L;
|
|
16
|
+
message: string;
|
|
17
|
+
min?: L;
|
|
18
|
+
type: 'max' | 'min';
|
|
19
|
+
validator: (value: null | undefined | V) => boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface ValidationSchema<T> {
|
|
22
|
+
validate: T[];
|
|
23
|
+
}
|
|
2
24
|
//# sourceMappingURL=_internals.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_internals.d.ts","names":[],"sources":["../../src/types/_internals.ts"],"mappings":"A,YAAY,aAAa,KAAK,SAAS,KAAK"}
|
|
1
|
+
{"version":3,"file":"_internals.d.ts","names":[],"sources":["../../src/types/_internals.ts"],"mappings":"A,YAAY,UAAU,GAAG,6BAA6B,YAAY,GAAG,KAAK,aAAa,OAAO,eAAe;YACjG,aAAa,KAAK,SAAS,KAAK;YAChC,iBAAiB,GAAG,6BAA6B,cAAc,GAAG,MAAM;iBAEnE,YAAY,GAAG;EAC5B,UAAU;EACV,QAAQ,aAAa;;iBAGR,cAAc,GAAG,IAAI;EAClC,YAAY,aAAa;EACzB;EACA;EACA,YAAY,0BAA0B;;iBAGzB,eAAe,GAAG,IAAI;EACnC,MAAM;EACN;EACA,MAAM;EACN;EACA,YAAY,0BAA0B;;iBAGzB,iBAAiB;EAC9B,UAAU"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Aggregate, Model, PipelineStage, Schema } from "mongoose";
|
|
2
|
+
import { Arrayable } from "type-fest";
|
|
2
3
|
type PrePaginatePipelineStage = '__PREPAGINATE__' | PipelineStage;
|
|
3
4
|
export interface AggregateCustomLabels<T = boolean | string | undefined> {
|
|
4
5
|
docs?: T;
|
|
@@ -56,7 +57,7 @@ export interface AggregatePaginateQueryPopulateOptions {
|
|
|
56
57
|
/**
|
|
57
58
|
* The nested population options applied to the populated documents.
|
|
58
59
|
*/
|
|
59
|
-
populate?: AggregatePaginateQueryPopulateOptions
|
|
60
|
+
populate?: Arrayable<AggregatePaginateQueryPopulateOptions>;
|
|
60
61
|
/**
|
|
61
62
|
* The field projection used when retrieving populated documents.
|
|
62
63
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregate-paginate.d.ts","names":[],"sources":["../../src/types/aggregate-paginate.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"aggregate-paginate.d.ts","names":[],"sources":["../../src/types/aggregate-paginate.ts"],"mappings":";;KAeK,+CAA+C;iBAEnC,sBAAsB;EACnC,OAAO;EACP,cAAc;EACd,cAAc;EACd,QAAQ;EACR,OAAO;EACP,WAAW;EACX,OAAO;EACP,gBAAgB;EAChB,WAAW;EACX,YAAY;EACZ,aAAa;;iBAGA,uBACb,YACA,uBACA,8CACM,MAAM,YAAY,cAAc;EACtC,oBAAoB,GAChB,QAAQ,UAAU,OAAO,4BACzB,UAAU,0BACV,YAAY,UAAU,QAAQ,wBAAwB,gBACrD,QAAQ,wBAAwB;;iBAGxB;EACb;EACA;EACA,eAAe;EACf;EACA;EACA;;;;;;;;;;EAWA;EACA;EACA;;iBAGa;;;;EAIb;;;;EAKA,QAAQ;;;;EAKR;;;;EAKA;;;;EAKA,WAAW,UAAU;;;;EAKrB;;iBAGa,wBAAwB;GACpC,gDAAgD;EACjD,MAAM;EACN;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;iBAGa,EAAE,QAAQ"}
|
package/dist/types/data.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export type BaseMongooseModelData<CreatedAtField extends boolean = true, Updated
|
|
|
7
7
|
} : unknown) & {
|
|
8
8
|
id: string;
|
|
9
9
|
};
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Converts data fields to document fields using the same `_id` rules as BaseMongooseDocType.
|
|
12
|
+
*/
|
|
11
13
|
export type DataToBaseMongooseDocType<T, ObjectIdFields extends keyof T = never, DateFields extends Exclude<keyof T, ObjectIdFields> = never, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true, IdField extends boolean = true> = BaseMongooseDocType<SetFieldsToObjectId<SetFieldType<T, DateFields, Date>, ObjectIdFields>, CreatedAtField, UpdatedAtField, IdField>;
|
|
12
14
|
//# sourceMappingURL=data.d.ts.map
|
package/dist/types/data.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.d.ts","names":[],"sources":["../../src/types/data.ts"],"mappings":";;YAOY,sBAAsB,uCAAuC,0CACpE;EAAgC;gBAChC;EAAgC;;EAC/B
|
|
1
|
+
{"version":3,"file":"data.d.ts","names":[],"sources":["../../src/types/data.ts"],"mappings":";;YAOY,sBAAsB,uCAAuC,0CACpE;EAAgC;gBAChC;EAAgC;;EAC/B;;;;;YAKM,0BACR,GACA,6BAA6B,WAC7B,mBAAmB,cAAc,GAAG,yBACpC,uCACA,uCACA,kCACA,oBACA,oBAAoB,aAAa,GAAG,YAAY,OAAO,iBACvD,gBACA,gBACA"}
|
package/dist/types/options.d.ts
CHANGED
|
@@ -5,9 +5,13 @@ import { ConnectOptions, Connection, Schema, SchemaTimestampsConfig } from "mong
|
|
|
5
5
|
export interface BuildMongooseModelOptions {
|
|
6
6
|
connection?: Connection;
|
|
7
7
|
plugins?: {
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Register lean conversion for marked Decimal128 fields. Defaults to true.
|
|
10
|
+
*/
|
|
9
11
|
leanDecimal128ToString?: boolean | MongooseLeanDecimal128ToStringPluginOptions;
|
|
10
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* Register JSON normalization. Defaults to true; an object enables it with custom options.
|
|
14
|
+
*/
|
|
11
15
|
normalize?: boolean | MongooseNormalizePluginOptions;
|
|
12
16
|
};
|
|
13
17
|
timestamps?: boolean | SchemaTimestampsConfig;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.d.ts","names":[],"sources":["../../src/types/options.ts"],"mappings":";;;;iBAYiB;EACb,aAAa;EACb
|
|
1
|
+
{"version":3,"file":"options.d.ts","names":[],"sources":["../../src/types/options.ts"],"mappings":";;;;iBAYiB;EACb,aAAa;EACb;;;;IAII,mCAAmC;;;;IAKnC,sBAAsB;;EAG1B,uBAAuB;;iBAGV;EACb,oBACI,SACA,cAAc,0BAA0B,SAAS,6BAA6B,eAC9E,sCACA,uBAEA,QAAQ,OACJ,SACA,OACA,6BACA;EAIR,2BAA2B"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "9.0.
|
|
4
|
+
"version": "9.0.4",
|
|
5
5
|
"description": "Provides Mongoose schema builders and utilities for connections, pagination, JSON normalization, and update checks.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,28 +29,21 @@
|
|
|
29
29
|
"./builders": "./dist/builders.js",
|
|
30
30
|
"./connection": "./dist/connection.js",
|
|
31
31
|
"./options": "./dist/options.js",
|
|
32
|
-
"./plugins/assertions": "./dist/plugins/assertions.js",
|
|
33
|
-
"./plugins/lean-decimal128-to-string": "./dist/plugins/lean-decimal128-to-string.js",
|
|
34
|
-
"./plugins/normalize": "./dist/plugins/normalize.js",
|
|
35
32
|
"./schema-builders": "./dist/schema-builders/index.js",
|
|
36
|
-
"./
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"./
|
|
41
|
-
"./schema-builders
|
|
42
|
-
"./
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"./schema-builders/
|
|
47
|
-
"./schema-builders/
|
|
48
|
-
"./
|
|
49
|
-
"./types":
|
|
50
|
-
"./types/aggregate-paginate": "./dist/types/aggregate-paginate.js",
|
|
51
|
-
"./types/data": "./dist/types/data.js",
|
|
52
|
-
"./types/options": "./dist/types/options.js",
|
|
53
|
-
"./types/paginate": "./dist/types/paginate.js",
|
|
33
|
+
"./types": {
|
|
34
|
+
"types": "./dist/types/index.d.ts",
|
|
35
|
+
"default": null
|
|
36
|
+
},
|
|
37
|
+
"./plugins/*": "./dist/plugins/*.js",
|
|
38
|
+
"./schema-builders/*": "./dist/schema-builders/*.js",
|
|
39
|
+
"./types/*": {
|
|
40
|
+
"types": "./dist/types/*.d.ts",
|
|
41
|
+
"default": null
|
|
42
|
+
},
|
|
43
|
+
"./schema-builders/_internals": null,
|
|
44
|
+
"./schema-builders/index": null,
|
|
45
|
+
"./types/_internals": null,
|
|
46
|
+
"./types/index": null,
|
|
54
47
|
"./package.json": "./package.json"
|
|
55
48
|
},
|
|
56
49
|
"files": [
|
|
@@ -76,27 +69,28 @@
|
|
|
76
69
|
"peerDependenciesMeta": {},
|
|
77
70
|
"dependencies": {
|
|
78
71
|
"decimal.js": "^10.6.0",
|
|
79
|
-
"es-toolkit": "^1.53.
|
|
80
|
-
"mongoose": "^9.
|
|
72
|
+
"es-toolkit": "^1.53.2",
|
|
73
|
+
"mongoose": "^9.12.1",
|
|
81
74
|
"mongoose-aggregate-paginate-v2": "^1.1.5",
|
|
82
75
|
"mongoose-paginate-v2": "^1.9.5",
|
|
76
|
+
"ts-extras": "^2.0.0",
|
|
83
77
|
"type-fest": "^5.10.0"
|
|
84
78
|
},
|
|
85
79
|
"devDependencies": {
|
|
86
80
|
"@antfu/eslint-config": "^10.0.0",
|
|
87
81
|
"@arethetypeswrong/core": "^0.18.5",
|
|
88
|
-
"@kikiutils/eslint-config": "^
|
|
82
|
+
"@kikiutils/eslint-config": "^11.0.1",
|
|
89
83
|
"@kikiutils/tsconfigs": "^7.0.0",
|
|
90
84
|
"@types/node": "^26.6.6",
|
|
91
|
-
"@vitest/coverage-v8": "^5.0
|
|
85
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
92
86
|
"changelogen": "^0.6.2",
|
|
93
87
|
"eslint": "^10.14.0",
|
|
94
|
-
"knip": "^6.
|
|
88
|
+
"knip": "^6.45.1",
|
|
95
89
|
"mongoose-lean-getters": "^2.3.1",
|
|
96
90
|
"publint": "^0.3.25",
|
|
97
|
-
"tsdown": "^0.23.
|
|
91
|
+
"tsdown": "^0.23.7",
|
|
98
92
|
"typescript": "^6.0.3",
|
|
99
93
|
"unplugin-unused": "^0.6.0",
|
|
100
|
-
"vitest": "^5.0
|
|
94
|
+
"vitest": "^5.1.0"
|
|
101
95
|
}
|
|
102
96
|
}
|
package/src/assertions.ts
CHANGED
|
@@ -10,7 +10,6 @@ export async function assertMongooseUpdateSuccess<T extends Document>(
|
|
|
10
10
|
const updateResult = await updatePromise;
|
|
11
11
|
if (!updateResult.acknowledged) throw new Error('Update was not acknowledged');
|
|
12
12
|
if (updateResult.modifiedCount < expectedModifiedCount) {
|
|
13
|
-
// eslint-disable-next-line style/max-len
|
|
14
13
|
throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}`);
|
|
15
14
|
}
|
|
16
15
|
}
|
|
@@ -53,24 +53,24 @@ declare module 'mongoose' {
|
|
|
53
53
|
export function mongooseAssertionsPlugin<S extends Schema>(schema: S) {
|
|
54
54
|
schema.method(
|
|
55
55
|
'assertUpdateSuccess',
|
|
56
|
-
function (
|
|
56
|
+
async function (
|
|
57
57
|
update?: UpdateQuery<any> | UpdateWithAggregationPipeline,
|
|
58
58
|
options?: null | QueryOptions,
|
|
59
59
|
expectedModifiedCount?: number,
|
|
60
60
|
) {
|
|
61
|
-
return assertMongooseUpdateSuccess(this.updateOne(update, options), expectedModifiedCount);
|
|
61
|
+
return await assertMongooseUpdateSuccess(this.updateOne(update, options), expectedModifiedCount);
|
|
62
62
|
},
|
|
63
63
|
);
|
|
64
64
|
|
|
65
65
|
schema.static(
|
|
66
66
|
'assertUpdateSuccess',
|
|
67
|
-
function (
|
|
67
|
+
async function (
|
|
68
68
|
filter: QueryFilter<any>,
|
|
69
69
|
update: UpdateQuery<any> | UpdateWithAggregationPipeline,
|
|
70
70
|
options?: (mongo.UpdateOptions & MongooseUpdateQueryOptions<any>) | null,
|
|
71
71
|
expectedModifiedCount: number = 1,
|
|
72
72
|
) {
|
|
73
|
-
return assertMongooseUpdateSuccess(
|
|
73
|
+
return await assertMongooseUpdateSuccess(
|
|
74
74
|
expectedModifiedCount === 1
|
|
75
75
|
? this.updateOne(filter, update, options)
|
|
76
76
|
: this.updateMany(filter, update, options),
|
|
@@ -2,11 +2,19 @@ import type {
|
|
|
2
2
|
Query,
|
|
3
3
|
Schema,
|
|
4
4
|
SchemaType,
|
|
5
|
+
Types,
|
|
5
6
|
} from 'mongoose';
|
|
7
|
+
import {
|
|
8
|
+
objectHasOwn,
|
|
9
|
+
objectKeys,
|
|
10
|
+
objectValues,
|
|
11
|
+
} from 'ts-extras';
|
|
6
12
|
|
|
7
13
|
declare module 'mongoose' {
|
|
8
14
|
interface LeanOptions {
|
|
9
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Override conversion of marked Decimal128 fields. Defaults to the plugin's enabledByDefault setting.
|
|
17
|
+
*/
|
|
10
18
|
decimal128ToString?: boolean;
|
|
11
19
|
}
|
|
12
20
|
}
|
|
@@ -14,7 +22,9 @@ declare module 'mongoose' {
|
|
|
14
22
|
type QueryWithOptionalMap = Query<unknown, unknown> & { map?: (transform: (result: unknown) => unknown) => unknown };
|
|
15
23
|
|
|
16
24
|
export interface MongooseLeanDecimal128ToStringPluginOptions {
|
|
17
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Whether marked fields are converted on lean queries by default. Defaults to true.
|
|
27
|
+
*/
|
|
18
28
|
enabledByDefault?: boolean;
|
|
19
29
|
}
|
|
20
30
|
|
|
@@ -27,7 +37,7 @@ const isNonNullObject = (value: unknown): value is Record<string, any> => value
|
|
|
27
37
|
function convertDocument(schema: Schema, value: unknown) {
|
|
28
38
|
if (!isNonNullObject(value)) return;
|
|
29
39
|
const discriminatorKey = schema.get('discriminatorKey') ?? '__t';
|
|
30
|
-
const discriminatorSchema =
|
|
40
|
+
const discriminatorSchema = objectValues(schema.discriminators ?? {}).find(
|
|
31
41
|
(childSchema) => childSchema.get('discriminatorKey') === discriminatorKey
|
|
32
42
|
&& (childSchema as Schema & { discriminatorMapping?: { value: unknown } }).discriminatorMapping?.value
|
|
33
43
|
=== value[discriminatorKey],
|
|
@@ -50,7 +60,7 @@ function convertPath(value: unknown, pathSegments: string[], schemaType: SchemaT
|
|
|
50
60
|
|
|
51
61
|
if (!isNonNullObject(value)) return;
|
|
52
62
|
const [key, ...remainingPathSegments] = pathSegments;
|
|
53
|
-
if (key === undefined || !
|
|
63
|
+
if (key === undefined || !objectHasOwn(value, key)) return;
|
|
54
64
|
if (remainingPathSegments.length) convertPath(value[key], remainingPathSegments, schemaType);
|
|
55
65
|
else value[key] = convertValue(value[key], schemaType);
|
|
56
66
|
}
|
|
@@ -60,7 +70,7 @@ function convertValue(value: unknown, schemaType: SchemaType): unknown {
|
|
|
60
70
|
const embeddedSchemaType = schemaType.getEmbeddedSchemaType();
|
|
61
71
|
if (embeddedSchemaType && Array.isArray(value)) {
|
|
62
72
|
for (let i = 0; i < value.length; i++) {
|
|
63
|
-
if (
|
|
73
|
+
if (objectHasOwn(value, i)) value[i] = convertValue(value[i], embeddedSchemaType);
|
|
64
74
|
}
|
|
65
75
|
|
|
66
76
|
return value;
|
|
@@ -69,7 +79,7 @@ function convertValue(value: unknown, schemaType: SchemaType): unknown {
|
|
|
69
79
|
if (embeddedSchemaType && schemaType.instance === 'Map') {
|
|
70
80
|
if (value instanceof Map) value.forEach((item, key) => value.set(key, convertValue(item, embeddedSchemaType)));
|
|
71
81
|
else if (isNonNullObject(value)) {
|
|
72
|
-
for (const key of
|
|
82
|
+
for (const key of objectKeys(value)) value[key] = convertValue(value[key], embeddedSchemaType);
|
|
73
83
|
}
|
|
74
84
|
|
|
75
85
|
return value;
|
|
@@ -86,7 +96,7 @@ function convertValue(value: unknown, schemaType: SchemaType): unknown {
|
|
|
86
96
|
&& isNonNullObject(value)
|
|
87
97
|
&& value._bsontype === 'Decimal128'
|
|
88
98
|
&& typeof value.toString === 'function'
|
|
89
|
-
) return value.toString();
|
|
99
|
+
) return (value as Types.Decimal128).toString();
|
|
90
100
|
|
|
91
101
|
return value;
|
|
92
102
|
}
|
|
@@ -121,6 +131,7 @@ export function mongooseLeanDecimal128ToStringPlugin(
|
|
|
121
131
|
const querySchema = query.model.schema;
|
|
122
132
|
if (Array.isArray(document)) document.forEach((value) => convertDocument(querySchema, value));
|
|
123
133
|
else convertDocument(querySchema, document);
|
|
134
|
+
|
|
124
135
|
return result;
|
|
125
136
|
}
|
|
126
137
|
|
package/src/plugins/normalize.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
} from 'es-toolkit/compat';
|
|
6
6
|
import { Types } from 'mongoose';
|
|
7
7
|
import type { Schema } from 'mongoose';
|
|
8
|
+
import { objectEntries } from 'ts-extras';
|
|
8
9
|
|
|
9
10
|
export interface MongooseNormalizePluginOptions {
|
|
10
11
|
/**
|
|
@@ -17,7 +18,9 @@ export interface MongooseNormalizePluginOptions {
|
|
|
17
18
|
*/
|
|
18
19
|
convertIdField?: boolean;
|
|
19
20
|
|
|
20
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Register normalization on child schemas too. Defaults to true.
|
|
23
|
+
*/
|
|
21
24
|
recursive?: boolean;
|
|
22
25
|
|
|
23
26
|
/**
|
|
@@ -63,7 +66,7 @@ export function mongooseNormalizePlugin<S extends Schema>(
|
|
|
63
66
|
// Collect special paths (Decimal128 & private)
|
|
64
67
|
const decimalPaths: string[] = [];
|
|
65
68
|
const privatePaths: string[] = [];
|
|
66
|
-
for (const [path, schemaType] of
|
|
69
|
+
for (const [path, schemaType] of objectEntries(schema.paths)) {
|
|
67
70
|
if (schemaType?.options?.private) privatePaths.push(path);
|
|
68
71
|
else if (schemaType?.instance === 'Decimal128') decimalPaths.push(path);
|
|
69
72
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { objectValues } from 'ts-extras';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
EnumInput,
|
|
5
|
+
EnumOptions,
|
|
6
|
+
Readonlyable,
|
|
7
|
+
ValidationSchema,
|
|
8
|
+
ValueWithMessage,
|
|
9
|
+
} from '../types/_internals';
|
|
10
|
+
|
|
11
|
+
// Constants/Variables
|
|
12
|
+
export const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
|
|
13
|
+
export const defaultLimitValidateMessages = {
|
|
14
|
+
max: 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).',
|
|
15
|
+
min: 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Functions
|
|
19
|
+
export function hasSchemaValidator(schema: Partial<ValidationSchema<{ type: string }>>, type: string): boolean {
|
|
20
|
+
return schema.validate?.some((validator) => validator.type === type) ?? false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isEnumArray<T>(value: EnumInput<T>): value is readonly T[] {
|
|
24
|
+
return Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isEnumOptions<T>(value: EnumOptions<T> | Record<string, T>): value is EnumOptions<T> {
|
|
28
|
+
return 'values' in value && Array.isArray(value.values);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isValueMessageTuple<T>(value: ValueWithMessage<T>): value is Readonlyable<[T, string]> {
|
|
32
|
+
return Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function normalizeEnumValues<T>(value: EnumInput<T>): { enumValues: T[]; message?: string } {
|
|
36
|
+
if (isEnumArray(value)) return { enumValues: [...value] };
|
|
37
|
+
if (isEnumOptions(value)) {
|
|
38
|
+
return {
|
|
39
|
+
enumValues: [...value.values],
|
|
40
|
+
message: typeof value.message === 'string' ? value.message : undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { enumValues: objectValues(value) };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function normalizeValueWithMessage<T>(value: ValueWithMessage<T>): readonly [T, string | undefined] {
|
|
48
|
+
return isValueMessageTuple(value)
|
|
49
|
+
? value
|
|
50
|
+
: [
|
|
51
|
+
value,
|
|
52
|
+
undefined,
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function setSchemaValidator<T extends { type: string }>(
|
|
57
|
+
schema: Partial<ValidationSchema<T>>,
|
|
58
|
+
validator: T,
|
|
59
|
+
): void {
|
|
60
|
+
schema.validate = [
|
|
61
|
+
...(schema.validate ?? []).filter(({ type }) => type !== validator.type),
|
|
62
|
+
validator,
|
|
63
|
+
];
|
|
64
|
+
}
|
|
@@ -6,11 +6,27 @@ import type {
|
|
|
6
6
|
} from 'mongoose';
|
|
7
7
|
import type { Merge } from 'type-fest';
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
|
|
9
|
+
import type {
|
|
10
|
+
EnumInput,
|
|
11
|
+
EnumValidator,
|
|
12
|
+
LimitValidator,
|
|
13
|
+
ValidationSchema,
|
|
14
|
+
ValueWithMessage,
|
|
15
|
+
} from '../types/_internals';
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
defaultEnumValidateMessage,
|
|
19
|
+
defaultLimitValidateMessages,
|
|
20
|
+
hasSchemaValidator,
|
|
21
|
+
normalizeEnumValues,
|
|
22
|
+
normalizeValueWithMessage,
|
|
23
|
+
setSchemaValidator,
|
|
24
|
+
} from './_internals';
|
|
11
25
|
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
26
|
|
|
13
27
|
type BigIntLimit = bigint;
|
|
28
|
+
type BigIntValidationSchema = ValidationSchema<EnumValidator<bigint> | LimitValidator<bigint>>;
|
|
29
|
+
|
|
14
30
|
type ExtendSchemaBuilder<
|
|
15
31
|
Props extends BaseProps,
|
|
16
32
|
ExtraOmitFields extends string,
|
|
@@ -23,21 +39,6 @@ interface BaseProps {
|
|
|
23
39
|
type: typeof Schema.Types.BigInt;
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
interface BigIntEnumValidator {
|
|
27
|
-
enumValues: (bigint | null)[];
|
|
28
|
-
message: string;
|
|
29
|
-
type: 'enum';
|
|
30
|
-
validator: (value: bigint | null | undefined) => boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface BigIntLimitValidator {
|
|
34
|
-
max?: bigint;
|
|
35
|
-
message: string;
|
|
36
|
-
min?: bigint;
|
|
37
|
-
type: 'max' | 'min';
|
|
38
|
-
validator: (value: bigint | null | undefined) => boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
42
|
export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
42
43
|
default: <
|
|
43
44
|
T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
|
|
@@ -48,10 +49,7 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
48
49
|
>;
|
|
49
50
|
|
|
50
51
|
enum: <
|
|
51
|
-
T extends
|
|
52
|
-
| Readonlyable<(N | null)[]>
|
|
53
|
-
| { [path: string]: N | null }
|
|
54
|
-
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
52
|
+
T extends EnumInput<N | null, M>,
|
|
55
53
|
M extends string,
|
|
56
54
|
N extends bigint,
|
|
57
55
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -66,7 +64,7 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
66
64
|
>;
|
|
67
65
|
|
|
68
66
|
max: <
|
|
69
|
-
T extends
|
|
67
|
+
T extends ValueWithMessage<L, S>,
|
|
70
68
|
L extends BigIntLimit,
|
|
71
69
|
S extends string,
|
|
72
70
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -75,7 +73,7 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
75
73
|
>;
|
|
76
74
|
|
|
77
75
|
min: <
|
|
78
|
-
T extends
|
|
76
|
+
T extends ValueWithMessage<L, S>,
|
|
79
77
|
L extends BigIntLimit,
|
|
80
78
|
S extends string,
|
|
81
79
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -90,69 +88,42 @@ export interface BigIntSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
|
|
|
90
88
|
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
91
89
|
}
|
|
92
90
|
|
|
93
|
-
interface BigIntValidationSchema {
|
|
94
|
-
validate: (BigIntEnumValidator | BigIntLimitValidator)[];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Constants/Variables
|
|
98
|
-
const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
|
|
99
|
-
const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
|
|
100
|
-
const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
|
|
101
|
-
|
|
102
91
|
// Functions
|
|
103
92
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.BigInt);
|
|
104
93
|
|
|
105
94
|
export function bigintSchemaBuilder() {
|
|
106
|
-
const schema: Record<string, any> = {};
|
|
95
|
+
const schema: Partial<BigIntValidationSchema> & Record<string, any> = {};
|
|
107
96
|
const baseBuilder = baseBuilderFactory(schema);
|
|
108
97
|
return new Proxy(
|
|
109
98
|
baseBuilder,
|
|
110
99
|
{
|
|
111
100
|
get(target, key, receiver) {
|
|
112
101
|
if (key === 'enum') {
|
|
113
|
-
if (schema
|
|
102
|
+
if (hasSchemaValidator(schema, 'enum')) {
|
|
114
103
|
throw new Error('Duplicate schema attribute: enum');
|
|
115
104
|
}
|
|
116
105
|
|
|
117
|
-
return (
|
|
118
|
-
|
|
119
|
-
| { [path: string]: bigint | null }
|
|
120
|
-
| { message?: string; values: Readonlyable<(bigint | null)[]> },
|
|
121
|
-
) => {
|
|
122
|
-
let enumValues: (bigint | null)[];
|
|
123
|
-
let message: string | undefined;
|
|
124
|
-
|
|
125
|
-
if (Array.isArray(value)) enumValues = [...value];
|
|
126
|
-
else if ('values' in value && Array.isArray(value.values)) {
|
|
127
|
-
enumValues = [...value.values];
|
|
128
|
-
if ('message' in value && typeof value.message === 'string') message = value.message;
|
|
129
|
-
} else enumValues = Object.values(value);
|
|
106
|
+
return (value: EnumInput<bigint | null>) => {
|
|
107
|
+
const { enumValues, message } = normalizeEnumValues(value);
|
|
130
108
|
|
|
131
109
|
const allowedValues = new Set(enumValues);
|
|
132
|
-
const validator:
|
|
110
|
+
const validator: EnumValidator<bigint> = {
|
|
133
111
|
enumValues,
|
|
134
112
|
message: message ?? defaultEnumValidateMessage,
|
|
135
113
|
type: 'enum',
|
|
136
114
|
validator: (value) => value === null || value === undefined || allowedValues.has(value),
|
|
137
115
|
};
|
|
138
116
|
|
|
139
|
-
schema
|
|
140
|
-
...schema.validate ?? [],
|
|
141
|
-
validator,
|
|
142
|
-
];
|
|
117
|
+
setSchemaValidator(schema, validator);
|
|
143
118
|
|
|
144
119
|
return receiver;
|
|
145
120
|
};
|
|
146
121
|
}
|
|
147
122
|
|
|
148
123
|
if (key === 'max' || key === 'min') {
|
|
149
|
-
return (value:
|
|
124
|
+
return (value: ValueWithMessage<BigIntLimit>) => {
|
|
150
125
|
const validator = createLimitValidator(key, value);
|
|
151
|
-
|
|
152
|
-
schema.validate = [
|
|
153
|
-
...validators.filter(({ type }) => type !== key),
|
|
154
|
-
validator,
|
|
155
|
-
];
|
|
126
|
+
setSchemaValidator(schema, validator);
|
|
156
127
|
|
|
157
128
|
return receiver;
|
|
158
129
|
};
|
|
@@ -166,19 +137,14 @@ export function bigintSchemaBuilder() {
|
|
|
166
137
|
|
|
167
138
|
function createLimitValidator(
|
|
168
139
|
type: 'max' | 'min',
|
|
169
|
-
value:
|
|
170
|
-
):
|
|
171
|
-
const [limit, message] =
|
|
172
|
-
? value as Readonly<[BigIntLimit, string]>
|
|
173
|
-
: [
|
|
174
|
-
value as BigIntLimit,
|
|
175
|
-
undefined,
|
|
176
|
-
];
|
|
140
|
+
value: ValueWithMessage<BigIntLimit>,
|
|
141
|
+
): LimitValidator<bigint> {
|
|
142
|
+
const [limit, message] = normalizeValueWithMessage(value);
|
|
177
143
|
|
|
178
144
|
if (typeof limit !== 'bigint') throw new TypeError(`BigInt ${type} limit must be a bigint`);
|
|
179
145
|
|
|
180
146
|
return {
|
|
181
|
-
message: message ??
|
|
147
|
+
message: message ?? defaultLimitValidateMessages[type],
|
|
182
148
|
[type]: limit,
|
|
183
149
|
type,
|
|
184
150
|
validator: (value) => {
|