ts-fortress 3.2.0 → 4.1.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 -0
- package/dist/entry-point.mjs.map +1 -1
- package/dist/index.mjs +3 -0
- 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/keyof.d.mts +4 -3
- package/dist/record/keyof.d.mts.map +1 -1
- package/dist/record/keyof.mjs +5 -1
- package/dist/record/keyof.mjs.map +1 -1
- package/dist/record/omit.d.mts +5 -4
- package/dist/record/omit.d.mts.map +1 -1
- package/dist/record/omit.mjs +8 -39
- package/dist/record/omit.mjs.map +1 -1
- package/dist/record/partial.d.mts +13 -4
- package/dist/record/partial.d.mts.map +1 -1
- package/dist/record/partial.mjs +11 -118
- package/dist/record/partial.mjs.map +1 -1
- package/dist/record/pick.d.mts +5 -4
- package/dist/record/pick.d.mts.map +1 -1
- package/dist/record/pick.mjs +8 -39
- package/dist/record/pick.mjs.map +1 -1
- package/dist/record/record.d.mts +4 -24
- package/dist/record/record.d.mts.map +1 -1
- package/dist/record/record.mjs +9 -7
- package/dist/record/record.mjs.map +1 -1
- package/dist/record/valueof.d.mts +8 -0
- package/dist/record/valueof.d.mts.map +1 -0
- package/dist/record/valueof.mjs +23 -0
- package/dist/record/valueof.mjs.map +1 -0
- package/dist/type.d.mts +25 -1
- package/dist/type.d.mts.map +1 -1
- package/dist/type.mjs +1 -1
- package/dist/utils/index.d.mts +1 -0
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +1 -0
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/to-union-string.d.mts +2 -0
- package/dist/utils/to-union-string.d.mts.map +1 -0
- package/dist/utils/to-union-string.mjs +4 -0
- package/dist/utils/to-union-string.mjs.map +1 -0
- package/package.json +1 -1
- package/src/record/index.mts +1 -0
- package/src/record/keyof.mts +34 -12
- package/src/record/omit.mts +37 -70
- package/src/record/omit.test.mts +8 -1
- package/src/record/partial.mts +165 -156
- package/src/record/partial.test.mts +44 -4
- package/src/record/pick.mts +37 -73
- package/src/record/pick.test.mts +8 -1
- package/src/record/record-strict-composition.test.mts +16 -16
- package/src/record/record.mts +17 -48
- package/src/record/record.test.mts +9 -1
- package/src/record/valueof.mts +57 -0
- package/src/record/valueof.test.mts +178 -0
- package/src/type.mts +54 -2
- package/src/utils/index.mts +1 -0
- package/src/utils/to-union-string.mts +2 -0
package/dist/entry-point.mjs
CHANGED
|
@@ -52,10 +52,12 @@ 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 { valueof } from './record/valueof.mjs';
|
|
55
56
|
export { createAssertFn } from './utils/create-assert-fn.mjs';
|
|
56
57
|
export { createCastFn } from './utils/create-cast-fn.mjs';
|
|
57
58
|
export { createIsFn } from './utils/create-is-fn.mjs';
|
|
58
59
|
export { createPrimitiveType } from './utils/create-primitive-type.mjs';
|
|
59
60
|
export { createType } from './utils/create-type.mjs';
|
|
61
|
+
export { toUnionString } from './utils/to-union-string.mjs';
|
|
60
62
|
export { createPrimitiveValidationError, prependIndexToValidationErrors, prependPathToValidationErrors, validationErrorToMessage, validationErrorsToMessages } from './utils/validation-error.mjs';
|
|
61
63
|
//# 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,10 +51,13 @@ 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 { valueof } from './record/valueof.mjs';
|
|
55
|
+
import 'ts-data-forge';
|
|
54
56
|
export { createAssertFn } from './utils/create-assert-fn.mjs';
|
|
55
57
|
export { createCastFn } from './utils/create-cast-fn.mjs';
|
|
56
58
|
export { createIsFn } from './utils/create-is-fn.mjs';
|
|
57
59
|
export { createPrimitiveType } from './utils/create-primitive-type.mjs';
|
|
58
60
|
export { createType } from './utils/create-type.mjs';
|
|
61
|
+
export { toUnionString } from './utils/to-union-string.mjs';
|
|
59
62
|
export { createPrimitiveValidationError, prependIndexToValidationErrors, prependPathToValidationErrors, validationErrorToMessage, validationErrorsToMessages } from './utils/validation-error.mjs';
|
|
60
63
|
//# 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"}
|
|
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"}
|
package/dist/record/index.mjs
CHANGED
|
@@ -5,4 +5,5 @@ 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 { valueof } from './valueof.mjs';
|
|
8
9
|
//# 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/keyof.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
export declare const keyof: <const R extends Type<
|
|
1
|
+
import { type RecordType, type Type } from '../type.mjs';
|
|
2
|
+
export declare const keyof: <const R extends ReadonlyRecord<string, Type<unknown>>>(recordType: RecordType<R>, options?: Partial<Readonly<{
|
|
3
3
|
typeName: string;
|
|
4
4
|
}>>) => KeyofType<R>;
|
|
5
|
-
type
|
|
5
|
+
type KeyofTypeSub<R extends ReadonlyRecord<string, Type<unknown>>> = Type<ToString<keyof R>>;
|
|
6
|
+
type KeyofType<R extends ReadonlyRecord<string, Type<unknown>>> = IsNever<keyof R> extends true ? Type<undefined> : KeyofTypeSub<R>;
|
|
6
7
|
export {};
|
|
7
8
|
//# sourceMappingURL=keyof.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyof.d.mts","sourceRoot":"","sources":["../../src/record/keyof.mts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"keyof.d.mts","sourceRoot":"","sources":["../../src/record/keyof.mts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAEzD,eAAO,MAAM,KAAK,GAAI,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EACzE,YAAY,UAAU,CAAC,CAAC,CAAC,EACzB,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,CACH,KACA,SAAS,CAAC,CAAC,CAQW,CAAC;AAQ1B,KAAK,YAAY,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,IAAI,CACvE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAClB,CAAC;AAEF,KAAK,SAAS,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,IAC5D,OAAO,CAAC,MAAM,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/record/keyof.mjs
CHANGED
|
@@ -4,11 +4,15 @@ import { undefinedType } from '../primitives/undefined.mjs';
|
|
|
4
4
|
|
|
5
5
|
const keyof = (recordType, options) =>
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
7
|
-
pipe(
|
|
7
|
+
pipe(getKeys(recordType)).map((keys) => Arr.isNonEmpty(keys)
|
|
8
8
|
? enumType(keys, {
|
|
9
9
|
typeName: options?.typeName ?? `keyof ${recordType.typeName}`,
|
|
10
10
|
})
|
|
11
11
|
: undefinedType).value;
|
|
12
|
+
const getKeys = (recordType) =>
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
14
|
+
Object.keys(recordType.shape);
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
12
16
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
13
17
|
|
|
14
18
|
export { keyof };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyof.mjs","sources":["../../src/record/keyof.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;MAKa,KAAK,GAAG,CACnB,
|
|
1
|
+
{"version":3,"file":"keyof.mjs","sources":["../../src/record/keyof.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;MAKa,KAAK,GAAG,CACnB,UAAyB,EACzB,OAIC;AAED;AACA,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,KACjC,GAAG,CAAC,UAAU,CAAC,IAAI;AACjB,MAAG,QAAQ,CAAC,IAAI,EAAE;QACd,QAAQ,EAAE,OAAO,EAAE,QAAQ,IAAI,CAAA,MAAA,EAAS,UAAU,CAAC,QAAQ,CAAA,CAAE;KAC9D;AACH,MAAG,aAAwC,CAC9C,CAAC;AAEJ,MAAM,OAAO,GAAG,CACd,UAAyB;AAEzB;AACA,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAA2C;AASzE;AAIA;;;;"}
|
package/dist/record/omit.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type RecordType, type Type } from '../type.mjs';
|
|
2
2
|
/** Creates a record type with keys omitted. */
|
|
3
|
-
export declare const omit: <const R extends Type<
|
|
3
|
+
export declare const omit: <const R extends ReadonlyRecord<string, Type<unknown>>, const KeysToOmit extends readonly (keyof R & string)[]>(recordType: RecordType<R>, keysToOmit: KeysToOmit, options?: Partial<Readonly<{
|
|
4
4
|
typeName: string;
|
|
5
|
+
/** @default true */
|
|
6
|
+
allowExcessProperties: boolean;
|
|
5
7
|
}>>) => OmittedType<R, KeysToOmit>;
|
|
6
|
-
type OmittedType<R extends Type<
|
|
7
|
-
export {};
|
|
8
|
+
export type OmittedType<R extends ReadonlyRecord<string, Type<unknown>>, KeysToOmit extends readonly (keyof R)[]> = RecordType<Omit<R, ArrayElement<KeysToOmit>>>;
|
|
8
9
|
//# sourceMappingURL=omit.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"omit.d.mts","sourceRoot":"","sources":["../../src/record/omit.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"omit.d.mts","sourceRoot":"","sources":["../../src/record/omit.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAIzD,+CAA+C;AAC/C,eAAO,MAAM,IAAI,GACf,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EACrD,KAAK,CAAC,UAAU,SAAS,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,EAAE,EAEtD,YAAY,UAAU,CAAC,CAAC,CAAC,EACzB,YAAY,UAAU,EACtB,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IAEjB,oBAAoB;IACpB,qBAAqB,EAAE,OAAO,CAAC;CAChC,CAAC,CACH,KACA,WAAW,CAAC,CAAC,EAAE,UAAU,CAOxB,CAAC;AAEL,MAAM,MAAM,WAAW,CACrB,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC/C,UAAU,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,IACrC,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/record/omit.mjs
CHANGED
|
@@ -1,44 +1,13 @@
|
|
|
1
|
-
import { Obj
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
|
-
import { createPrimitiveValidationError } from '../utils/validation-error.mjs';
|
|
1
|
+
import { Obj } from 'ts-data-forge';
|
|
2
|
+
import { toUnionString } from '../utils/to-union-string.mjs';
|
|
3
|
+
import { record } from './record.mjs';
|
|
6
4
|
|
|
7
5
|
/** Creates a record type with keys omitted. */
|
|
8
|
-
const omit = (recordType, keysToOmit, options) => {
|
|
9
|
-
|
|
10
|
-
`Omit<${recordType.typeName}, ${keysToOmit
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!isRecord(a)) {
|
|
14
|
-
return Result.err([
|
|
15
|
-
createPrimitiveValidationError({
|
|
16
|
-
actualValue: a,
|
|
17
|
-
expectedType: 'record',
|
|
18
|
-
typeName: typeNameFilled,
|
|
19
|
-
}),
|
|
20
|
-
]);
|
|
21
|
-
}
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
23
|
-
const objectFilled = {
|
|
24
|
-
...Obj.pick(recordType.fill(a), keysToOmit),
|
|
25
|
-
...a,
|
|
26
|
-
};
|
|
27
|
-
const baseValidationResult = recordType.validate(objectFilled);
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
29
|
-
return Result.map(baseValidationResult, () => a);
|
|
30
|
-
};
|
|
31
|
-
const fill = (a) => Obj.omit(recordType.fill(a), keysToOmit);
|
|
32
|
-
return {
|
|
33
|
-
typeName: typeNameFilled,
|
|
34
|
-
defaultValue,
|
|
35
|
-
fill,
|
|
36
|
-
validate,
|
|
37
|
-
is: createIsFn(validate),
|
|
38
|
-
assertIs: createAssertFn(validate),
|
|
39
|
-
cast: createCastFn(validate),
|
|
40
|
-
};
|
|
41
|
-
};
|
|
6
|
+
const omit = (recordType, keysToOmit, options) => record(Obj.omit(recordType.shape, keysToOmit), {
|
|
7
|
+
typeName: options?.typeName ??
|
|
8
|
+
`Omit<${recordType.typeName}, ${toUnionString(keysToOmit)}>`,
|
|
9
|
+
allowExcessProperties: options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
10
|
+
});
|
|
42
11
|
|
|
43
12
|
export { omit };
|
|
44
13
|
//# sourceMappingURL=omit.mjs.map
|
package/dist/record/omit.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"omit.mjs","sources":["../../src/record/omit.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
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,OAOC,KAED,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,aAAa,CAAC,UAAU,CAAC,CAAA,CAAA,CAAG;AAC9D,IAAA,qBAAqB,EACnB,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAAC,qBAAqB;AACrE,CAAA;;;;"}
|
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type RecordType, type Type } from '../type.mjs';
|
|
2
|
+
import { type OptionalPropertyType } from './optional.mjs';
|
|
2
3
|
/**
|
|
3
4
|
* Creates a Partial type. If keysToBeOptional is set, only those keys are
|
|
4
5
|
* optional, otherwise, all properties are optional.
|
|
5
6
|
*/
|
|
6
|
-
export declare const partial: <const R extends Type<
|
|
7
|
+
export declare const partial: <const R extends ReadonlyRecord<string, Type<unknown>>, const KeysToBeOptional extends NonEmptyArray<keyof R & string>>(recordType: RecordType<R>, options?: Partial<Readonly<{
|
|
7
8
|
keysToBeOptional: KeysToBeOptional;
|
|
8
9
|
typeName: string;
|
|
10
|
+
/** @default true */
|
|
11
|
+
allowExcessProperties: boolean;
|
|
9
12
|
}>>) => PartialType<R, KeysToBeOptional>;
|
|
10
|
-
type
|
|
11
|
-
type
|
|
13
|
+
type PartialTypeShape<R extends ReadonlyRecord<string, Type<unknown>>, KeysToBeOptional extends NonEmptyArray<keyof R & string> | undefined> = TypeEq<KeysToBeOptional, undefined> extends true ? FullyOptionalType<R> : PartiallyOptionalType<R, ArrayElement<KeysToBeOptional>>;
|
|
14
|
+
type FullyOptionalType<R extends ReadonlyRecord<string, Type<unknown>>> = {
|
|
15
|
+
readonly [P in keyof R]: OptionalPropertyType<R[P]>;
|
|
16
|
+
};
|
|
17
|
+
type PartiallyOptionalType<R extends ReadonlyRecord<string, Type<unknown>>, K extends keyof R> = {
|
|
18
|
+
readonly [P in keyof R]: P extends K ? OptionalPropertyType<R[P]> : R[P];
|
|
19
|
+
};
|
|
20
|
+
export type PartialType<R extends ReadonlyRecord<string, Type<unknown>>, KeysToBeOptional extends NonEmptyArray<keyof R & string> | undefined> = RecordType<PartialTypeShape<R, KeysToBeOptional>>;
|
|
12
21
|
export {};
|
|
13
22
|
//# sourceMappingURL=partial.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"partial.d.mts","sourceRoot":"","sources":["../../src/record/partial.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"partial.d.mts","sourceRoot":"","sources":["../../src/record/partial.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAAY,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAGrE;;;GAGG;AACH,eAAO,MAAM,OAAO,GAClB,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EACrD,KAAK,CAAC,gBAAgB,SAAS,aAAa,CA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AAa,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,SAAS,IAClE,UAAU,CAAC,gBAAgB,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC"}
|
package/dist/record/partial.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createPrimitiveValidationError } from '../utils/validation-error.mjs';
|
|
1
|
+
import 'ts-data-forge';
|
|
2
|
+
import { toUnionString } from '../utils/to-union-string.mjs';
|
|
3
|
+
import { optional } from './optional.mjs';
|
|
4
|
+
import { record } from './record.mjs';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Creates a Partial type. If keysToBeOptional is set, only those keys are
|
|
@@ -12,122 +11,16 @@ const partial = (recordType, options) => {
|
|
|
12
11
|
const typeNameFilled = options?.typeName ??
|
|
13
12
|
(options?.keysToBeOptional === undefined
|
|
14
13
|
? `Partial<${recordType.typeName}>`
|
|
15
|
-
: `PartiallyPartial<${recordType.typeName}, ${options.keysToBeOptional
|
|
16
|
-
const keysToBeOptional = options?.keysToBeOptional ?? Object.keys(recordType.
|
|
14
|
+
: `PartiallyPartial<${recordType.typeName}, ${toUnionString(options.keysToBeOptional)}>`);
|
|
15
|
+
const keysToBeOptional = new Set(options?.keysToBeOptional ?? Object.keys(recordType.shape));
|
|
16
|
+
const partialShape =
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!isRecord(a)) {
|
|
21
|
-
return Result.err([
|
|
22
|
-
createPrimitiveValidationError({
|
|
23
|
-
actualValue: a,
|
|
24
|
-
expectedType: 'record',
|
|
25
|
-
typeName: typeNameFilled,
|
|
26
|
-
}),
|
|
27
|
-
]);
|
|
28
|
-
}
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
30
|
-
const objectFilled = {
|
|
31
|
-
// fill optional properties to skip check by recordType.validate
|
|
32
|
-
...Obj.pick(recordType.defaultValue, keysToBeOptional),
|
|
33
|
-
// overwrite with `a`
|
|
34
|
-
...a,
|
|
35
|
-
};
|
|
36
|
-
const validationResult = recordType.validate(objectFilled);
|
|
37
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
38
|
-
return Result.map(validationResult, () => a);
|
|
39
|
-
};
|
|
40
|
-
const fill = (a) => {
|
|
41
|
-
if (!isRecord(a)) {
|
|
42
|
-
return defaultValue;
|
|
43
|
-
}
|
|
44
|
-
const inputKeys = ISet.create(Object.keys(a));
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
46
|
-
return Obj.omit(
|
|
47
|
-
// First, create a recursively filled object with all properties required.
|
|
48
|
-
recordType.fill(a),
|
|
49
|
-
// Then, Omit optional properties not present in `a`.
|
|
50
|
-
keysToBeOptional.filter((k) =>
|
|
51
|
-
// avoid removing properties from `a`
|
|
52
|
-
!inputKeys.has(k)));
|
|
53
|
-
};
|
|
54
|
-
return {
|
|
18
|
+
Object.fromEntries(Object.entries(recordType.shape).map(([k, v]) => [k, keysToBeOptional.has(k) ? optional(v) : v]));
|
|
19
|
+
return record(partialShape, {
|
|
55
20
|
typeName: typeNameFilled,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
validate,
|
|
59
|
-
is: createIsFn(validate),
|
|
60
|
-
assertIs: createAssertFn(validate),
|
|
61
|
-
cast: createCastFn(validate),
|
|
62
|
-
};
|
|
21
|
+
allowExcessProperties: options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
22
|
+
});
|
|
63
23
|
};
|
|
64
|
-
// expectType<TsFortressInternal.SetOptional<Type<0>>, OptionalType<0>>('=');
|
|
65
|
-
// expectType<
|
|
66
|
-
// TsFortressInternal.SetOptionalForAllKey<{
|
|
67
|
-
// a: Type<0>;
|
|
68
|
-
// }>,
|
|
69
|
-
// Readonly<{
|
|
70
|
-
// a: OptionalType<0>;
|
|
71
|
-
// }>
|
|
72
|
-
// >('=');
|
|
73
|
-
// expectType<
|
|
74
|
-
// TsFortressInternal.SetOptionalForAllKey<{
|
|
75
|
-
// a: Type<0>;
|
|
76
|
-
// b: OptionalType<0>;
|
|
77
|
-
// }>,
|
|
78
|
-
// Readonly<{
|
|
79
|
-
// a: OptionalType<0>;
|
|
80
|
-
// b: OptionalType<0>;
|
|
81
|
-
// }>
|
|
82
|
-
// >('=');
|
|
83
|
-
// expectType<
|
|
84
|
-
// TsFortressInternal.SetOptionalForAllKey<
|
|
85
|
-
// RecordType<{
|
|
86
|
-
// a: OptionalType<0>;
|
|
87
|
-
// b: OptionalType<0>;
|
|
88
|
-
// c: Type<0>;
|
|
89
|
-
// }>['source']
|
|
90
|
-
// >,
|
|
91
|
-
// Readonly<{
|
|
92
|
-
// a: OptionalType<0>;
|
|
93
|
-
// b: OptionalType<0>;
|
|
94
|
-
// c: OptionalType<0>;
|
|
95
|
-
// }>
|
|
96
|
-
// >('=');
|
|
97
|
-
// expectType<
|
|
98
|
-
// PartialType<
|
|
99
|
-
// RecordType<{
|
|
100
|
-
// a: OptionalType<0>;
|
|
101
|
-
// b: OptionalType<0>;
|
|
102
|
-
// c: Type<0>;
|
|
103
|
-
// }>,
|
|
104
|
-
// undefined
|
|
105
|
-
// >,
|
|
106
|
-
// RecordType<
|
|
107
|
-
// Readonly<{
|
|
108
|
-
// a: OptionalType<0>;
|
|
109
|
-
// b: OptionalType<0>;
|
|
110
|
-
// c: OptionalType<0>;
|
|
111
|
-
// }>
|
|
112
|
-
// >
|
|
113
|
-
// >('=');
|
|
114
|
-
// expectType<
|
|
115
|
-
// PartialType<
|
|
116
|
-
// Type<{
|
|
117
|
-
// a: OptionalType<0>;
|
|
118
|
-
// b: Type<0>;
|
|
119
|
-
// c: Type<0>;
|
|
120
|
-
// }>,
|
|
121
|
-
// ['b']
|
|
122
|
-
// >,
|
|
123
|
-
// RecordType<
|
|
124
|
-
// Readonly<{
|
|
125
|
-
// a: OptionalType<0>;
|
|
126
|
-
// b: OptionalType<0>;
|
|
127
|
-
// c: Type<0>;
|
|
128
|
-
// }>
|
|
129
|
-
// >
|
|
130
|
-
// >('=');
|
|
131
24
|
|
|
132
25
|
export { partial };
|
|
133
26
|
//# sourceMappingURL=partial.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"partial.mjs","sources":["../../src/record/partial.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"partial.mjs","sources":["../../src/record/partial.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAMA;;;AAGG;MACU,OAAO,GAAG,CAIrB,UAAyB,EACzB,OAQC,KACmC;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,aAAa,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA,CAAA,CAAG,CAAC;AAE7F,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.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type RecordType, type Type } from '../type.mjs';
|
|
2
2
|
/** Creates a record type with keys picked. */
|
|
3
|
-
export declare const pick: <const R extends Type<
|
|
3
|
+
export declare const pick: <const R extends ReadonlyRecord<string, Type<unknown>>, const KeysToPick extends readonly (keyof R & string)[]>(recordType: RecordType<R>, keysToPick: KeysToPick, options?: Partial<Readonly<{
|
|
4
4
|
typeName: string;
|
|
5
|
+
/** @default true */
|
|
6
|
+
allowExcessProperties: boolean;
|
|
5
7
|
}>>) => PickedType<R, KeysToPick>;
|
|
6
|
-
type PickedType<R extends Type<
|
|
7
|
-
export {};
|
|
8
|
+
export type PickedType<R extends ReadonlyRecord<string, Type<unknown>>, KeysToPick extends readonly (keyof R)[]> = RecordType<Pick<R, ArrayElement<KeysToPick>>>;
|
|
8
9
|
//# sourceMappingURL=pick.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pick.d.mts","sourceRoot":"","sources":["../../src/record/pick.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"pick.d.mts","sourceRoot":"","sources":["../../src/record/pick.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAIzD,8CAA8C;AAC9C,eAAO,MAAM,IAAI,GACf,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EACrD,KAAK,CAAC,UAAU,SAAS,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,EAAE,EAEtD,YAAY,UAAU,CAAC,CAAC,CAAC,EACzB,YAAY,UAAU,EACtB,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IAEjB,oBAAoB;IACpB,qBAAqB,EAAE,OAAO,CAAC;CAChC,CAAC,CACH,KACA,UAAU,CAAC,CAAC,EAAE,UAAU,CAOvB,CAAC;AAEL,MAAM,MAAM,UAAU,CACpB,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC/C,UAAU,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,IACrC,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/record/pick.mjs
CHANGED
|
@@ -1,44 +1,13 @@
|
|
|
1
|
-
import { Obj
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
|
-
import { createPrimitiveValidationError } from '../utils/validation-error.mjs';
|
|
1
|
+
import { Obj } from 'ts-data-forge';
|
|
2
|
+
import { toUnionString } from '../utils/to-union-string.mjs';
|
|
3
|
+
import { record } from './record.mjs';
|
|
6
4
|
|
|
7
5
|
/** Creates a record type with keys picked. */
|
|
8
|
-
const pick = (recordType, keysToPick, options) => {
|
|
9
|
-
|
|
10
|
-
`Pick<${recordType.typeName}, ${keysToPick
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!isRecord(a)) {
|
|
14
|
-
return Result.err([
|
|
15
|
-
createPrimitiveValidationError({
|
|
16
|
-
actualValue: a,
|
|
17
|
-
expectedType: 'record',
|
|
18
|
-
typeName: typeNameFilled,
|
|
19
|
-
}),
|
|
20
|
-
]);
|
|
21
|
-
}
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
23
|
-
const objectFilled = {
|
|
24
|
-
...Obj.omit(recordType.fill(a), keysToPick),
|
|
25
|
-
...a,
|
|
26
|
-
};
|
|
27
|
-
const baseValidationResult = recordType.validate(objectFilled);
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
29
|
-
return Result.map(baseValidationResult, () => a);
|
|
30
|
-
};
|
|
31
|
-
const fill = (a) => Obj.pick(recordType.fill(a), keysToPick);
|
|
32
|
-
return {
|
|
33
|
-
typeName: typeNameFilled,
|
|
34
|
-
defaultValue,
|
|
35
|
-
fill,
|
|
36
|
-
validate,
|
|
37
|
-
is: createIsFn(validate),
|
|
38
|
-
assertIs: createAssertFn(validate),
|
|
39
|
-
cast: createCastFn(validate),
|
|
40
|
-
};
|
|
41
|
-
};
|
|
6
|
+
const pick = (recordType, keysToPick, options) => record(Obj.pick(recordType.shape, keysToPick), {
|
|
7
|
+
typeName: options?.typeName ??
|
|
8
|
+
`Pick<${recordType.typeName}, ${toUnionString(keysToPick)}>`,
|
|
9
|
+
allowExcessProperties: options?.allowExcessProperties ?? recordType.allowExcessProperties,
|
|
10
|
+
});
|
|
42
11
|
|
|
43
12
|
export { pick };
|
|
44
13
|
//# sourceMappingURL=pick.mjs.map
|
package/dist/record/pick.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pick.mjs","sources":["../../src/record/pick.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
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,OAOC,KAED,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,aAAa,CAAC,UAAU,CAAC,CAAA,CAAA,CAAG;AAC9D,IAAA,qBAAqB,EACnB,OAAO,EAAE,qBAAqB,IAAI,UAAU,CAAC,qBAAqB;AACrE,CAAA;;;;"}
|
package/dist/record/record.d.mts
CHANGED
|
@@ -1,28 +1,9 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
export declare const record: <const R extends ReadonlyRecord<string, Type<unknown>>>(
|
|
1
|
+
import { type RecordType, type TsFortressInternal, type Type } from '../type.mjs';
|
|
2
|
+
export declare const record: <const R extends ReadonlyRecord<string, Type<unknown>>>(shape: R, options?: Partial<Readonly<{
|
|
3
3
|
typeName: string;
|
|
4
4
|
/** @default true */
|
|
5
5
|
allowExcessProperties: boolean;
|
|
6
|
-
}>>) =>
|
|
7
|
-
type RecordTypeValue<R extends ReadonlyRecord<string, Type<unknown>>> = TsFortressInternal.RecordTypeValueImpl<R>;
|
|
8
|
-
declare namespace TsFortressInternal {
|
|
9
|
-
export type RecordTypeValueImpl<R extends ReadonlyRecord<string, Type<unknown>>> = RecordTypeValueImplSub<R, OptionalTypeKeys<R>>;
|
|
10
|
-
type RecordTypeValueImplSub<A extends ReadonlyRecord<string, Type<unknown>>, OptionalKeys extends keyof A> = TypeEq<OptionalKeys, never> extends true ? Readonly<{
|
|
11
|
-
[key in Exclude<keyof A, OptionalKeys>]: TypeOf<A[key]>;
|
|
12
|
-
}> : TypeEq<keyof A, OptionalKeys> extends true ? Readonly<MergeIntersection<{
|
|
13
|
-
[key in OptionalKeys]?: TypeOf<A[key]>;
|
|
14
|
-
}>> : Readonly<MergeIntersection<{
|
|
15
|
-
[key in OptionalKeys]?: TypeOf<A[key]>;
|
|
16
|
-
} & {
|
|
17
|
-
[key in Exclude<keyof A, OptionalKeys>]: TypeOf<A[key]>;
|
|
18
|
-
}>>;
|
|
19
|
-
type OptionalTypeKeys<A extends ReadonlyRecord<string, Type<unknown>>> = {
|
|
20
|
-
[K in keyof A]: A[K] extends {
|
|
21
|
-
optional: true;
|
|
22
|
-
} ? K : never;
|
|
23
|
-
}[keyof A];
|
|
24
|
-
export {};
|
|
25
|
-
}
|
|
6
|
+
}>>) => RecordType<R>;
|
|
26
7
|
/**
|
|
27
8
|
* Creates a strict record type that does not allow excess properties.
|
|
28
9
|
* This is an alias for `record(source, { allowExcessProperties: false })`.
|
|
@@ -46,6 +27,5 @@ declare namespace TsFortressInternal {
|
|
|
46
27
|
*/
|
|
47
28
|
export declare const strictRecord: <const R extends ReadonlyRecord<string, Type<unknown>>>(source: R, options?: Partial<Readonly<{
|
|
48
29
|
typeName: string;
|
|
49
|
-
}>>) => Type<RecordTypeValue<R>>;
|
|
50
|
-
export {};
|
|
30
|
+
}>>) => Type<TsFortressInternal.RecordTypeValue<R>>;
|
|
51
31
|
//# sourceMappingURL=record.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"record.d.mts","sourceRoot":"","sources":["../../src/record/record.mts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"record.d.mts","sourceRoot":"","sources":["../../src/record/record.mts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,kBAAkB,EACvB,KAAK,IAAI,EACV,MAAM,aAAa,CAAC;AAWrB,eAAO,MAAM,MAAM,GAAI,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC1E,OAAO,CAAC,EACR,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;IAEjB,oBAAoB;IACpB,qBAAqB,EAAE,OAAO,CAAC;CAChC,CAAC,CACH,KACA,UAAU,CAAC,CAAC,CAyGd,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,YAAY,GACvB,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAErD,QAAQ,CAAC,EACT,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,CACH,KACA,IAAI,CAAC,kBAAkB,CAAC,eAAe,CAAC,CAAC,CAAC,CACiB,CAAC"}
|
package/dist/record/record.mjs
CHANGED
|
@@ -4,16 +4,16 @@ import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
|
4
4
|
import { createIsFn } from '../utils/create-is-fn.mjs';
|
|
5
5
|
import { createPrimitiveValidationError, prependPathToValidationErrors } from '../utils/validation-error.mjs';
|
|
6
6
|
|
|
7
|
-
const record = (
|
|
8
|
-
const sourceKeys = new Set(Object.keys(
|
|
7
|
+
const record = (shape, options) => {
|
|
8
|
+
const sourceKeys = new Set(Object.keys(shape));
|
|
9
9
|
const typeNameFilled = options?.typeName ??
|
|
10
|
-
`{ ${Object.entries(
|
|
10
|
+
`{ ${Object.entries(shape)
|
|
11
11
|
.map(([k, v]) => `${k}: ${v.typeName}`)
|
|
12
12
|
.join(', ')} }`;
|
|
13
13
|
const allowExcessProperties = options?.allowExcessProperties ?? true;
|
|
14
14
|
const defaultValue =
|
|
15
15
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
16
|
-
Object.fromEntries(Object.entries(
|
|
16
|
+
Object.fromEntries(Object.entries(shape).map(([key, value]) => tp(key, value.defaultValue)));
|
|
17
17
|
const validate = (a) => {
|
|
18
18
|
if (!isRecord(a)) {
|
|
19
19
|
return Result.err([
|
|
@@ -25,9 +25,9 @@ const record = (source, options) => {
|
|
|
25
25
|
]);
|
|
26
26
|
}
|
|
27
27
|
const defaultErrors = Arr.generate(function* () {
|
|
28
|
-
for (const [k, valueType] of Object.entries(
|
|
28
|
+
for (const [k, valueType] of Object.entries(shape)) {
|
|
29
29
|
if (!Object.hasOwn(a, k)) {
|
|
30
|
-
if (
|
|
30
|
+
if (shape[k]?.optional !== true) {
|
|
31
31
|
yield {
|
|
32
32
|
path: [k],
|
|
33
33
|
actualValue: a,
|
|
@@ -69,7 +69,7 @@ const record = (source, options) => {
|
|
|
69
69
|
};
|
|
70
70
|
const fill = (a) => isRecord(a)
|
|
71
71
|
? // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
72
|
-
Object.fromEntries(Object.entries(
|
|
72
|
+
Object.fromEntries(Object.entries(shape).map(([k, v]) => tp(k, Object.hasOwn(a, k) ? v.fill(a[k]) : v.defaultValue)))
|
|
73
73
|
: defaultValue;
|
|
74
74
|
return {
|
|
75
75
|
typeName: typeNameFilled,
|
|
@@ -79,6 +79,8 @@ const record = (source, options) => {
|
|
|
79
79
|
is: createIsFn(validate),
|
|
80
80
|
assertIs: createAssertFn(validate),
|
|
81
81
|
cast: createCastFn(validate),
|
|
82
|
+
shape,
|
|
83
|
+
allowExcessProperties,
|
|
82
84
|
};
|
|
83
85
|
};
|
|
84
86
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"record.mjs","sources":["../../src/record/record.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"record.mjs","sources":["../../src/record/record.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;MAgBa,MAAM,GAAG,CACpB,KAAQ,EACR,OAOC,KACgB;AAGjB,IAAA,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAE9C,IAAA,MAAM,cAAc,GAClB,OAAO,EAAE,QAAQ;AACjB,QAAA,CAAA,EAAA,EAAK,MAAM,CAAC,OAAO,CAAC,KAAK;AACtB,aAAA,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAA,EAAA,EAAK,CAAC,CAAC,QAAQ,EAAE;AACrC,aAAA,IAAI,CAAC,IAAI,CAAC,CAAA,EAAA,CAAI;AAEnB,IAAA,MAAM,qBAAqB,GAAG,OAAO,EAAE,qBAAqB,IAAI,IAAI;AAEpE,IAAA,MAAM,YAAY;;AAEhB,IAAA,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,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Ac;QACxB,YAAY;QACZ,IAAI;QACJ,QAAQ;AACR,QAAA,EAAE,EAAE,UAAU,CAAC,QAAQ,CAAC;AACxB,QAAA,QAAQ,EAAE,cAAc,CAAC,QAAQ,CAAC;AAClC,QAAA,IAAI,EAAE,YAAY,CAAC,QAAQ,CAAC;QAC5B,KAAK;QACL,qBAAqB;KACtB;AACH;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACI,MAAM,YAAY,GAAG,CAG1B,MAAS,EACT,OAIC,KAED,MAAM,CAAC,MAAM,EAAE,EAAE,GAAG,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type RecordType, type Type } from '../type.mjs';
|
|
2
|
+
export declare const valueof: <const R extends ReadonlyRecord<string, Type<unknown>>>(recordType: RecordType<R>, options?: Partial<Readonly<{
|
|
3
|
+
typeName: string;
|
|
4
|
+
}>>) => ValueOfType<R>;
|
|
5
|
+
type ValueofTypeSub<R extends ReadonlyRecord<string, Type<unknown>>> = Type<R[keyof R]['defaultValue']>;
|
|
6
|
+
type ValueOfType<R extends ReadonlyRecord<string, Type<unknown>>> = IsNever<keyof R> extends true ? Type<undefined> : ValueofTypeSub<R>;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=valueof.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"valueof.d.mts","sourceRoot":"","sources":["../../src/record/valueof.mts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAEzD,eAAO,MAAM,OAAO,GAAI,KAAK,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,EAC3E,YAAY,UAAU,CAAC,CAAC,CAAC,EACzB,UAAU,OAAO,CACf,QAAQ,CAAC;IACP,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,CACH,KACA,WAAW,CAAC,CAAC,CAiBf,CAAC;AAEF,KAAK,cAAc,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,IAAI,CACzE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,cAAc,CAAC,CAC3B,CAAC;AAEF,KAAK,WAAW,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,IAC9D,OAAO,CAAC,MAAM,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC"}
|