@kikiutils/mongoose 9.0.5 → 10.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 +13 -25
- package/dist/_internals.d.ts +10 -1
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +20 -1
- package/dist/_internals.js.map +1 -1
- package/dist/assertions.d.ts +5 -2
- package/dist/assertions.d.ts.map +1 -1
- package/dist/assertions.js +10 -4
- package/dist/assertions.js.map +1 -1
- package/dist/builders.d.ts +9 -4
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +9 -9
- package/dist/builders.js.map +1 -1
- package/dist/connection.d.ts +3 -0
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +7 -1
- package/dist/connection.js.map +1 -1
- package/dist/documents.d.ts +8 -0
- package/dist/documents.d.ts.map +1 -0
- package/dist/documents.js +10 -0
- package/dist/documents.js.map +1 -0
- package/dist/errors/duplicate-key.d.ts +9 -0
- package/dist/errors/duplicate-key.d.ts.map +1 -0
- package/dist/errors/duplicate-key.js +15 -0
- package/dist/errors/duplicate-key.js.map +1 -0
- package/dist/errors/mongoose-update-conflict-error.d.ts +4 -0
- package/dist/errors/mongoose-update-conflict-error.d.ts.map +1 -0
- package/dist/errors/mongoose-update-conflict-error.js +7 -0
- package/dist/errors/mongoose-update-conflict-error.js.map +1 -0
- package/dist/options.d.ts +15 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +19 -2
- package/dist/options.js.map +1 -1
- package/dist/plugins/assertions.d.ts +30 -9
- package/dist/plugins/assertions.d.ts.map +1 -1
- package/dist/plugins/assertions.js +32 -4
- package/dist/plugins/assertions.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +3 -15
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.js +30 -23
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -1
- package/dist/plugins/normalize.d.ts +3 -40
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +98 -44
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/_internals/array.d.ts +9 -0
- package/dist/schema-builders/_internals/array.d.ts.map +1 -0
- package/dist/schema-builders/_internals/array.js +12 -0
- package/dist/schema-builders/_internals/array.js.map +1 -0
- package/dist/schema-builders/_internals/boolean.d.ts +8 -0
- package/dist/schema-builders/_internals/boolean.d.ts.map +1 -0
- package/dist/schema-builders/_internals/boolean.js +9 -0
- package/dist/schema-builders/_internals/boolean.js.map +1 -0
- package/dist/schema-builders/_internals/date.d.ts +8 -0
- package/dist/schema-builders/_internals/date.d.ts.map +1 -0
- package/dist/schema-builders/_internals/date.js +9 -0
- package/dist/schema-builders/_internals/date.js.map +1 -0
- package/dist/schema-builders/_internals/decimal128.d.ts +36 -0
- package/dist/schema-builders/_internals/decimal128.d.ts.map +1 -0
- package/dist/schema-builders/_internals/decimal128.js +38 -0
- package/dist/schema-builders/_internals/decimal128.js.map +1 -0
- package/dist/schema-builders/_internals/int32.d.ts +21 -0
- package/dist/schema-builders/_internals/int32.d.ts.map +1 -0
- package/dist/schema-builders/_internals/int32.js +29 -0
- package/dist/schema-builders/_internals/int32.js.map +1 -0
- package/dist/schema-builders/_internals/mixed.d.ts +9 -0
- package/dist/schema-builders/_internals/mixed.d.ts.map +1 -0
- package/dist/schema-builders/_internals/mixed.js +10 -0
- package/dist/schema-builders/_internals/mixed.js.map +1 -0
- package/dist/schema-builders/_internals/number.d.ts +21 -0
- package/dist/schema-builders/_internals/number.d.ts.map +1 -0
- package/dist/schema-builders/_internals/number.js +20 -0
- package/dist/schema-builders/_internals/number.js.map +1 -0
- package/dist/schema-builders/_internals/object-id.d.ts +9 -0
- package/dist/schema-builders/_internals/object-id.d.ts.map +1 -0
- package/dist/schema-builders/_internals/object-id.js +10 -0
- package/dist/schema-builders/_internals/object-id.js.map +1 -0
- package/dist/schema-builders/_internals/ref.d.ts +8 -0
- package/dist/schema-builders/_internals/ref.d.ts.map +1 -0
- package/dist/schema-builders/_internals/ref.js +13 -0
- package/dist/schema-builders/_internals/ref.js.map +1 -0
- package/dist/schema-builders/_internals/shared.d.ts +104 -0
- package/dist/schema-builders/_internals/shared.d.ts.map +1 -0
- package/dist/schema-builders/_internals/shared.js +128 -0
- package/dist/schema-builders/_internals/shared.js.map +1 -0
- package/dist/schema-builders/_internals/string.d.ts +37 -0
- package/dist/schema-builders/_internals/string.d.ts.map +1 -0
- package/dist/schema-builders/_internals/string.js +39 -0
- package/dist/schema-builders/_internals/string.js.map +1 -0
- package/dist/schema-builders/index.d.ts +12 -13
- package/dist/schema-builders/index.js +11 -13
- package/dist/transaction.d.ts +10 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +9 -0
- package/dist/transaction.js.map +1 -0
- package/dist/types/data.d.ts +3 -5
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -11
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +4 -19
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +17 -19
- package/src/_internals.ts +31 -1
- package/src/assertions.ts +20 -11
- package/src/builders.ts +26 -18
- package/src/connection.ts +8 -2
- package/src/documents.ts +12 -0
- package/src/errors/duplicate-key.ts +14 -0
- package/src/errors/mongoose-update-conflict-error.ts +3 -0
- package/src/options.ts +31 -1
- package/src/plugins/assertions.ts +117 -49
- package/src/plugins/lean-decimal128-to-string.ts +47 -41
- package/src/plugins/normalize.ts +169 -99
- package/src/schema-builders/_internals/array.ts +15 -0
- package/src/schema-builders/_internals/boolean.ts +10 -0
- package/src/schema-builders/_internals/date.ts +11 -0
- package/src/schema-builders/_internals/decimal128.ts +79 -0
- package/src/schema-builders/_internals/int32.ts +58 -0
- package/src/schema-builders/_internals/mixed.ts +12 -0
- package/src/schema-builders/_internals/number.ts +71 -0
- package/src/schema-builders/_internals/object-id.ts +13 -0
- package/src/schema-builders/_internals/ref.ts +18 -0
- package/src/schema-builders/_internals/shared.ts +302 -0
- package/src/schema-builders/_internals/string.ts +113 -0
- package/src/schema-builders/index.ts +26 -35
- package/src/transaction.ts +22 -0
- package/src/types/data.ts +5 -10
- package/src/types/index.ts +32 -22
- package/src/types/paginate.ts +9 -24
- package/dist/schema-builders/_internals.d.ts +0 -18
- package/dist/schema-builders/_internals.d.ts.map +0 -1
- package/dist/schema-builders/_internals.js +0 -36
- package/dist/schema-builders/_internals.js.map +0 -1
- package/dist/schema-builders/base.d.ts +0 -5
- package/dist/schema-builders/base.d.ts.map +0 -1
- package/dist/schema-builders/base.js +0 -33
- package/dist/schema-builders/base.js.map +0 -1
- package/dist/schema-builders/bigint.d.ts +0 -39
- package/dist/schema-builders/bigint.d.ts.map +0 -1
- package/dist/schema-builders/bigint.js +0 -47
- package/dist/schema-builders/bigint.js.map +0 -1
- package/dist/schema-builders/boolean.d.ts +0 -33
- package/dist/schema-builders/boolean.d.ts.map +0 -1
- package/dist/schema-builders/boolean.js +0 -6
- package/dist/schema-builders/boolean.js.map +0 -1
- package/dist/schema-builders/date.d.ts +0 -49
- package/dist/schema-builders/date.d.ts.map +0 -1
- package/dist/schema-builders/date.js +0 -6
- package/dist/schema-builders/date.js.map +0 -1
- package/dist/schema-builders/decimal128.d.ts +0 -79
- package/dist/schema-builders/decimal128.d.ts.map +0 -1
- package/dist/schema-builders/decimal128.js +0 -47
- package/dist/schema-builders/decimal128.js.map +0 -1
- package/dist/schema-builders/double.d.ts +0 -39
- package/dist/schema-builders/double.d.ts.map +0 -1
- package/dist/schema-builders/double.js +0 -51
- package/dist/schema-builders/double.js.map +0 -1
- package/dist/schema-builders/int32.d.ts +0 -39
- package/dist/schema-builders/int32.d.ts.map +0 -1
- package/dist/schema-builders/int32.js +0 -47
- package/dist/schema-builders/int32.js.map +0 -1
- package/dist/schema-builders/mixed.d.ts +0 -33
- package/dist/schema-builders/mixed.d.ts.map +0 -1
- package/dist/schema-builders/mixed.js +0 -7
- package/dist/schema-builders/mixed.js.map +0 -1
- package/dist/schema-builders/number.d.ts +0 -49
- package/dist/schema-builders/number.d.ts.map +0 -1
- package/dist/schema-builders/number.js +0 -6
- package/dist/schema-builders/number.js.map +0 -1
- package/dist/schema-builders/object-id.d.ts +0 -43
- package/dist/schema-builders/object-id.d.ts.map +0 -1
- package/dist/schema-builders/object-id.js +0 -7
- package/dist/schema-builders/object-id.js.map +0 -1
- package/dist/schema-builders/ref.d.ts +0 -44
- package/dist/schema-builders/ref.d.ts.map +0 -1
- package/dist/schema-builders/ref.js +0 -10
- package/dist/schema-builders/ref.js.map +0 -1
- package/dist/schema-builders/string.d.ts +0 -138
- package/dist/schema-builders/string.d.ts.map +0 -1
- package/dist/schema-builders/string.js +0 -48
- package/dist/schema-builders/string.js.map +0 -1
- package/dist/schema-builders/uuid.d.ts +0 -33
- package/dist/schema-builders/uuid.d.ts.map +0 -1
- package/dist/schema-builders/uuid.js +0 -7
- package/dist/schema-builders/uuid.js.map +0 -1
- package/dist/types/_internals.d.ts +0 -24
- package/dist/types/_internals.d.ts.map +0 -1
- package/dist/types/_internals.js +0 -1
- package/dist/types/aggregate-paginate.d.ts +0 -82
- package/dist/types/aggregate-paginate.d.ts.map +0 -1
- package/dist/types/aggregate-paginate.js +0 -1
- package/dist/types/options.d.ts +0 -23
- package/dist/types/options.d.ts.map +0 -1
- package/dist/types/options.js +0 -1
- package/src/schema-builders/_internals.ts +0 -64
- package/src/schema-builders/base.ts +0 -57
- package/src/schema-builders/bigint.ts +0 -156
- package/src/schema-builders/boolean.ts +0 -45
- package/src/schema-builders/date.ts +0 -80
- package/src/schema-builders/decimal128.ts +0 -196
- package/src/schema-builders/double.ts +0 -166
- package/src/schema-builders/int32.ts +0 -157
- package/src/schema-builders/mixed.ts +0 -37
- package/src/schema-builders/number.ts +0 -80
- package/src/schema-builders/object-id.ts +0 -60
- package/src/schema-builders/ref.ts +0 -67
- package/src/schema-builders/string.ts +0 -258
- package/src/schema-builders/uuid.ts +0 -48
- package/src/types/_internals.ts +0 -27
- package/src/types/aggregate-paginate.ts +0 -115
- package/src/types/options.ts +0 -46
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { EnumInput, NextSchemaBuilder, OptionGuard, SchemaBuilder, Validator } from "./shared.js";
|
|
3
|
+
type Next<Props, Used extends string, Option extends string> = NextSchemaBuilder<'int32', Props, Used, Option, {
|
|
4
|
+
validate: Array<Validator<number>>;
|
|
5
|
+
}>;
|
|
6
|
+
type This<Props, Used extends string, Option extends string> = Int32SchemaBuilder<Props, Used> & OptionGuard<Used, Option>;
|
|
7
|
+
/**
|
|
8
|
+
* Mongoose has no `enum`, `min` or `max` for Int32, so these are added as validators.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Int32SchemaBuilder<Props = {
|
|
11
|
+
type: typeof Schema.Types.Int32;
|
|
12
|
+
}, Used extends string = never> extends SchemaBuilder<'int32', Props, Used, number> {
|
|
13
|
+
enum(this: This<Props, Used, 'enum'>, values: EnumInput<number>, message?: string): Next<Props, Used, 'enum'>;
|
|
14
|
+
max(this: This<Props, Used, 'max'>, value: number, message?: string): Next<Props, Used, 'max'>;
|
|
15
|
+
min(this: This<Props, Used, 'min'>, value: number, message?: string): Next<Props, Used, 'min'>;
|
|
16
|
+
}
|
|
17
|
+
export declare function int32(): Int32SchemaBuilder<{
|
|
18
|
+
type: typeof Schema.Types.Int32;
|
|
19
|
+
}, never>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=int32.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int32.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/int32.ts"],"mappings":";;KAeK,KAAK,OAAO,qBAAqB,yBAAyB,2BAE3D,OACA,MACA;EACE,UAAU,MAAM;;KAGjB,KAAK,OAAO,qBAAqB,yBAClC,mBAAmB,OAAO,QAC1B,YAAY,MAAM;;;;qBAOT,mBACT;EAAU,aAAa,OAAO,MAAM;GACpC,qCACM,uBAAuB,OAAO;EACpC,KAAK,MAAM,KAAK,OAAO,eAAe,QAAQ,mBAAmB,mBAAmB,KAAK,OAAO;EAIhG,IAAI,MAAM,KAAK,OAAO,cAAc,eAAe,mBAAmB,KAAK,OAAO;EAKlF,IAAI,MAAM,KAAK,OAAO,cAAc,eAAe,mBAAmB,KAAK,OAAO;;wBAUtE,SAAK;EAtBD,aAAO,OAAO,MAAM"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { SchemaBuilder, createEnumValidator, createLimitValidator, getEnumValues } from "./shared.js";
|
|
3
|
+
// src/schema-builders/_internals/int32.ts
|
|
4
|
+
const compare = (value, limit) => value - limit;
|
|
5
|
+
/**
|
|
6
|
+
* Mongoose has no `enum`, `min` or `max` for Int32, so these are added as validators.
|
|
7
|
+
*/
|
|
8
|
+
var Int32SchemaBuilder = class extends SchemaBuilder {
|
|
9
|
+
enum(values, message) {
|
|
10
|
+
return this.withValidator("enum", createEnumValidator(getEnumValues(values, "number"), message));
|
|
11
|
+
}
|
|
12
|
+
max(value, message) {
|
|
13
|
+
assertFiniteLimit("max", value);
|
|
14
|
+
return this.withValidator("max", createLimitValidator("max", value, compare, message));
|
|
15
|
+
}
|
|
16
|
+
min(value, message) {
|
|
17
|
+
assertFiniteLimit("min", value);
|
|
18
|
+
return this.withValidator("min", createLimitValidator("min", value, compare, message));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
function assertFiniteLimit(type, limit) {
|
|
22
|
+
if (!Number.isFinite(limit)) throw new RangeError(`Int32 ${type} limit must be finite`);
|
|
23
|
+
}
|
|
24
|
+
function int32() {
|
|
25
|
+
return new Int32SchemaBuilder({ type: Schema.Types.Int32 });
|
|
26
|
+
}
|
|
27
|
+
export { Int32SchemaBuilder, int32 };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=int32.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int32.js","names":[],"sources":["../../../src/schema-builders/_internals/int32.ts"],"sourcesContent":["import { Schema } from 'mongoose';\n\nimport type {\n EnumInput,\n NextSchemaBuilder,\n OptionGuard,\n Validator,\n} from './shared';\nimport {\n createEnumValidator,\n createLimitValidator,\n getEnumValues,\n SchemaBuilder,\n} from './shared';\n\ntype Next<Props, Used extends string, Option extends string> = NextSchemaBuilder<\n 'int32',\n Props,\n Used,\n Option,\n { validate: Array<Validator<number>> }\n>;\n\ntype This<Props, Used extends string, Option extends string> =\n & Int32SchemaBuilder<Props, Used>\n & OptionGuard<Used, Option>;\n\nconst compare = (value: number, limit: number) => value - limit;\n\n/**\n * Mongoose has no `enum`, `min` or `max` for Int32, so these are added as validators.\n */\nexport class Int32SchemaBuilder<\n Props = { type: typeof Schema.Types.Int32 },\n Used extends string = never,\n> extends SchemaBuilder<'int32', Props, Used, number> {\n enum(this: This<Props, Used, 'enum'>, values: EnumInput<number>, message?: string): Next<Props, Used, 'enum'> {\n return this.withValidator('enum', createEnumValidator(getEnumValues(values, 'number'), message));\n }\n\n max(this: This<Props, Used, 'max'>, value: number, message?: string): Next<Props, Used, 'max'> {\n assertFiniteLimit('max', value);\n return this.withValidator('max', createLimitValidator('max', value, compare, message));\n }\n\n min(this: This<Props, Used, 'min'>, value: number, message?: string): Next<Props, Used, 'min'> {\n assertFiniteLimit('min', value);\n return this.withValidator('min', createLimitValidator('min', value, compare, message));\n }\n}\n\nfunction assertFiniteLimit(type: 'max' | 'min', limit: number) {\n if (!Number.isFinite(limit)) throw new RangeError(`Int32 ${type} limit must be finite`);\n}\n\nexport function int32() {\n return new Int32SchemaBuilder({ type: Schema.Types.Int32 });\n}\n"],"mappings":";;;AA2BA,MAAM,WAAW,OAAe,UAAkB,QAAQ;;;;AAK1D,IAAa,qBAAb,cAGU,cAA4C;CAClD,KAAsC,QAA2B,SAA6C;EAC1G,OAAO,KAAK,cAAc,QAAQ,oBAAoB,cAAc,QAAQ,QAAQ,GAAG,OAAO,CAAC;CACnG;CAEA,IAAoC,OAAe,SAA4C;EAC3F,kBAAkB,OAAO,KAAK;EAC9B,OAAO,KAAK,cAAc,OAAO,qBAAqB,OAAO,OAAO,SAAS,OAAO,CAAC;CACzF;CAEA,IAAoC,OAAe,SAA4C;EAC3F,kBAAkB,OAAO,KAAK;EAC9B,OAAO,KAAK,cAAc,OAAO,qBAAqB,OAAO,OAAO,SAAS,OAAO,CAAC;CACzF;AACJ;AAEA,SAAS,kBAAkB,MAAqB,OAAe;CAC3D,IAAI,CAAC,OAAO,SAAS,KAAK,GAAG,MAAM,IAAI,WAAW,SAAS,KAAK,sBAAsB;AAC1F;AAEA,SAAgB,QAAQ;CACpB,OAAO,IAAI,mBAAmB,EAAE,MAAM,OAAO,MAAM,MAAM,CAAC;AAC9D"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { SchemaBuilder } from "./shared.js";
|
|
3
|
+
export declare class MixedSchemaBuilder<Props = {
|
|
4
|
+
type: typeof Schema.Types.Mixed;
|
|
5
|
+
}, Used extends string = never> extends SchemaBuilder<'mixed', Props, Used, unknown> {}
|
|
6
|
+
export declare function mixed(): MixedSchemaBuilder<{
|
|
7
|
+
type: typeof Schema.Types.Mixed;
|
|
8
|
+
}, never>;
|
|
9
|
+
//# sourceMappingURL=mixed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mixed.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/mixed.ts"],"mappings":";;qBAIa,mBACT;EAAU,aAAa,OAAO,MAAM;GACpC,qCACM,uBAAuB,OAAO;wBAExB,SAAK;EAJD,aAAO,OAAO,MAAM"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { SchemaBuilder } from "./shared.js";
|
|
3
|
+
// src/schema-builders/_internals/mixed.ts
|
|
4
|
+
var MixedSchemaBuilder = class extends SchemaBuilder {};
|
|
5
|
+
function mixed() {
|
|
6
|
+
return new MixedSchemaBuilder({ type: Schema.Types.Mixed });
|
|
7
|
+
}
|
|
8
|
+
export { MixedSchemaBuilder, mixed };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=mixed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mixed.js","names":[],"sources":["../../../src/schema-builders/_internals/mixed.ts"],"sourcesContent":["import { Schema } from 'mongoose';\n\nimport { SchemaBuilder } from './shared';\n\nexport class MixedSchemaBuilder<\n Props = { type: typeof Schema.Types.Mixed },\n Used extends string = never,\n> extends SchemaBuilder<'mixed', Props, Used, unknown> {}\n\nexport function mixed() {\n return new MixedSchemaBuilder({ type: Schema.Types.Mixed });\n}\n"],"mappings":";;;AAIA,IAAa,qBAAb,cAGU,cAA6C,CAAC;AAExD,SAAgB,QAAQ;CACpB,OAAO,IAAI,mBAAmB,EAAE,MAAM,OAAO,MAAM,MAAM,CAAC;AAC9D"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EnumInput, EnumValue, NextSchemaBuilder, OptionGuard, SchemaBuilder } from "./shared.js";
|
|
2
|
+
type Next<Props, Used extends string, Option extends string, Added> = NextSchemaBuilder<'number', Props, Used, Option, Added>;
|
|
3
|
+
type This<Props, Used extends string, Option extends string> = NumberSchemaBuilder<Props, Used> & OptionGuard<Used, Option>;
|
|
4
|
+
export declare class NumberSchemaBuilder<Props = {
|
|
5
|
+
type: NumberConstructor;
|
|
6
|
+
}, Used extends string = never> extends SchemaBuilder<'number', Props, Used, number> {
|
|
7
|
+
enum<const T extends EnumInput<number>, Message extends string | undefined = undefined>(this: This<Props, Used, 'enum'>, values: T, message?: Message): Next<Props, Used, 'enum', {
|
|
8
|
+
enum: Message extends string ? {
|
|
9
|
+
message: Message;
|
|
10
|
+
values: Array<EnumValue<T, number>>;
|
|
11
|
+
} : Array<EnumValue<T, number>>;
|
|
12
|
+
}>;
|
|
13
|
+
min<T extends number, Message extends string | undefined = undefined>(this: This<Props, Used, 'min'>, value: T, message?: Message): Next<Props, Used, 'min', {
|
|
14
|
+
min: Message extends string ? [T, Message] : T;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
export declare function number(): NumberSchemaBuilder<{
|
|
18
|
+
type: NumberConstructor;
|
|
19
|
+
}, never>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=number.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/number.ts"],"mappings":";KAYK,KAAK,OAAO,qBAAqB,uBAAuB,SAAS,4BAElE,OACA,MACA,QACA;KAGC,KAAK,OAAO,qBAAqB,yBAClC,oBAAoB,OAAO,QAC3B,YAAY,MAAM;qBAET,oBACT;EAAU,MAAM;GAChB,qCACM,wBAAwB,OAAO;EACrC,WAAW,UAAU,mBAAmB,gDACpC,MAAM,KAAK,OAAO,eAClB,QAAQ,GACR,UAAU,UACX,KACC,OACA;IAGI,MAAM;MAEE,SAAS;MACT,QAAQ,MAAM,UAAU;QAE1B,MAAM,UAAU;;EAiB9B,IAAI,kBAAkB,gDAClB,MAAM,KAAK,OAAO,cAClB,OAAO,GACP,UAAU,UACX,KAAK,OAAO;IAAe,KAAK,0BAA0B,GAAG,WAAW;;;wBAK/D,UAAM;EA3CF,MAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SchemaBuilder, getEnumValues, withMessage } from "./shared.js";
|
|
2
|
+
// src/schema-builders/_internals/number.ts
|
|
3
|
+
var NumberSchemaBuilder = class extends SchemaBuilder {
|
|
4
|
+
enum(values, message) {
|
|
5
|
+
const enumValues = getEnumValues(values, "number");
|
|
6
|
+
return this.with("enum", { enum: message === void 0 ? enumValues : {
|
|
7
|
+
message,
|
|
8
|
+
values: enumValues
|
|
9
|
+
} });
|
|
10
|
+
}
|
|
11
|
+
min(value, message) {
|
|
12
|
+
return this.with("min", { min: withMessage(value, message) });
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
function number() {
|
|
16
|
+
return new NumberSchemaBuilder({ type: Number });
|
|
17
|
+
}
|
|
18
|
+
export { NumberSchemaBuilder, number };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=number.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.js","names":[],"sources":["../../../src/schema-builders/_internals/number.ts"],"sourcesContent":["import type {\n EnumInput,\n EnumValue,\n NextSchemaBuilder,\n OptionGuard,\n} from './shared';\nimport {\n getEnumValues,\n SchemaBuilder,\n withMessage,\n} from './shared';\n\ntype Next<Props, Used extends string, Option extends string, Added> = NextSchemaBuilder<\n 'number',\n Props,\n Used,\n Option,\n Added\n>;\n\ntype This<Props, Used extends string, Option extends string> =\n & NumberSchemaBuilder<Props, Used>\n & OptionGuard<Used, Option>;\n\nexport class NumberSchemaBuilder<\n Props = { type: NumberConstructor },\n Used extends string = never,\n> extends SchemaBuilder<'number', Props, Used, number> {\n enum<const T extends EnumInput<number>, Message extends string | undefined = undefined>(\n this: This<Props, Used, 'enum'>,\n values: T,\n message?: Message,\n ): Next<\n Props,\n Used,\n 'enum',\n {\n enum: Message extends string\n ? {\n message: Message;\n values: Array<EnumValue<T, number>>;\n }\n : Array<EnumValue<T, number>>;\n }\n > {\n const enumValues = getEnumValues(values, 'number');\n return this.with(\n 'enum',\n {\n enum: message === undefined\n ? enumValues\n : {\n message,\n values: enumValues,\n },\n },\n );\n }\n\n min<T extends number, Message extends string | undefined = undefined>(\n this: This<Props, Used, 'min'>,\n value: T,\n message?: Message,\n ): Next<Props, Used, 'min', { min: Message extends string ? [T, Message] : T }> {\n return this.with('min', { min: withMessage(value, message) });\n }\n}\n\nexport function number() {\n return new NumberSchemaBuilder({ type: Number });\n}\n"],"mappings":";;AAwBA,IAAa,sBAAb,cAGU,cAA6C;CACnD,KAEI,QACA,SAaF;EACE,MAAM,aAAa,cAAc,QAAQ,QAAQ;EACjD,OAAO,KAAK,KACR,QACA,EACI,MAAM,YAAY,KAAA,IACZ,aACA;GACE;GACA,QAAQ;EACZ,EACR,CACJ;CACJ;CAEA,IAEI,OACA,SAC4E;EAC5E,OAAO,KAAK,KAAK,OAAO,EAAE,KAAK,YAAY,OAAO,OAAO,EAAE,CAAC;CAChE;AACJ;AAEA,SAAgB,SAAS;CACrB,OAAO,IAAI,oBAAoB,EAAE,MAAM,OAAO,CAAC;AACnD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Schema, Types } from "mongoose";
|
|
2
|
+
import { SchemaBuilder } from "./shared.js";
|
|
3
|
+
export declare class ObjectIdSchemaBuilder<Props = {
|
|
4
|
+
type: typeof Schema.Types.ObjectId;
|
|
5
|
+
}, Used extends string = never> extends SchemaBuilder<'objectId', Props, Used, Types.ObjectId> {}
|
|
6
|
+
export declare function objectId(): ObjectIdSchemaBuilder<{
|
|
7
|
+
type: typeof Schema.Types.ObjectId;
|
|
8
|
+
}, never>;
|
|
9
|
+
//# sourceMappingURL=object-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-id.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/object-id.ts"],"mappings":";;qBAKa,sBACT;EAAU,aAAa,OAAO,MAAM;GACpC,qCACM,0BAA0B,OAAO,MAAM,MAAM;wBAEvC,YAAQ;EAJJ,aAAO,OAAO,MAAM"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { SchemaBuilder } from "./shared.js";
|
|
3
|
+
// src/schema-builders/_internals/object-id.ts
|
|
4
|
+
var ObjectIdSchemaBuilder = class extends SchemaBuilder {};
|
|
5
|
+
function objectId() {
|
|
6
|
+
return new ObjectIdSchemaBuilder({ type: Schema.Types.ObjectId });
|
|
7
|
+
}
|
|
8
|
+
export { ObjectIdSchemaBuilder, objectId };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=object-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-id.js","names":[],"sources":["../../../src/schema-builders/_internals/object-id.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport type { Types } from 'mongoose';\n\nimport { SchemaBuilder } from './shared';\n\nexport class ObjectIdSchemaBuilder<\n Props = { type: typeof Schema.Types.ObjectId },\n Used extends string = never,\n> extends SchemaBuilder<'objectId', Props, Used, Types.ObjectId> {}\n\nexport function objectId() {\n return new ObjectIdSchemaBuilder({ type: Schema.Types.ObjectId });\n}\n"],"mappings":";;;AAKA,IAAa,wBAAb,cAGU,cAAuD,CAAC;AAElE,SAAgB,WAAW;CACvB,OAAO,IAAI,sBAAsB,EAAE,MAAM,OAAO,MAAM,SAAS,CAAC;AACpE"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Schema, Types } from "mongoose";
|
|
2
|
+
import { SchemaBuilder } from "./shared.js";
|
|
3
|
+
export declare class RefSchemaBuilder<Props, Used extends string = never> extends SchemaBuilder<'ref', Props, Used, Types.ObjectId> {}
|
|
4
|
+
export declare function ref<const T extends string>(modelName: T): RefSchemaBuilder<{
|
|
5
|
+
ref: T;
|
|
6
|
+
type: typeof Schema.Types.ObjectId;
|
|
7
|
+
}, never>;
|
|
8
|
+
//# sourceMappingURL=ref.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ref.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/ref.ts"],"mappings":";;qBAKa,iBAAiB,OAAO,qCAAqC,qBAEtE,OACA,MACA,MAAM;wBAGM,UAAU,kBAAkB,WAAW,IAAC;EACjB,KAAA;EAAS,aAAO,OAAO,MAAM"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Schema } from "mongoose";
|
|
2
|
+
import { SchemaBuilder } from "./shared.js";
|
|
3
|
+
// src/schema-builders/_internals/ref.ts
|
|
4
|
+
var RefSchemaBuilder = class extends SchemaBuilder {};
|
|
5
|
+
function ref(modelName) {
|
|
6
|
+
return new RefSchemaBuilder({
|
|
7
|
+
ref: modelName,
|
|
8
|
+
type: Schema.Types.ObjectId
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export { RefSchemaBuilder, ref };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=ref.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ref.js","names":[],"sources":["../../../src/schema-builders/_internals/ref.ts"],"sourcesContent":["import { Schema } from 'mongoose';\nimport type { Types } from 'mongoose';\n\nimport { SchemaBuilder } from './shared';\n\nexport class RefSchemaBuilder<Props, Used extends string = never> extends SchemaBuilder<\n 'ref',\n Props,\n Used,\n Types.ObjectId\n> {}\n\nexport function ref<const T extends string>(modelName: T) {\n return new RefSchemaBuilder<{ ref: T; type: typeof Schema.Types.ObjectId }>({\n ref: modelName,\n type: Schema.Types.ObjectId,\n });\n}\n"],"mappings":";;;AAKA,IAAa,mBAAb,cAA0E,cAKxE,CAAC;AAEH,SAAgB,IAA4B,WAAc;CACtD,OAAO,IAAI,iBAAiE;EACxE,KAAK;EACL,MAAM,OAAO,MAAM;CACvB,CAAC;AACL"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { IndexDirection, IndexOptions } from "mongoose";
|
|
2
|
+
import { Simplify, UnknownRecord, ValueOf } from "type-fest";
|
|
3
|
+
import { ArraySchemaBuilder } from "./array.js";
|
|
4
|
+
import { BooleanSchemaBuilder } from "./boolean.js";
|
|
5
|
+
import { DateSchemaBuilder } from "./date.js";
|
|
6
|
+
import { Decimal128SchemaBuilder } from "./decimal128.js";
|
|
7
|
+
import { Int32SchemaBuilder } from "./int32.js";
|
|
8
|
+
import { MixedSchemaBuilder } from "./mixed.js";
|
|
9
|
+
import { NumberSchemaBuilder } from "./number.js";
|
|
10
|
+
import { ObjectIdSchemaBuilder } from "./object-id.js";
|
|
11
|
+
import { RefSchemaBuilder } from "./ref.js";
|
|
12
|
+
import { StringSchemaBuilder } from "./string.js";
|
|
13
|
+
/**
|
|
14
|
+
* A TypeScript enum object contributes only its values of the builder's type. `null` is not an enum value because
|
|
15
|
+
* the native String and Number enums and the emulated ones all accept it.
|
|
16
|
+
*/
|
|
17
|
+
export type EnumInput<Value> = EnumObject | readonly Value[];
|
|
18
|
+
export type EnumObject = Readonly<Record<string, number | string>>;
|
|
19
|
+
export type EnumValue<T, Value> = T extends readonly unknown[] ? T[number] : Extract<ValueOf<T>, Value>;
|
|
20
|
+
/**
|
|
21
|
+
* Using an option also marks the listed ones as used, at runtime and in the types.
|
|
22
|
+
*/
|
|
23
|
+
declare const conflictingOptions: {
|
|
24
|
+
readonly index: readonly ["unique"];
|
|
25
|
+
readonly length: readonly ["maxlength", "minlength"];
|
|
26
|
+
readonly maxlength: readonly ["length"];
|
|
27
|
+
readonly minlength: readonly ["length"];
|
|
28
|
+
readonly unique: readonly ["index"];
|
|
29
|
+
};
|
|
30
|
+
type ConflictingOptions = typeof conflictingOptions;
|
|
31
|
+
export type NextSchemaBuilder<Kind extends SchemaBuilderKind, Props, Used extends string, Option extends string, Added> = SchemaBuilderMap<Added & Props, UseOption<Used, Option>>[Kind];
|
|
32
|
+
export type OptionGuard<Used extends string, Option extends string> = Option extends Used ? { [_ in `Error: '${Option}' is already set or conflicts with another option`]: never; } : unknown;
|
|
33
|
+
export type SchemaBuilderKind = keyof SchemaBuilderMap<any, any>;
|
|
34
|
+
export type UseOption<Used extends string, Option extends string> = Option | (Option extends keyof ConflictingOptions ? ConflictingOptions[Option][number] : never) | Used;
|
|
35
|
+
export interface SchemaBuilderMap<Props, Used extends string> {
|
|
36
|
+
array: ArraySchemaBuilder<Props, Used>;
|
|
37
|
+
boolean: BooleanSchemaBuilder<Props, Used>;
|
|
38
|
+
date: DateSchemaBuilder<Props, Used>;
|
|
39
|
+
decimal128: Decimal128SchemaBuilder<Props, Used>;
|
|
40
|
+
int32: Int32SchemaBuilder<Props, Used>;
|
|
41
|
+
mixed: MixedSchemaBuilder<Props, Used>;
|
|
42
|
+
number: NumberSchemaBuilder<Props, Used>;
|
|
43
|
+
objectId: ObjectIdSchemaBuilder<Props, Used>;
|
|
44
|
+
ref: RefSchemaBuilder<Props, Used>;
|
|
45
|
+
string: StringSchemaBuilder<Props, Used>;
|
|
46
|
+
}
|
|
47
|
+
export interface Validator<Value> {
|
|
48
|
+
message?: string;
|
|
49
|
+
type: string;
|
|
50
|
+
validator: (value: null | undefined | Value) => boolean | Promise<boolean>;
|
|
51
|
+
}
|
|
52
|
+
export declare const defaultEnumMessage = "Path `{PATH}` (`{VALUE}`) is not a valid enum value.";
|
|
53
|
+
export declare const defaultLimitMessages: {
|
|
54
|
+
max: string;
|
|
55
|
+
min: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Every call returns a new builder, so a partially configured builder can be reused. `Value` is what `default`
|
|
59
|
+
* accepts; `CastValue` is what Mongoose stores and passes to validators.
|
|
60
|
+
*/
|
|
61
|
+
export declare abstract class SchemaBuilder<Kind extends SchemaBuilderKind, Props, Used extends string, Value, CastValue = Value> {
|
|
62
|
+
#private;
|
|
63
|
+
private readonly '~types';
|
|
64
|
+
constructor(definition: UnknownRecord, used?: ReadonlySet<string>);
|
|
65
|
+
default<T extends ((this: any, doc: any) => null | undefined | Value) | null | Value>(this: OptionGuard<Used, 'default'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>, value: T): NextSchemaBuilder<Kind, Props, Used, 'default', {
|
|
66
|
+
default: T;
|
|
67
|
+
}>;
|
|
68
|
+
immutable(this: OptionGuard<Used, 'immutable'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>): NextSchemaBuilder<Kind, Props, Used, 'immutable', {
|
|
69
|
+
immutable: true;
|
|
70
|
+
}>;
|
|
71
|
+
index<T extends boolean | IndexDirection | IndexOptions = true>(this: OptionGuard<Used, 'index'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>, value?: T): NextSchemaBuilder<Kind, Props, Used, 'index', {
|
|
72
|
+
index: T;
|
|
73
|
+
}>;
|
|
74
|
+
optional(): Simplify<Props>;
|
|
75
|
+
/**
|
|
76
|
+
* Marks the path for removal from `toJSON` output by the normalize plugin.
|
|
77
|
+
*/
|
|
78
|
+
private(this: OptionGuard<Used, 'private'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>): NextSchemaBuilder<Kind, Props, Used, 'private', {
|
|
79
|
+
private: true;
|
|
80
|
+
}>;
|
|
81
|
+
required(): Simplify<Props & {
|
|
82
|
+
required: true;
|
|
83
|
+
}>;
|
|
84
|
+
unique(this: OptionGuard<Used, 'unique'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>): NextSchemaBuilder<Kind, Props, Used, 'unique', {
|
|
85
|
+
unique: true;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Adds a custom validator alongside any the builder already adds. Mongoose skips validators for `undefined`
|
|
89
|
+
* values, but passes `null`.
|
|
90
|
+
*/
|
|
91
|
+
validate(this: OptionGuard<Used, 'validate'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>, validator: (value: CastValue | null) => boolean | Promise<boolean>, message?: string): NextSchemaBuilder<Kind, Props, Used, 'validate', {
|
|
92
|
+
validate: Array<Validator<unknown>>;
|
|
93
|
+
}>;
|
|
94
|
+
protected with(option: string, patch: UnknownRecord): any;
|
|
95
|
+
protected withValidator(option: string, validator: Validator<any>): any;
|
|
96
|
+
}
|
|
97
|
+
export declare function createEnumValidator<Value>(values: readonly Value[], message?: string): Validator<Value> & {
|
|
98
|
+
enumValues: Value[];
|
|
99
|
+
};
|
|
100
|
+
export declare function createLimitValidator<Value>(type: 'max' | 'min', limit: Value, compare: (value: Value, limit: Value) => number, message?: string): Partial<Record<'max' | 'min', Value>> & Validator<Value>;
|
|
101
|
+
export declare function getEnumValues<Value extends number | string>(input: EnumInput<Value>, valueType: 'number' | 'string'): Value[];
|
|
102
|
+
export declare function withMessage<T, Message extends string | undefined>(value: T, message: Message): Message extends string ? [T, Message] : T;
|
|
103
|
+
export {};
|
|
104
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/shared.ts"],"mappings":";;;;;;;;;;;;;;;;YA6BY,UAAU,SAAS,sBAAsB;YACzC,aAAa,SAAS;YACtB,UAAU,GAAG,SAAS,+BAA+B,YAAY,QAAQ,QAAQ,IAAI;;;;cAK3F;;;;;;;KAYD,4BAA4B;YAErB,kBACR,aAAa,mBACb,OACA,qBACA,uBACA,SACA,iBAAiB,QAAQ,OAAO,UAAU,MAAM,SAAS;YAEjD,YAAY,qBAAqB,yBAAyB,eAAe,UAC5E,gBAAgB;YAGb,0BAA0B;YAE1B,UAAU,qBAAqB,yBACvC,UACC,qBAAqB,qBAAqB,mBAAmB,2BAC9D;iBAEa,iBAAiB,OAAO;EACrC,OAAO,mBAAmB,OAAO;EACjC,SAAS,qBAAqB,OAAO;EACrC,MAAM,kBAAkB,OAAO;EAC/B,YAAY,wBAAwB,OAAO;EAC3C,OAAO,mBAAmB,OAAO;EACjC,OAAO,mBAAmB,OAAO;EACjC,QAAQ,oBAAoB,OAAO;EACnC,UAAU,sBAAsB,OAAO;EACvC,KAAK,iBAAiB,OAAO;EAC7B,QAAQ,oBAAoB,OAAO;;iBAGtB,UAAU;EACvB;EACA;EACA,YAAY,0BAA0B,oBAAoB;;qBAGjD;qBACA;;;;;;;;8BASS,cAClB,aAAa,mBACb,OACA,qBACA,OACA,YAAY;;;EAcA,YAAA,YAAY,eAAe,OAAM;EAK7C,QAAQ,YAAY,WAAW,gCAAgC,gBAAgB,OAC3E,MAAM,YAAY,mBAAmB,cAAc,MAAM,OAAO,MAAM,OAAO,YAC7E,OAAO,IACR,kBAAkB,MAAM,OAAO;IAAmB,SAAS;;EAI9D,UACI,MAAM,YAAY,qBAAqB,cAAc,MAAM,OAAO,MAAM,OAAO,aAChF,kBAAkB,MAAM,OAAO;IAAqB;;EAIvD,MAAM,oBAAoB,iBAAiB,qBACvC,MAAM,YAAY,iBAAiB,cAAc,MAAM,OAAO,MAAM,OAAO,YAC3E,QAAQ,IACT,kBAAkB,MAAM,OAAO;IAAiB,OAAO;;EAI1D,YAAY,SAAS;;;;EAOrB,QACI,MAAM,YAAY,mBAAmB,cAAc,MAAM,OAAO,MAAM,OAAO,aAC9E,kBAAkB,MAAM,OAAO;IAAmB;;EAIrD,YAAY,SAAS;IAAU;;EAO/B,OACI,MAAM,YAAY,kBAAkB,cAAc,MAAM,OAAO,MAAM,OAAO,aAC7E,kBAAkB,MAAM,OAAO;IAAkB;;;;;;EAQpD,SACI,MAAM,YAAY,oBAAoB,cAAc,MAAM,OAAO,MAAM,OAAO,YAC9E,YAAY,OAAO,+BAA+B,kBAClD,mBACD,kBAAkB,MAAM,OAAO;IAAoB,UAAU,MAAM;;YAW5D,KAAK,gBAAgB,OAAO;YAqB5B,cAAc,gBAAgB,WAAW;;wBA6BvC,oBAAoB,OAChC,iBAAiB,SACjB,mBACD,UAAU;EAAW,YAAY;;wBAUpB,qBAAqB,OACjC,qBACA,OAAO,OACP,UAAU,OAAO,OAAO,OAAO,kBAC/B,mBACD,QAAQ,sBAAsB,UAAU,UAAU;wBAarC,cAAc,+BAC1B,OAAO,UAAU,QACjB,iCACD;wBAoBa,YAAY,GAAG,oCAC3B,OAAO,GACP,SAAS,UACV,0BAA0B,GAAG,WAAW"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { objectEntries, objectKeys } from "ts-extras";
|
|
2
|
+
// src/schema-builders/_internals/shared.ts
|
|
3
|
+
/**
|
|
4
|
+
* Using an option also marks the listed ones as used, at runtime and in the types.
|
|
5
|
+
*/
|
|
6
|
+
const conflictingOptions = {
|
|
7
|
+
index: ["unique"],
|
|
8
|
+
length: ["maxlength", "minlength"],
|
|
9
|
+
maxlength: ["length"],
|
|
10
|
+
minlength: ["length"],
|
|
11
|
+
unique: ["index"]
|
|
12
|
+
};
|
|
13
|
+
const defaultEnumMessage = "Path `{PATH}` (`{VALUE}`) is not a valid enum value.";
|
|
14
|
+
const defaultLimitMessages = {
|
|
15
|
+
max: "Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).",
|
|
16
|
+
min: "Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN})."
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Every call returns a new builder, so a partially configured builder can be reused. `Value` is what `default`
|
|
20
|
+
* accepts; `CastValue` is what Mongoose stores and passes to validators.
|
|
21
|
+
*/
|
|
22
|
+
var SchemaBuilder = class {
|
|
23
|
+
#definition;
|
|
24
|
+
#used;
|
|
25
|
+
constructor(definition, used = /* @__PURE__ */ new Set()) {
|
|
26
|
+
this.#definition = definition;
|
|
27
|
+
this.#used = used;
|
|
28
|
+
}
|
|
29
|
+
default(value) {
|
|
30
|
+
return this.with("default", { default: value });
|
|
31
|
+
}
|
|
32
|
+
immutable() {
|
|
33
|
+
return this.with("immutable", { immutable: true });
|
|
34
|
+
}
|
|
35
|
+
index(value) {
|
|
36
|
+
return this.with("index", { index: value ?? true });
|
|
37
|
+
}
|
|
38
|
+
optional() {
|
|
39
|
+
return this.#build();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Marks the path for removal from `toJSON` output by the normalize plugin.
|
|
43
|
+
*/
|
|
44
|
+
private() {
|
|
45
|
+
return this.with("private", { private: true });
|
|
46
|
+
}
|
|
47
|
+
required() {
|
|
48
|
+
return {
|
|
49
|
+
...this.#build(),
|
|
50
|
+
required: true
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
unique() {
|
|
54
|
+
return this.with("unique", { unique: true });
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds a custom validator alongside any the builder already adds. Mongoose skips validators for `undefined`
|
|
58
|
+
* values, but passes `null`.
|
|
59
|
+
*/
|
|
60
|
+
validate(validator, message) {
|
|
61
|
+
return this.withValidator("validate", {
|
|
62
|
+
...message === void 0 ? {} : { message },
|
|
63
|
+
type: "user defined",
|
|
64
|
+
validator
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
with(option, patch) {
|
|
68
|
+
if (this.#used.has(option)) throw new Error(`Schema option '${option}' is already set or conflicts with another option`);
|
|
69
|
+
const used = new Set(this.#used).add(option);
|
|
70
|
+
conflictingOptions[option]?.forEach((conflictingOption) => used.add(conflictingOption));
|
|
71
|
+
return Reflect.construct(this.constructor, [{
|
|
72
|
+
...this.#definition,
|
|
73
|
+
...patch
|
|
74
|
+
}, used]);
|
|
75
|
+
}
|
|
76
|
+
withValidator(option, validator) {
|
|
77
|
+
const validate = this.#definition.validate;
|
|
78
|
+
return this.with(option, { validate: [...validate ?? [], validator] });
|
|
79
|
+
}
|
|
80
|
+
#build() {
|
|
81
|
+
return copyContainers(this.#definition);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function copyContainers(value) {
|
|
85
|
+
if (Array.isArray(value)) return value.map(copyContainers);
|
|
86
|
+
if (value === null || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) return value;
|
|
87
|
+
return Object.fromEntries(objectEntries(value).map(([key, item]) => [key, copyContainers(item)]));
|
|
88
|
+
}
|
|
89
|
+
function createEnumValidator(values, message = defaultEnumMessage) {
|
|
90
|
+
const allowedValues = new Set(values);
|
|
91
|
+
return {
|
|
92
|
+
enumValues: [...values],
|
|
93
|
+
message,
|
|
94
|
+
type: "enum",
|
|
95
|
+
validator: (value) => value === null || value === void 0 || allowedValues.has(value)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function createLimitValidator(type, limit, compare, message = defaultLimitMessages[type]) {
|
|
99
|
+
return {
|
|
100
|
+
message,
|
|
101
|
+
[type]: limit,
|
|
102
|
+
type,
|
|
103
|
+
validator: (value) => {
|
|
104
|
+
if (value === null || value === void 0) return true;
|
|
105
|
+
const result = compare(value, limit);
|
|
106
|
+
return type === "min" ? result >= 0 : result <= 0;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function getEnumValues(input, valueType) {
|
|
111
|
+
let values;
|
|
112
|
+
if (Array.isArray(input)) values = [...input];
|
|
113
|
+
else {
|
|
114
|
+
const enumObject = input;
|
|
115
|
+
values = objectKeys(enumObject).filter((key) => {
|
|
116
|
+
const value = enumObject[key];
|
|
117
|
+
return typeof value !== "string" || enumObject[value] !== Number(key);
|
|
118
|
+
}).map((key) => enumObject[key]).filter((value) => valueType === "number" ? typeof value === "number" : typeof value === "string");
|
|
119
|
+
}
|
|
120
|
+
if (!values.length) throw new RangeError(`An enum needs at least one ${valueType} value`);
|
|
121
|
+
return values;
|
|
122
|
+
}
|
|
123
|
+
function withMessage(value, message) {
|
|
124
|
+
return message === void 0 ? value : [value, message];
|
|
125
|
+
}
|
|
126
|
+
export { SchemaBuilder, createEnumValidator, createLimitValidator, defaultEnumMessage, defaultLimitMessages, getEnumValues, withMessage };
|
|
127
|
+
|
|
128
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","names":[],"sources":["../../../src/schema-builders/_internals/shared.ts"],"sourcesContent":["import type {\n IndexDirection,\n IndexOptions,\n} from 'mongoose';\nimport {\n objectEntries,\n objectKeys,\n} from 'ts-extras';\nimport type {\n Simplify,\n UnknownRecord,\n ValueOf,\n} from 'type-fest';\n\nimport type { ArraySchemaBuilder } from './array';\nimport type { BooleanSchemaBuilder } from './boolean';\nimport type { DateSchemaBuilder } from './date';\nimport type { Decimal128SchemaBuilder } from './decimal128';\nimport type { Int32SchemaBuilder } from './int32';\nimport type { MixedSchemaBuilder } from './mixed';\nimport type { NumberSchemaBuilder } from './number';\nimport type { ObjectIdSchemaBuilder } from './object-id';\nimport type { RefSchemaBuilder } from './ref';\nimport type { StringSchemaBuilder } from './string';\n\n/**\n * A TypeScript enum object contributes only its values of the builder's type. `null` is not an enum value because\n * the native String and Number enums and the emulated ones all accept it.\n */\nexport type EnumInput<Value> = EnumObject | readonly Value[];\nexport type EnumObject = Readonly<Record<string, number | string>>;\nexport type EnumValue<T, Value> = T extends readonly unknown[] ? T[number] : Extract<ValueOf<T>, Value>;\n\n/**\n * Using an option also marks the listed ones as used, at runtime and in the types.\n */\nconst conflictingOptions = {\n // A `unique` index takes its direction from `index` only in some option orders.\n index: ['unique'],\n length: [\n 'maxlength',\n 'minlength',\n ],\n maxlength: ['length'],\n minlength: ['length'],\n unique: ['index'],\n} as const satisfies Readonly<Record<string, readonly string[]>>;\n\ntype ConflictingOptions = typeof conflictingOptions;\n\nexport type NextSchemaBuilder<\n Kind extends SchemaBuilderKind,\n Props,\n Used extends string,\n Option extends string,\n Added,\n> = SchemaBuilderMap<Added & Props, UseOption<Used, Option>>[Kind];\n\nexport type OptionGuard<Used extends string, Option extends string> = Option extends Used\n ? { [_ in `Error: '${Option}' is already set or conflicts with another option`]: never }\n : unknown;\n\nexport type SchemaBuilderKind = keyof SchemaBuilderMap<any, any>;\n\nexport type UseOption<Used extends string, Option extends string> =\n | Option\n | (Option extends keyof ConflictingOptions ? ConflictingOptions[Option][number] : never)\n | Used;\n\nexport interface SchemaBuilderMap<Props, Used extends string> {\n array: ArraySchemaBuilder<Props, Used>;\n boolean: BooleanSchemaBuilder<Props, Used>;\n date: DateSchemaBuilder<Props, Used>;\n decimal128: Decimal128SchemaBuilder<Props, Used>;\n int32: Int32SchemaBuilder<Props, Used>;\n mixed: MixedSchemaBuilder<Props, Used>;\n number: NumberSchemaBuilder<Props, Used>;\n objectId: ObjectIdSchemaBuilder<Props, Used>;\n ref: RefSchemaBuilder<Props, Used>;\n string: StringSchemaBuilder<Props, Used>;\n}\n\nexport interface Validator<Value> {\n message?: string;\n type: string;\n validator: (value: null | undefined | Value) => boolean | Promise<boolean>;\n}\n\nexport const defaultEnumMessage = 'Path `{PATH}` (`{VALUE}`) is not a valid enum value.';\nexport const defaultLimitMessages = {\n max: 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).',\n min: 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).',\n};\n\n/**\n * Every call returns a new builder, so a partially configured builder can be reused. `Value` is what `default`\n * accepts; `CastValue` is what Mongoose stores and passes to validators.\n */\nexport abstract class SchemaBuilder<\n Kind extends SchemaBuilderKind,\n Props,\n Used extends string,\n Value,\n CastValue = Value,\n> {\n // Keeps the type parameters structurally significant so the `this` guards can tell builders apart.\n declare private readonly '~types': {\n castValue: CastValue;\n kind: Kind;\n props: Props;\n used: Used;\n value: Value;\n };\n\n readonly #definition: Readonly<UnknownRecord>;\n readonly #used: ReadonlySet<string>;\n\n constructor(definition: UnknownRecord, used: ReadonlySet<string> = new Set()) {\n this.#definition = definition;\n this.#used = used;\n }\n\n default<T extends ((this: any, doc: any) => null | undefined | Value) | null | Value>(\n this: OptionGuard<Used, 'default'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>,\n value: T,\n ): NextSchemaBuilder<Kind, Props, Used, 'default', { default: T }> {\n return this.with('default', { default: value });\n }\n\n immutable(\n this: OptionGuard<Used, 'immutable'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>,\n ): NextSchemaBuilder<Kind, Props, Used, 'immutable', { immutable: true }> {\n return this.with('immutable', { immutable: true });\n }\n\n index<T extends boolean | IndexDirection | IndexOptions = true>(\n this: OptionGuard<Used, 'index'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>,\n value?: T,\n ): NextSchemaBuilder<Kind, Props, Used, 'index', { index: T }> {\n return this.with('index', { index: value ?? true });\n }\n\n optional(): Simplify<Props> {\n return this.#build() as Simplify<Props>;\n }\n\n /**\n * Marks the path for removal from `toJSON` output by the normalize plugin.\n */\n private(\n this: OptionGuard<Used, 'private'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>,\n ): NextSchemaBuilder<Kind, Props, Used, 'private', { private: true }> {\n return this.with('private', { private: true });\n }\n\n required(): Simplify<Props & { required: true }> {\n return {\n ...this.#build(),\n required: true,\n } as Simplify<Props & { required: true }>;\n }\n\n unique(\n this: OptionGuard<Used, 'unique'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>,\n ): NextSchemaBuilder<Kind, Props, Used, 'unique', { unique: true }> {\n return this.with('unique', { unique: true });\n }\n\n /**\n * Adds a custom validator alongside any the builder already adds. Mongoose skips validators for `undefined`\n * values, but passes `null`.\n */\n validate(\n this: OptionGuard<Used, 'validate'> & SchemaBuilder<Kind, Props, Used, Value, CastValue>,\n validator: (value: CastValue | null) => boolean | Promise<boolean>,\n message?: string,\n ): NextSchemaBuilder<Kind, Props, Used, 'validate', { validate: Array<Validator<unknown>> }> {\n return this.withValidator(\n 'validate',\n {\n ...message === undefined ? {} : { message },\n type: 'user defined',\n validator,\n },\n );\n }\n\n protected with(option: string, patch: UnknownRecord): any {\n if (this.#used.has(option)) {\n throw new Error(`Schema option '${option}' is already set or conflicts with another option`);\n }\n\n const used = new Set(this.#used).add(option);\n (conflictingOptions as Readonly<Record<string, readonly string[] | undefined>>)[option]\n ?.forEach((conflictingOption) => used.add(conflictingOption));\n\n return Reflect.construct(\n this.constructor,\n [\n {\n ...this.#definition,\n ...patch,\n },\n used,\n ],\n );\n }\n\n protected withValidator(option: string, validator: Validator<any>): any {\n const validate = this.#definition.validate as Array<Validator<unknown>> | undefined;\n return this.with(\n option,\n {\n validate: [\n ...validate ?? [],\n validator,\n ],\n },\n );\n }\n\n #build(): UnknownRecord {\n // Builders are reused, so a definition must not share arrays or plain objects that callers or Mongoose\n // may modify, such as enum values, validators and index options.\n return copyContainers(this.#definition) as UnknownRecord;\n }\n}\n\nfunction copyContainers(value: unknown): unknown {\n if (Array.isArray(value)) return value.map(copyContainers);\n if (value === null || typeof value !== 'object' || Object.getPrototypeOf(value) !== Object.prototype) return value;\n return Object.fromEntries(objectEntries(value).map(([key, item]) => [\n key,\n copyContainers(item),\n ]));\n}\n\nexport function createEnumValidator<Value>(\n values: readonly Value[],\n message = defaultEnumMessage,\n): Validator<Value> & { enumValues: Value[] } {\n const allowedValues = new Set(values);\n return {\n enumValues: [...values],\n message,\n type: 'enum',\n validator: (value) => value === null || value === undefined || allowedValues.has(value),\n };\n}\n\nexport function createLimitValidator<Value>(\n type: 'max' | 'min',\n limit: Value,\n compare: (value: Value, limit: Value) => number,\n message = defaultLimitMessages[type],\n): Partial<Record<'max' | 'min', Value>> & Validator<Value> {\n return {\n message,\n [type]: limit,\n type,\n validator: (value) => {\n if (value === null || value === undefined) return true;\n const result = compare(value, limit);\n return type === 'min' ? result >= 0 : result <= 0;\n },\n };\n}\n\nexport function getEnumValues<Value extends number | string>(\n input: EnumInput<Value>,\n valueType: 'number' | 'string',\n): Value[] {\n let values: Value[];\n if (Array.isArray(input)) values = [...input as readonly Value[]];\n else {\n const enumObject = input as EnumObject;\n // Numeric TypeScript enums also map each value back to its name; only such entries are skipped.\n values = objectKeys(enumObject)\n .filter((key) => {\n const value = enumObject[key];\n return typeof value !== 'string' || enumObject[value] !== Number(key);\n })\n .map((key) => enumObject[key])\n .filter((value): value is Value => valueType === 'number' ? typeof value === 'number' : typeof value === 'string');\n }\n\n // Mongoose treats an empty enum as \"no enum\" and drops the validator.\n if (!values.length) throw new RangeError(`An enum needs at least one ${valueType} value`);\n return values;\n}\n\nexport function withMessage<T, Message extends string | undefined>(\n value: T,\n message: Message,\n): Message extends string ? [T, Message] : T {\n return (message === undefined\n ? value\n : [\n value,\n message,\n ]) as Message extends string ? [T, Message] : T;\n}\n"],"mappings":";;;;;AAoCA,MAAM,qBAAqB;CAEvB,OAAO,CAAC,QAAQ;CAChB,QAAQ,CACJ,aACA,WACJ;CACA,WAAW,CAAC,QAAQ;CACpB,WAAW,CAAC,QAAQ;CACpB,QAAQ,CAAC,OAAO;AACpB;AA0CA,MAAa,qBAAqB;AAClC,MAAa,uBAAuB;CAChC,KAAK;CACL,KAAK;AACT;;;;;AAMA,IAAsB,gBAAtB,MAME;CAUW;CACA;CAET,YAAY,YAA2B,uBAA4B,IAAI,IAAI,GAAG;EAC1E,KAAK,cAAc;EACnB,KAAK,QAAQ;CACjB;CAEA,QAEI,OAC+D;EAC/D,OAAO,KAAK,KAAK,WAAW,EAAE,SAAS,MAAM,CAAC;CAClD;CAEA,YAE0E;EACtE,OAAO,KAAK,KAAK,aAAa,EAAE,WAAW,KAAK,CAAC;CACrD;CAEA,MAEI,OAC2D;EAC3D,OAAO,KAAK,KAAK,SAAS,EAAE,OAAO,SAAS,KAAK,CAAC;CACtD;CAEA,WAA4B;EACxB,OAAO,KAAK,OAAO;CACvB;;;;CAKA,UAEsE;EAClE,OAAO,KAAK,KAAK,WAAW,EAAE,SAAS,KAAK,CAAC;CACjD;CAEA,WAAiD;EAC7C,OAAO;GACH,GAAG,KAAK,OAAO;GACf,UAAU;EACd;CACJ;CAEA,SAEoE;EAChE,OAAO,KAAK,KAAK,UAAU,EAAE,QAAQ,KAAK,CAAC;CAC/C;;;;;CAMA,SAEI,WACA,SACyF;EACzF,OAAO,KAAK,cACR,YACA;GACI,GAAG,YAAY,KAAA,IAAY,CAAC,IAAI,EAAE,QAAQ;GAC1C,MAAM;GACN;EACJ,CACJ;CACJ;CAEU,KAAK,QAAgB,OAA2B;EACtD,IAAI,KAAK,MAAM,IAAI,MAAM,GACrB,MAAM,IAAI,MAAM,kBAAkB,OAAO,kDAAkD;EAG/F,MAAM,OAAO,IAAI,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,MAAM;EAC1C,mBAA+E,OAAO,EACjF,SAAS,sBAAsB,KAAK,IAAI,iBAAiB,CAAC;EAEhE,OAAO,QAAQ,UACX,KAAK,aACL,CACI;GACI,GAAG,KAAK;GACR,GAAG;EACP,GACA,IACJ,CACJ;CACJ;CAEU,cAAc,QAAgB,WAAgC;EACpE,MAAM,WAAW,KAAK,YAAY;EAClC,OAAO,KAAK,KACR,QACA,EACI,UAAU,CACN,GAAG,YAAY,CAAC,GAChB,SACJ,EACJ,CACJ;CACJ;CAEA,SAAwB;EAGpB,OAAO,eAAe,KAAK,WAAW;CAC1C;AACJ;AAEA,SAAS,eAAe,OAAyB;CAC7C,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO,MAAM,IAAI,cAAc;CACzD,IAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,OAAO,eAAe,KAAK,MAAM,OAAO,WAAW,OAAO;CAC7G,OAAO,OAAO,YAAY,cAAc,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,UAAU,CAChE,KACA,eAAe,IAAI,CACvB,CAAC,CAAC;AACN;AAEA,SAAgB,oBACZ,QACA,UAAU,oBACgC;CAC1C,MAAM,gBAAgB,IAAI,IAAI,MAAM;CACpC,OAAO;EACH,YAAY,CAAC,GAAG,MAAM;EACtB;EACA,MAAM;EACN,YAAY,UAAU,UAAU,QAAQ,UAAU,KAAA,KAAa,cAAc,IAAI,KAAK;CAC1F;AACJ;AAEA,SAAgB,qBACZ,MACA,OACA,SACA,UAAU,qBAAqB,OACyB;CACxD,OAAO;EACH;GACC,OAAO;EACR;EACA,YAAY,UAAU;GAClB,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;GAClD,MAAM,SAAS,QAAQ,OAAO,KAAK;GACnC,OAAO,SAAS,QAAQ,UAAU,IAAI,UAAU;EACpD;CACJ;AACJ;AAEA,SAAgB,cACZ,OACA,WACO;CACP,IAAI;CACJ,IAAI,MAAM,QAAQ,KAAK,GAAG,SAAS,CAAC,GAAG,KAAyB;MAC3D;EACD,MAAM,aAAa;EAEnB,SAAS,WAAW,UAAU,CAAC,CAC1B,QAAQ,QAAQ;GACb,MAAM,QAAQ,WAAW;GACzB,OAAO,OAAO,UAAU,YAAY,WAAW,WAAW,OAAO,GAAG;EACxE,CAAC,CAAC,CACD,KAAK,QAAQ,WAAW,IAAI,CAAC,CAC7B,QAAQ,UAA0B,cAAc,WAAW,OAAO,UAAU,WAAW,OAAO,UAAU,QAAQ;CACzH;CAGA,IAAI,CAAC,OAAO,QAAQ,MAAM,IAAI,WAAW,8BAA8B,UAAU,OAAO;CACxF,OAAO;AACX;AAEA,SAAgB,YACZ,OACA,SACyC;CACzC,OAAQ,YAAY,KAAA,IACd,QACA,CACE,OACA,OACJ;AACR"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { EnumInput, EnumValue, NextSchemaBuilder, OptionGuard, SchemaBuilder } from "./shared.js";
|
|
2
|
+
type Next<Props, Used extends string, Option extends string, Added> = NextSchemaBuilder<'string', Props, Used, Option, Added>;
|
|
3
|
+
type This<Props, Used extends string, Option extends string> = OptionGuard<Used, Option> & StringSchemaBuilder<Props, Used>;
|
|
4
|
+
export declare class StringSchemaBuilder<Props = {
|
|
5
|
+
type: StringConstructor;
|
|
6
|
+
}, Used extends string = never> extends SchemaBuilder<'string', Props, Used, string> {
|
|
7
|
+
enum<const T extends EnumInput<string>, Message extends string | undefined = undefined>(this: This<Props, Used, 'enum'>, values: T, message?: Message): Next<Props, Used, 'enum', {
|
|
8
|
+
enum: Message extends string ? {
|
|
9
|
+
message: Message;
|
|
10
|
+
values: Array<EnumValue<T, string>>;
|
|
11
|
+
} : Array<EnumValue<T, string>>;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* Mongoose measures lengths in UTF-16 code units.
|
|
15
|
+
*/
|
|
16
|
+
length<T extends number, Message extends string | undefined = undefined>(this: This<Props, Used, 'length'>, value: T, message?: Message): Next<Props, Used, 'length', {
|
|
17
|
+
maxlength: Message extends string ? [T, Message] : T;
|
|
18
|
+
minlength: Message extends string ? [T, Message] : T;
|
|
19
|
+
}>;
|
|
20
|
+
lowercase(this: This<Props, Used, 'lowercase'>): Next<Props, Used, 'lowercase', {
|
|
21
|
+
lowercase: true;
|
|
22
|
+
}>;
|
|
23
|
+
maxlength<T extends number, Message extends string | undefined = undefined>(this: This<Props, Used, 'maxlength'>, value: T, message?: Message): Next<Props, Used, 'maxlength', {
|
|
24
|
+
maxlength: Message extends string ? [T, Message] : T;
|
|
25
|
+
}>;
|
|
26
|
+
minlength<T extends number, Message extends string | undefined = undefined>(this: This<Props, Used, 'minlength'>, value: T, message?: Message): Next<Props, Used, 'minlength', {
|
|
27
|
+
minlength: Message extends string ? [T, Message] : T;
|
|
28
|
+
}>;
|
|
29
|
+
trim(this: This<Props, Used, 'trim'>): Next<Props, Used, 'trim', {
|
|
30
|
+
trim: true;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
33
|
+
export declare function string(): StringSchemaBuilder<{
|
|
34
|
+
type: StringConstructor;
|
|
35
|
+
}, never>;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.d.ts","names":[],"sources":["../../../src/schema-builders/_internals/string.ts"],"mappings":";KAYK,KAAK,OAAO,qBAAqB,uBAAuB,SAAS,4BAElE,OACA,MACA,QACA;KAGC,KAAK,OAAO,qBAAqB,yBAClC,YAAY,MAAM,UAClB,oBAAoB,OAAO;qBAElB,oBACT;EAAU,MAAM;GAChB,qCACM,wBAAwB,OAAO;EACrC,WAAW,UAAU,mBAAmB,gDACpC,MAAM,KAAK,OAAO,eAClB,QAAQ,GACR,UAAU,UACX,KACC,OACA;IAGI,MAAM;MAEE,SAAS;MACT,QAAQ,MAAM,UAAU;QAE1B,MAAM,UAAU;;;;;EAoB9B,OAAO,kBAAkB,gDACrB,MAAM,KAAK,OAAO,iBAClB,OAAO,GACP,UAAU,UACX,KACC,OACA;IAGI,WAAW,0BAA0B,GAAG,WAAW;IACnD,WAAW,0BAA0B,GAAG,WAAW;;EAa3D,UAAU,MAAM,KAAK,OAAO,qBAAqB,KAAK,OAAO;IAAqB;;EAIlF,UAAU,kBAAkB,gDACxB,MAAM,KAAK,OAAO,oBAClB,OAAO,GACP,UAAU,UACX,KAAK,OAAO;IAAqB,WAAW,0BAA0B,GAAG,WAAW;;EAIvF,UAAU,kBAAkB,gDACxB,MAAM,KAAK,OAAO,oBAClB,OAAO,GACP,UAAU,UACX,KAAK,OAAO;IAAqB,WAAW,0BAA0B,GAAG,WAAW;;EAIvF,KAAK,MAAM,KAAK,OAAO,gBAAgB,KAAK,OAAO;IAAgB;;;wBAKvD,UAAM;EArFF,MAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SchemaBuilder, getEnumValues, withMessage } from "./shared.js";
|
|
2
|
+
// src/schema-builders/_internals/string.ts
|
|
3
|
+
var StringSchemaBuilder = class extends SchemaBuilder {
|
|
4
|
+
enum(values, message) {
|
|
5
|
+
const enumValues = getEnumValues(values, "string");
|
|
6
|
+
return this.with("enum", { enum: message === void 0 ? enumValues : {
|
|
7
|
+
message,
|
|
8
|
+
values: enumValues
|
|
9
|
+
} });
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Mongoose measures lengths in UTF-16 code units.
|
|
13
|
+
*/
|
|
14
|
+
length(value, message) {
|
|
15
|
+
const limit = withMessage(value, message);
|
|
16
|
+
return this.with("length", {
|
|
17
|
+
maxlength: limit,
|
|
18
|
+
minlength: limit
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
lowercase() {
|
|
22
|
+
return this.with("lowercase", { lowercase: true });
|
|
23
|
+
}
|
|
24
|
+
maxlength(value, message) {
|
|
25
|
+
return this.with("maxlength", { maxlength: withMessage(value, message) });
|
|
26
|
+
}
|
|
27
|
+
minlength(value, message) {
|
|
28
|
+
return this.with("minlength", { minlength: withMessage(value, message) });
|
|
29
|
+
}
|
|
30
|
+
trim() {
|
|
31
|
+
return this.with("trim", { trim: true });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
function string() {
|
|
35
|
+
return new StringSchemaBuilder({ type: String });
|
|
36
|
+
}
|
|
37
|
+
export { StringSchemaBuilder, string };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.js","names":[],"sources":["../../../src/schema-builders/_internals/string.ts"],"sourcesContent":["import type {\n EnumInput,\n EnumValue,\n NextSchemaBuilder,\n OptionGuard,\n} from './shared';\nimport {\n getEnumValues,\n SchemaBuilder,\n withMessage,\n} from './shared';\n\ntype Next<Props, Used extends string, Option extends string, Added> = NextSchemaBuilder<\n 'string',\n Props,\n Used,\n Option,\n Added\n>;\n\ntype This<Props, Used extends string, Option extends string> =\n & OptionGuard<Used, Option>\n & StringSchemaBuilder<Props, Used>;\n\nexport class StringSchemaBuilder<\n Props = { type: StringConstructor },\n Used extends string = never,\n> extends SchemaBuilder<'string', Props, Used, string> {\n enum<const T extends EnumInput<string>, Message extends string | undefined = undefined>(\n this: This<Props, Used, 'enum'>,\n values: T,\n message?: Message,\n ): Next<\n Props,\n Used,\n 'enum',\n {\n enum: Message extends string\n ? {\n message: Message;\n values: Array<EnumValue<T, string>>;\n }\n : Array<EnumValue<T, string>>;\n }\n > {\n const enumValues = getEnumValues(values, 'string');\n return this.with(\n 'enum',\n {\n enum: message === undefined\n ? enumValues\n : {\n message,\n values: enumValues,\n },\n },\n );\n }\n\n /**\n * Mongoose measures lengths in UTF-16 code units.\n */\n length<T extends number, Message extends string | undefined = undefined>(\n this: This<Props, Used, 'length'>,\n value: T,\n message?: Message,\n ): Next<\n Props,\n Used,\n 'length',\n {\n maxlength: Message extends string ? [T, Message] : T;\n minlength: Message extends string ? [T, Message] : T;\n }\n > {\n const limit = withMessage(value, message);\n return this.with(\n 'length',\n {\n maxlength: limit,\n minlength: limit,\n },\n );\n }\n\n lowercase(this: This<Props, Used, 'lowercase'>): Next<Props, Used, 'lowercase', { lowercase: true }> {\n return this.with('lowercase', { lowercase: true });\n }\n\n maxlength<T extends number, Message extends string | undefined = undefined>(\n this: This<Props, Used, 'maxlength'>,\n value: T,\n message?: Message,\n ): Next<Props, Used, 'maxlength', { maxlength: Message extends string ? [T, Message] : T }> {\n return this.with('maxlength', { maxlength: withMessage(value, message) });\n }\n\n minlength<T extends number, Message extends string | undefined = undefined>(\n this: This<Props, Used, 'minlength'>,\n value: T,\n message?: Message,\n ): Next<Props, Used, 'minlength', { minlength: Message extends string ? [T, Message] : T }> {\n return this.with('minlength', { minlength: withMessage(value, message) });\n }\n\n trim(this: This<Props, Used, 'trim'>): Next<Props, Used, 'trim', { trim: true }> {\n return this.with('trim', { trim: true });\n }\n}\n\nexport function string() {\n return new StringSchemaBuilder({ type: String });\n}\n"],"mappings":";;AAwBA,IAAa,sBAAb,cAGU,cAA6C;CACnD,KAEI,QACA,SAaF;EACE,MAAM,aAAa,cAAc,QAAQ,QAAQ;EACjD,OAAO,KAAK,KACR,QACA,EACI,MAAM,YAAY,KAAA,IACZ,aACA;GACE;GACA,QAAQ;EACZ,EACR,CACJ;CACJ;;;;CAKA,OAEI,OACA,SASF;EACE,MAAM,QAAQ,YAAY,OAAO,OAAO;EACxC,OAAO,KAAK,KACR,UACA;GACI,WAAW;GACX,WAAW;EACf,CACJ;CACJ;CAEA,YAAqG;EACjG,OAAO,KAAK,KAAK,aAAa,EAAE,WAAW,KAAK,CAAC;CACrD;CAEA,UAEI,OACA,SACwF;EACxF,OAAO,KAAK,KAAK,aAAa,EAAE,WAAW,YAAY,OAAO,OAAO,EAAE,CAAC;CAC5E;CAEA,UAEI,OACA,SACwF;EACxF,OAAO,KAAK,KAAK,aAAa,EAAE,WAAW,YAAY,OAAO,OAAO,EAAE,CAAC;CAC5E;CAEA,OAAiF;EAC7E,OAAO,KAAK,KAAK,QAAQ,EAAE,MAAM,KAAK,CAAC;CAC3C;AACJ;AAEA,SAAgB,SAAS;CACrB,OAAO,IAAI,oBAAoB,EAAE,MAAM,OAAO,CAAC;AACnD"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { BooleanSchemaBuilder,
|
|
3
|
-
import { DateSchemaBuilder,
|
|
4
|
-
import { Decimal128SchemaBuilder,
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { StringSchemaBuilder,
|
|
12
|
-
|
|
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
|
+
import { ArraySchemaBuilder, array } from "./_internals/array.js";
|
|
2
|
+
import { BooleanSchemaBuilder, boolean } from "./_internals/boolean.js";
|
|
3
|
+
import { DateSchemaBuilder, date } from "./_internals/date.js";
|
|
4
|
+
import { Decimal128SchemaBuilder, decimal128 } from "./_internals/decimal128.js";
|
|
5
|
+
import { Int32SchemaBuilder, int32 } from "./_internals/int32.js";
|
|
6
|
+
import { MixedSchemaBuilder, mixed } from "./_internals/mixed.js";
|
|
7
|
+
import { NumberSchemaBuilder, number } from "./_internals/number.js";
|
|
8
|
+
import { ObjectIdSchemaBuilder, objectId } from "./_internals/object-id.js";
|
|
9
|
+
import { RefSchemaBuilder, ref } from "./_internals/ref.js";
|
|
10
|
+
import { SchemaBuilder, UseOption, Validator } from "./_internals/shared.js";
|
|
11
|
+
import { StringSchemaBuilder, string } from "./_internals/string.js";
|
|
12
|
+
export { type ArraySchemaBuilder, type BooleanSchemaBuilder, type DateSchemaBuilder, type Decimal128SchemaBuilder, type Int32SchemaBuilder, type MixedSchemaBuilder, type NumberSchemaBuilder, type ObjectIdSchemaBuilder, type RefSchemaBuilder, type SchemaBuilder, type StringSchemaBuilder, type UseOption, type Validator, array, boolean, date, decimal128, int32, mixed, number, objectId, ref, string };
|