ts-fortress 4.2.0 → 4.3.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/dist/entry-point.mjs +2 -1
- package/dist/entry-point.mjs.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/record/index.d.mts +1 -0
- package/dist/record/index.d.mts.map +1 -1
- package/dist/record/index.mjs +1 -0
- package/dist/record/index.mjs.map +1 -1
- package/dist/record/omit.mjs +2 -2
- package/dist/record/omit.mjs.map +1 -1
- package/dist/record/optional.d.mts +1 -0
- package/dist/record/optional.d.mts.map +1 -1
- package/dist/record/optional.mjs.map +1 -1
- package/dist/record/partial.mjs +2 -2
- package/dist/record/partial.mjs.map +1 -1
- package/dist/record/pick.mjs +2 -2
- package/dist/record/pick.mjs.map +1 -1
- package/dist/record/required.d.mts +22 -0
- package/dist/record/required.d.mts.map +1 -0
- package/dist/record/required.mjs +33 -0
- package/dist/record/required.mjs.map +1 -0
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/to-union-string.d.mts +1 -0
- package/dist/utils/to-union-string.d.mts.map +1 -1
- package/dist/utils/to-union-string.mjs +4 -3
- package/dist/utils/to-union-string.mjs.map +1 -1
- package/package.json +1 -1
- package/src/compose/intersection.test.mts +10 -11
- package/src/compose/merge-records.test.mts +2 -3
- package/src/compose/union.test.mts +2 -3
- package/src/record/index.mts +1 -0
- package/src/record/omit.mts +2 -2
- package/src/record/optional.mts +5 -0
- package/src/record/partial.mts +3 -3
- package/src/record/pick.mts +2 -2
- package/src/record/required.mts +246 -0
- package/src/record/required.test.mts +600 -0
- package/src/utils/to-union-string.mts +5 -2
package/dist/entry-point.mjs
CHANGED
|
@@ -52,12 +52,13 @@ export { isOptionalProperty, optional } from './record/optional.mjs';
|
|
|
52
52
|
export { partial } from './record/partial.mjs';
|
|
53
53
|
export { pick } from './record/pick.mjs';
|
|
54
54
|
export { record, strictRecord } from './record/record.mjs';
|
|
55
|
+
export { required } from './record/required.mjs';
|
|
55
56
|
export { valueof } from './record/valueof.mjs';
|
|
56
57
|
export { createAssertFn } from './utils/create-assert-fn.mjs';
|
|
57
58
|
export { createCastFn } from './utils/create-cast-fn.mjs';
|
|
58
59
|
export { createIsFn } from './utils/create-is-fn.mjs';
|
|
59
60
|
export { createPrimitiveType } from './utils/create-primitive-type.mjs';
|
|
60
61
|
export { createType } from './utils/create-type.mjs';
|
|
61
|
-
export { toIntersectionString, toUnionString } from './utils/to-union-string.mjs';
|
|
62
|
+
export { toIntersectionString, toUnionKeyString, toUnionString } from './utils/to-union-string.mjs';
|
|
62
63
|
export { createPrimitiveValidationError, prependIndexToValidationErrors, prependPathToValidationErrors, validationErrorToMessage, validationErrorsToMessages } from './utils/validation-error.mjs';
|
|
63
64
|
//# sourceMappingURL=entry-point.mjs.map
|
package/dist/entry-point.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"entry-point.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -51,6 +51,7 @@ export { isOptionalProperty, optional } from './record/optional.mjs';
|
|
|
51
51
|
export { partial } from './record/partial.mjs';
|
|
52
52
|
export { pick } from './record/pick.mjs';
|
|
53
53
|
export { record, strictRecord } from './record/record.mjs';
|
|
54
|
+
export { required } from './record/required.mjs';
|
|
54
55
|
export { valueof } from './record/valueof.mjs';
|
|
55
56
|
import 'ts-data-forge';
|
|
56
57
|
export { createAssertFn } from './utils/create-assert-fn.mjs';
|
|
@@ -58,6 +59,6 @@ export { createCastFn } from './utils/create-cast-fn.mjs';
|
|
|
58
59
|
export { createIsFn } from './utils/create-is-fn.mjs';
|
|
59
60
|
export { createPrimitiveType } from './utils/create-primitive-type.mjs';
|
|
60
61
|
export { createType } from './utils/create-type.mjs';
|
|
61
|
-
export { toIntersectionString, toUnionString } from './utils/to-union-string.mjs';
|
|
62
|
+
export { toIntersectionString, toUnionKeyString, toUnionString } from './utils/to-union-string.mjs';
|
|
62
63
|
export { createPrimitiveValidationError, prependIndexToValidationErrors, prependPathToValidationErrors, validationErrorToMessage, validationErrorsToMessages } from './utils/validation-error.mjs';
|
|
63
64
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/record/index.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/record/index.mts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/record/index.mts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC"}
|
package/dist/record/index.mjs
CHANGED
|
@@ -5,5 +5,6 @@ export { isOptionalProperty, optional } from './optional.mjs';
|
|
|
5
5
|
export { partial } from './partial.mjs';
|
|
6
6
|
export { pick } from './pick.mjs';
|
|
7
7
|
export { record, strictRecord } from './record.mjs';
|
|
8
|
+
export { required } from './required.mjs';
|
|
8
9
|
export { valueof } from './valueof.mjs';
|
|
9
10
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
package/dist/record/omit.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Obj } from 'ts-data-forge';
|
|
2
|
-
import {
|
|
2
|
+
import { toUnionKeyString } from '../utils/to-union-string.mjs';
|
|
3
3
|
import { record } from './record.mjs';
|
|
4
4
|
|
|
5
5
|
/** Creates a record type with keys omitted. */
|
|
6
6
|
const omit = (recordType, keysToOmit, options) => record(Obj.omit(recordType.shape, keysToOmit), {
|
|
7
7
|
typeName: options?.typeName ??
|
|
8
|
-
`Omit<${recordType.typeName}, ${
|
|
8
|
+
`Omit<${recordType.typeName}, ${toUnionKeyString(keysToOmit)}>`,
|
|
9
9
|
allowExcessProperties: options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
10
10
|
});
|
|
11
11
|
|
package/dist/record/omit.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"omit.mjs","sources":["../../src/record/omit.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAKA;AACO,MAAM,IAAI,GAAG,CAIlB,UAAyB,EACzB,UAAsB,EACtB,OAKE,KAEF,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE;IAC7C,QAAQ,EACN,OAAO,EAAE,QAAQ;QACjB,CAAA,KAAA,EAAQ,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,
|
|
1
|
+
{"version":3,"file":"omit.mjs","sources":["../../src/record/omit.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAKA;AACO,MAAM,IAAI,GAAG,CAIlB,UAAyB,EACzB,UAAsB,EACtB,OAKE,KAEF,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE;IAC7C,QAAQ,EACN,OAAO,EAAE,QAAQ;QACjB,CAAA,KAAA,EAAQ,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,gBAAgB,CAAC,UAAU,CAAC,CAAA,CAAA,CAAG;AACjE,IAAA,qBAAqB,EACnB,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAAC,qBAAqB;AACrE,CAAA;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Type } from '../type.mjs';
|
|
2
2
|
export type OptionalPropertyType<T extends Type<unknown>> = T & PartiallyRequired<T, 'optional'>;
|
|
3
|
+
export type RequiredPropertyType<T extends Type<unknown>> = PartiallyOptional<T, 'optional'>;
|
|
3
4
|
export declare const optional: <T extends Type<unknown>>(t: T) => OptionalPropertyType<T>;
|
|
4
5
|
export declare const isOptionalProperty: <T extends Type<unknown>>(t: T) => t is OptionalPropertyType<T>;
|
|
5
6
|
//# sourceMappingURL=optional.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optional.d.mts","sourceRoot":"","sources":["../../src/record/optional.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAC3D,iBAAiB,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;AAEnC,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,EAC9C,GAAG,CAAC,KACH,oBAAoB,CAAC,CAAC,CAGvB,CAAC;AAEH,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,EACxD,GAAG,CAAC,KACH,CAAC,IAAI,oBAAoB,CAAC,CAAC,CACuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"optional.d.mts","sourceRoot":"","sources":["../../src/record/optional.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAC3D,iBAAiB,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;AAEnC,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,iBAAiB,CAC3E,CAAC,EACD,UAAU,CACX,CAAC;AAEF,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,EAC9C,GAAG,CAAC,KACH,oBAAoB,CAAC,CAAC,CAGvB,CAAC;AAEH,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,EACxD,GAAG,CAAC,KACH,CAAC,IAAI,oBAAoB,CAAC,CAAC,CACuB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optional.mjs","sources":["../../src/record/optional.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"optional.mjs","sources":["../../src/record/optional.mts"],"sourcesContent":[null],"names":[],"mappings":"MAUa,QAAQ,GAAG,CACtB,CAAI,MACyB;AAC7B,IAAA,GAAG,CAAC;AACJ,IAAA,QAAQ,EAAE,IAAI;AACf,CAAA;MAEY,kBAAkB,GAAG,CAChC,CAAI,KAEJ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,QAAQ,KAAK;;;;"}
|
package/dist/record/partial.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'ts-data-forge';
|
|
2
|
-
import {
|
|
2
|
+
import { toUnionKeyString } from '../utils/to-union-string.mjs';
|
|
3
3
|
import { optional } from './optional.mjs';
|
|
4
4
|
import { record } from './record.mjs';
|
|
5
5
|
|
|
@@ -11,7 +11,7 @@ const partial = (recordType, options) => {
|
|
|
11
11
|
const typeNameFilled = options?.typeName ??
|
|
12
12
|
(options?.keysToBeOptional === undefined
|
|
13
13
|
? `Partial<${recordType.typeName}>`
|
|
14
|
-
: `PartiallyPartial<${recordType.typeName}, ${
|
|
14
|
+
: `PartiallyPartial<${recordType.typeName}, ${toUnionKeyString(options.keysToBeOptional)}>`);
|
|
15
15
|
const keysToBeOptional = new Set(options?.keysToBeOptional ?? Object.keys(recordType.shape));
|
|
16
16
|
const partialShape =
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"partial.mjs","sources":["../../src/record/partial.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAMA;;;AAGG;MACU,OAAO,GAAG,CAIrB,UAAyB,EACzB,OAME,KACkC;AACpC,IAAA,MAAM,cAAc,GAClB,OAAO,EAAE,QAAQ;AACjB,SAAC,OAAO,EAAE,gBAAgB,KAAK;AAC7B,cAAE,CAAA,QAAA,EAAW,UAAU,CAAC,QAAQ,CAAA,CAAA;AAChC,cAAE,CAAA,iBAAA,EAAoB,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,
|
|
1
|
+
{"version":3,"file":"partial.mjs","sources":["../../src/record/partial.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAMA;;;AAGG;MACU,OAAO,GAAG,CAIrB,UAAyB,EACzB,OAME,KACkC;AACpC,IAAA,MAAM,cAAc,GAClB,OAAO,EAAE,QAAQ;AACjB,SAAC,OAAO,EAAE,gBAAgB,KAAK;AAC7B,cAAE,CAAA,QAAA,EAAW,UAAU,CAAC,QAAQ,CAAA,CAAA;AAChC,cAAE,CAAA,iBAAA,EAAoB,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,gBAAgB,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA,CAAA,CAAG,CAAC;AAEhG,IAAA,MAAM,gBAAgB,GAAwB,IAAI,GAAG,CACnD,OAAO,EAAE,gBAAgB,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAC3D;AAED,IAAA,MAAM,YAAY;;IAEhB,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,GAAG,CAClC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAU,CACpE,CAIF;IAEH,OAAO,MAAM,CAAC,YAAY,EAAE;AAC1B,QAAA,QAAQ,EAAE,cAAc;AACxB,QAAA,qBAAqB,EACnB,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAAC,qBAAqB;AACrE,KAAA,CAAC;AACJ;;;;"}
|
package/dist/record/pick.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Obj } from 'ts-data-forge';
|
|
2
|
-
import {
|
|
2
|
+
import { toUnionKeyString } from '../utils/to-union-string.mjs';
|
|
3
3
|
import { record } from './record.mjs';
|
|
4
4
|
|
|
5
5
|
/** Creates a record type with keys picked. */
|
|
6
6
|
const pick = (recordType, keysToPick, options) => record(Obj.pick(recordType.shape, keysToPick), {
|
|
7
7
|
typeName: options?.typeName ??
|
|
8
|
-
`Pick<${recordType.typeName}, ${
|
|
8
|
+
`Pick<${recordType.typeName}, ${toUnionKeyString(keysToPick)}>`,
|
|
9
9
|
allowExcessProperties: options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
10
10
|
});
|
|
11
11
|
|
package/dist/record/pick.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pick.mjs","sources":["../../src/record/pick.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAKA;AACO,MAAM,IAAI,GAAG,CAIlB,UAAyB,EACzB,UAAsB,EACtB,OAKE,KAEF,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE;IAC7C,QAAQ,EACN,OAAO,EAAE,QAAQ;QACjB,CAAA,KAAA,EAAQ,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,
|
|
1
|
+
{"version":3,"file":"pick.mjs","sources":["../../src/record/pick.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAKA;AACO,MAAM,IAAI,GAAG,CAIlB,UAAyB,EACzB,UAAsB,EACtB,OAKE,KAEF,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE;IAC7C,QAAQ,EACN,OAAO,EAAE,QAAQ;QACjB,CAAA,KAAA,EAAQ,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,gBAAgB,CAAC,UAAU,CAAC,CAAA,CAAA,CAAG;AACjE,IAAA,qBAAqB,EACnB,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAAC,qBAAqB;AACrE,CAAA;;;;"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type RecordType, type Type } from '../type.mjs';
|
|
2
|
+
import { type RequiredPropertyType } from './optional.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Required type. If keysToBeRequired is set, only those keys are
|
|
5
|
+
* made required, otherwise, all properties are made required.
|
|
6
|
+
*/
|
|
7
|
+
export declare const required: <const R extends ReadonlyRecord<string, Type<unknown>>, const KeysToBeRequired extends NonEmptyArray<keyof R & string>>(recordType: RecordType<R>, options?: PartialReadonly<{
|
|
8
|
+
keysToBeRequired: KeysToBeRequired;
|
|
9
|
+
typeName: string;
|
|
10
|
+
/** @default true */
|
|
11
|
+
allowExcessProperties: boolean;
|
|
12
|
+
}>) => RequiredType<R, KeysToBeRequired>;
|
|
13
|
+
type RequiredTypeShape<R extends ReadonlyRecord<string, Type<unknown>>, KeysToBeRequired extends NonEmptyArray<keyof R & string> | undefined> = TypeEq<KeysToBeRequired, undefined> extends true ? FullyRequiredType<R> : PartiallyRequiredType<R, ArrayElement<KeysToBeRequired>>;
|
|
14
|
+
type FullyRequiredType<R extends ReadonlyRecord<string, Type<unknown>>> = {
|
|
15
|
+
readonly [P in keyof R]: RequiredPropertyType<R[P]>;
|
|
16
|
+
};
|
|
17
|
+
type PartiallyRequiredType<R extends ReadonlyRecord<string, Type<unknown>>, K extends keyof R> = {
|
|
18
|
+
readonly [P in keyof R]: P extends K ? RequiredPropertyType<R[P]> : R[P];
|
|
19
|
+
};
|
|
20
|
+
export type RequiredType<R extends ReadonlyRecord<string, Type<unknown>>, KeysToBeRequired extends NonEmptyArray<keyof R & string> | undefined> = RecordType<RequiredTypeShape<R, KeysToBeRequired>>;
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=required.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"required.d.mts","sourceRoot":"","sources":["../../src/record/required.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,gBAAgB,CAAC;AAGxB;;;GAGG;AACH,eAAO,MAAM,QAAQ,GACnB,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EACrD,KAAK,CAAC,gBAAgB,SAAS,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,EAE9D,YAAY,UAAU,CAAC,CAAC,CAAC,EACzB,UAAU,eAAe,CAAC;IACxB,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,QAAQ,EAAE,MAAM,CAAC;IAEjB,oBAAoB;IACpB,qBAAqB,EAAE,OAAO,CAAC;CAChC,CAAC,KACD,YAAY,CAAC,CAAC,EAAE,gBAAgB,CA2BlC,CAAC;AAaF,KAAK,iBAAiB,CACpB,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC/C,gBAAgB,SAAS,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,SAAS,IAEpE,MAAM,CAAC,gBAAgB,EAAE,SAAS,CAAC,SAAS,IAAI,GAC5C,iBAAiB,CAAC,CAAC,CAAC,GACpB,qBAAqB,CAAC,CAAC,EAAE,YAAY,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAE/D,KAAK,iBAAiB,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI;IACxE,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACpD,CAAC;AAEF,KAAK,qBAAqB,CACxB,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC/C,CAAC,SAAS,MAAM,CAAC,IACf;IACF,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACzE,CAAC;AAEF,MAAM,MAAM,YAAY,CACtB,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC/C,gBAAgB,SAAS,aAAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,SAAS,IAClE,UAAU,CAAC,iBAAiB,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Obj } from 'ts-data-forge';
|
|
2
|
+
import { toUnionKeyString } from '../utils/to-union-string.mjs';
|
|
3
|
+
import { isOptionalProperty } from './optional.mjs';
|
|
4
|
+
import { record } from './record.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Required type. If keysToBeRequired is set, only those keys are
|
|
8
|
+
* made required, otherwise, all properties are made required.
|
|
9
|
+
*/
|
|
10
|
+
const required = (recordType, options) => {
|
|
11
|
+
const typeNameFilled = options?.typeName ??
|
|
12
|
+
(options?.keysToBeRequired === undefined
|
|
13
|
+
? `Required<${recordType.typeName}>`
|
|
14
|
+
: `PartiallyRequired<${recordType.typeName}, ${toUnionKeyString(options.keysToBeRequired)}>`);
|
|
15
|
+
const keysToBeRequired = new Set(options?.keysToBeRequired ?? Object.keys(recordType.shape));
|
|
16
|
+
const requiredShape =
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
18
|
+
Object.fromEntries(Object.entries(recordType.shape).map(([k, v]) => [k, keysToBeRequired.has(k) ? makeRequired(v) : v]));
|
|
19
|
+
return record(requiredShape, {
|
|
20
|
+
typeName: typeNameFilled,
|
|
21
|
+
allowExcessProperties: options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Makes an optional property required by removing the optional flag.
|
|
26
|
+
*/
|
|
27
|
+
const makeRequired = (t) => isOptionalProperty(t)
|
|
28
|
+
? // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
29
|
+
Obj.omit(t, ['optional'])
|
|
30
|
+
: t;
|
|
31
|
+
|
|
32
|
+
export { required };
|
|
33
|
+
//# sourceMappingURL=required.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"required.mjs","sources":["../../src/record/required.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAUA;;;AAGG;MACU,QAAQ,GAAG,CAItB,UAAyB,EACzB,OAME,KACmC;AACrC,IAAA,MAAM,cAAc,GAClB,OAAO,EAAE,QAAQ;AACjB,SAAC,OAAO,EAAE,gBAAgB,KAAK;AAC7B,cAAE,CAAA,SAAA,EAAY,UAAU,CAAC,QAAQ,CAAA,CAAA;AACjC,cAAE,CAAA,kBAAA,EAAqB,UAAU,CAAC,QAAQ,CAAA,EAAA,EAAK,gBAAgB,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA,CAAA,CAAG,CAAC;AAEjG,IAAA,MAAM,gBAAgB,GAAwB,IAAI,GAAG,CACnD,OAAO,EAAE,gBAAgB,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAC3D;AAED,IAAA,MAAM,aAAa;;IAEjB,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,GAAG,CAClC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAU,CACxE,CAIF;IAEH,OAAO,MAAM,CAAC,aAAa,EAAE;AAC3B,QAAA,QAAQ,EAAE,cAAc;AACxB,QAAA,qBAAqB,EACnB,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAAC,qBAAqB;AACrE,KAAA,CAAC;AACJ;AAEA;;AAEG;AACH,MAAM,YAAY,GAAG,CACnB,CAAI,KAEJ,kBAAkB,CAAC,CAAC;AAClB;QACG,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC;MACzB,CAAC;;;;"}
|
package/dist/utils/index.mjs
CHANGED
|
@@ -3,6 +3,6 @@ export { createCastFn } from './create-cast-fn.mjs';
|
|
|
3
3
|
export { createIsFn } from './create-is-fn.mjs';
|
|
4
4
|
export { createPrimitiveType } from './create-primitive-type.mjs';
|
|
5
5
|
export { createType } from './create-type.mjs';
|
|
6
|
-
export { toIntersectionString, toUnionString } from './to-union-string.mjs';
|
|
6
|
+
export { toIntersectionString, toUnionKeyString, toUnionString } from './to-union-string.mjs';
|
|
7
7
|
export { createPrimitiveValidationError, prependIndexToValidationErrors, prependPathToValidationErrors, validationErrorToMessage, validationErrorsToMessages } from './validation-error.mjs';
|
|
8
8
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare const toUnionKeyString: (members: readonly string[]) => string;
|
|
1
2
|
export declare const toUnionString: (members: readonly string[]) => string;
|
|
2
3
|
export declare const toIntersectionString: (members: readonly string[]) => string;
|
|
3
4
|
//# sourceMappingURL=to-union-string.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-union-string.d.mts","sourceRoot":"","sources":["../../src/utils/to-union-string.mts"],"names":[],"mappings":"AAAA,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"to-union-string.d.mts","sourceRoot":"","sources":["../../src/utils/to-union-string.mts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,GAAI,SAAS,SAAS,MAAM,EAAE,KAAG,MACpB,CAAC;AAE3C,eAAO,MAAM,aAAa,GAAI,SAAS,SAAS,MAAM,EAAE,KAAG,MACtC,CAAC;AAEtB,eAAO,MAAM,oBAAoB,GAAI,SAAS,SAAS,MAAM,EAAE,KAAG,MAC7C,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const toUnionKeyString = (members) => members.map((k) => `"${k}"`).join(' | ');
|
|
2
|
+
const toUnionString = (members) => members.join(' | ');
|
|
3
|
+
const toIntersectionString = (members) => members.join(' & ');
|
|
3
4
|
|
|
4
|
-
export { toIntersectionString, toUnionString };
|
|
5
|
+
export { toIntersectionString, toUnionKeyString, toUnionString };
|
|
5
6
|
//# sourceMappingURL=to-union-string.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-union-string.mjs","sources":["../../src/utils/to-union-string.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"to-union-string.mjs","sources":["../../src/utils/to-union-string.mts"],"sourcesContent":[null],"names":[],"mappings":"AAAO,MAAM,gBAAgB,GAAG,CAAC,OAA0B,KACzD,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAA,CAAA,EAAI,CAAC,CAAA,CAAA,CAAG,CAAC,CAAC,IAAI,CAAC,KAAK;AAElC,MAAM,aAAa,GAAG,CAAC,OAA0B,KACtD,OAAO,CAAC,IAAI,CAAC,KAAK;AAEb,MAAM,oBAAoB,GAAG,CAAC,OAA0B,KAC7D,OAAO,CAAC,IAAI,CAAC,KAAK;;;;"}
|
package/package.json
CHANGED
|
@@ -109,9 +109,8 @@ describe('intersection', () => {
|
|
|
109
109
|
path: [],
|
|
110
110
|
actualValue: { x: 0, y: 1 },
|
|
111
111
|
expectedType:
|
|
112
|
-
'(
|
|
113
|
-
typeName:
|
|
114
|
-
'("{ x: number, y: number }" & "{ z: number, w: number }")',
|
|
112
|
+
'({ x: number, y: number } & { z: number, w: number })',
|
|
113
|
+
typeName: '({ x: number, y: number } & { z: number, w: number })',
|
|
115
114
|
message:
|
|
116
115
|
'The type of value is expected to match all types of { { x: number, y: number }, { z: number, w: number } }',
|
|
117
116
|
},
|
|
@@ -221,10 +220,10 @@ describe('intersection', () => {
|
|
|
221
220
|
{
|
|
222
221
|
path: [],
|
|
223
222
|
actualValue: 'aaa',
|
|
224
|
-
expectedType: '(
|
|
223
|
+
expectedType: '(number & number)',
|
|
225
224
|
message:
|
|
226
225
|
'The type of value is expected to match all types of { number, number }',
|
|
227
|
-
typeName: '(
|
|
226
|
+
typeName: '(number & number)',
|
|
228
227
|
},
|
|
229
228
|
{
|
|
230
229
|
path: [],
|
|
@@ -236,10 +235,10 @@ describe('intersection', () => {
|
|
|
236
235
|
{
|
|
237
236
|
path: [],
|
|
238
237
|
actualValue: 'aaa',
|
|
239
|
-
expectedType: '(
|
|
238
|
+
expectedType: '(number & number)',
|
|
240
239
|
message:
|
|
241
240
|
'The type of value is expected to match all types of { number, number }',
|
|
242
|
-
typeName: '(
|
|
241
|
+
typeName: '(number & number)',
|
|
243
242
|
},
|
|
244
243
|
{
|
|
245
244
|
path: [],
|
|
@@ -356,10 +355,10 @@ describe('intersection', () => {
|
|
|
356
355
|
{
|
|
357
356
|
path: [],
|
|
358
357
|
actualValue: 7,
|
|
359
|
-
expectedType: '(
|
|
358
|
+
expectedType: '(uintRange(0, 5) & uintRange(1, 7))',
|
|
360
359
|
message:
|
|
361
360
|
'The type of value is expected to match all types of { uintRange(0, 5), uintRange(1, 7) }',
|
|
362
|
-
typeName: '(
|
|
361
|
+
typeName: '(uintRange(0, 5) & uintRange(1, 7))',
|
|
363
362
|
},
|
|
364
363
|
{
|
|
365
364
|
path: [],
|
|
@@ -371,10 +370,10 @@ describe('intersection', () => {
|
|
|
371
370
|
{
|
|
372
371
|
path: [],
|
|
373
372
|
actualValue: 7,
|
|
374
|
-
expectedType: '(
|
|
373
|
+
expectedType: '(uintRange(0, 5) & uintRange(1, 7))',
|
|
375
374
|
message:
|
|
376
375
|
'The type of value is expected to match all types of { uintRange(0, 5), uintRange(1, 7) }',
|
|
377
|
-
typeName: '(
|
|
376
|
+
typeName: '(uintRange(0, 5) & uintRange(1, 7))',
|
|
378
377
|
},
|
|
379
378
|
{
|
|
380
379
|
path: [],
|
|
@@ -101,9 +101,8 @@ describe('mergeRecords', () => {
|
|
|
101
101
|
path: [],
|
|
102
102
|
actualValue: { x: 0, y: 1 },
|
|
103
103
|
expectedType:
|
|
104
|
-
'(
|
|
105
|
-
typeName:
|
|
106
|
-
'("{ x: number, y: number }" & "{ z: number, w: number }")',
|
|
104
|
+
'({ x: number, y: number } & { z: number, w: number })',
|
|
105
|
+
typeName: '({ x: number, y: number } & { z: number, w: number })',
|
|
107
106
|
message:
|
|
108
107
|
'The type of value is expected to match all types of { { x: number, y: number }, { z: number, w: number } }',
|
|
109
108
|
},
|
|
@@ -91,9 +91,8 @@ describe('union', () => {
|
|
|
91
91
|
path: [],
|
|
92
92
|
actualValue: 5,
|
|
93
93
|
expectedType:
|
|
94
|
-
'(
|
|
95
|
-
typeName:
|
|
96
|
-
'("{ x: number, y: number }" | "literal(3)" | "literal("2")")',
|
|
94
|
+
'({ x: number, y: number } | literal(3) | literal("2"))',
|
|
95
|
+
typeName: '({ x: number, y: number } | literal(3) | literal("2"))',
|
|
97
96
|
message:
|
|
98
97
|
'The type of value is expected to be one of the elements contained in { { x: number, y: number }, literal(3), literal("2") }',
|
|
99
98
|
});
|
package/src/record/index.mts
CHANGED
package/src/record/omit.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, Obj } from 'ts-data-forge';
|
|
2
2
|
import { type RecordType, type Type } from '../type.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { toUnionKeyString } from '../utils/index.mjs';
|
|
4
4
|
import { record } from './record.mjs';
|
|
5
5
|
|
|
6
6
|
/** Creates a record type with keys omitted. */
|
|
@@ -20,7 +20,7 @@ export const omit = <
|
|
|
20
20
|
record(Obj.omit(recordType.shape, keysToOmit), {
|
|
21
21
|
typeName:
|
|
22
22
|
options?.typeName ??
|
|
23
|
-
`Omit<${recordType.typeName}, ${
|
|
23
|
+
`Omit<${recordType.typeName}, ${toUnionKeyString(keysToOmit)}>`,
|
|
24
24
|
allowExcessProperties:
|
|
25
25
|
options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
26
26
|
});
|
package/src/record/optional.mts
CHANGED
|
@@ -3,6 +3,11 @@ import { type Type } from '../type.mjs';
|
|
|
3
3
|
export type OptionalPropertyType<T extends Type<unknown>> = T &
|
|
4
4
|
PartiallyRequired<T, 'optional'>;
|
|
5
5
|
|
|
6
|
+
export type RequiredPropertyType<T extends Type<unknown>> = PartiallyOptional<
|
|
7
|
+
T,
|
|
8
|
+
'optional'
|
|
9
|
+
>;
|
|
10
|
+
|
|
6
11
|
export const optional = <T extends Type<unknown>>(
|
|
7
12
|
t: T,
|
|
8
13
|
): OptionalPropertyType<T> => ({
|
package/src/record/partial.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType } from 'ts-data-forge';
|
|
2
2
|
import { type RecordType, type Type } from '../type.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { toUnionKeyString } from '../utils/index.mjs';
|
|
4
4
|
import { optional, type OptionalPropertyType } from './optional.mjs';
|
|
5
5
|
import { record } from './record.mjs';
|
|
6
6
|
|
|
@@ -25,7 +25,7 @@ export const partial = <
|
|
|
25
25
|
options?.typeName ??
|
|
26
26
|
(options?.keysToBeOptional === undefined
|
|
27
27
|
? `Partial<${recordType.typeName}>`
|
|
28
|
-
: `PartiallyPartial<${recordType.typeName}, ${
|
|
28
|
+
: `PartiallyPartial<${recordType.typeName}, ${toUnionKeyString(options.keysToBeOptional)}>`);
|
|
29
29
|
|
|
30
30
|
const keysToBeOptional: ReadonlySet<string> = new Set(
|
|
31
31
|
options?.keysToBeOptional ?? Object.keys(recordType.shape),
|
|
@@ -105,7 +105,7 @@ expectType<
|
|
|
105
105
|
}>;
|
|
106
106
|
allowExcessProperties: boolean;
|
|
107
107
|
}>
|
|
108
|
-
>('
|
|
108
|
+
>('=');
|
|
109
109
|
|
|
110
110
|
expectType<
|
|
111
111
|
RecordType<
|
package/src/record/pick.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, Obj } from 'ts-data-forge';
|
|
2
2
|
import { type RecordType, type Type } from '../type.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { toUnionKeyString } from '../utils/index.mjs';
|
|
4
4
|
import { record } from './record.mjs';
|
|
5
5
|
|
|
6
6
|
/** Creates a record type with keys picked. */
|
|
@@ -20,7 +20,7 @@ export const pick = <
|
|
|
20
20
|
record(Obj.pick(recordType.shape, keysToPick), {
|
|
21
21
|
typeName:
|
|
22
22
|
options?.typeName ??
|
|
23
|
-
`Pick<${recordType.typeName}, ${
|
|
23
|
+
`Pick<${recordType.typeName}, ${toUnionKeyString(keysToPick)}>`,
|
|
24
24
|
allowExcessProperties:
|
|
25
25
|
options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
26
26
|
});
|