ts-fortress 2.6.1 → 2.7.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 +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/other-types/index.d.mts +2 -0
- package/dist/other-types/index.d.mts.map +1 -0
- package/dist/other-types/index.mjs +2 -0
- package/dist/other-types/index.mjs.map +1 -0
- package/dist/other-types/unknown.d.mts +3 -0
- package/dist/other-types/unknown.d.mts.map +1 -0
- package/dist/{unknown.mjs → other-types/unknown.mjs} +2 -2
- package/dist/other-types/unknown.mjs.map +1 -0
- package/dist/primitives/string.mjs +1 -1
- package/dist/primitives/string.mjs.map +1 -1
- package/dist/utils/create-cast-fn.d.mts.map +1 -1
- package/dist/utils/create-cast-fn.mjs +1 -2
- package/dist/utils/create-cast-fn.mjs.map +1 -1
- package/dist/utils/validation-error.d.mts +2 -2
- package/dist/utils/validation-error.d.mts.map +1 -1
- package/dist/utils/validation-error.mjs +10 -3
- package/dist/utils/validation-error.mjs.map +1 -1
- package/package.json +6 -6
- package/src/array/array-of-length.test.mts +2 -2
- package/src/array/array.test.mts +2 -4
- package/src/array/non-empty-array.test.mts +2 -2
- package/src/array/tuple.test.mts +2 -2
- package/src/branded/number/finite-number.test.mts +1 -1
- package/src/branded/number/int.test.mts +1 -1
- package/src/branded/number/positive-int.test.mts +1 -1
- package/src/branded/number/uint.test.mts +1 -1
- package/src/branded/simple-branded-number.test.mts +2 -2
- package/src/branded/simple-branded-string.test.mts +2 -2
- package/src/compose/intersection.test.mts +2 -2
- package/src/compose/union.test.mts +4 -4
- package/src/enum/enum.test.mts +0 -2
- package/src/enum/uint-range.test.mts +0 -2
- package/src/index.mts +1 -1
- package/src/other-types/index.mts +1 -0
- package/src/{unknown.mts → other-types/unknown.mts} +2 -2
- package/src/primitives/bigint.test.mts +3 -3
- package/src/primitives/boolean.test.mts +4 -4
- package/src/primitives/null.test.mts +3 -3
- package/src/primitives/number.test.mts +5 -3
- package/src/primitives/string.mts +1 -1
- package/src/primitives/string.test.mts +8 -6
- package/src/primitives/symbol.test.mts +1 -1
- package/src/primitives/undefined.test.mts +3 -3
- package/src/record/key-value-record.test.mts +2 -2
- package/src/record/omit.test.mts +1 -1
- package/src/record/partial.test.mts +5 -5
- package/src/record/pick.test.mts +1 -1
- package/src/record/record.test.mts +4 -4
- package/src/utils/create-cast-fn.mts +2 -8
- package/src/utils/validation-error.mts +18 -3
- package/src/utils/validation-error.test.mts +3 -3
- package/dist/unknown.d.mts +0 -3
- package/dist/unknown.d.mts.map +0 -1
- package/dist/unknown.mjs.map +0 -1
package/dist/entry-point.mjs
CHANGED
|
@@ -29,6 +29,7 @@ export { union } from './compose/union.mjs';
|
|
|
29
29
|
export { enumType } from './enum/enum.mjs';
|
|
30
30
|
export { intRange } from './enum/int-range.mjs';
|
|
31
31
|
export { uintRange } from './enum/uint-range.mjs';
|
|
32
|
+
export { unknown } from './other-types/unknown.mjs';
|
|
32
33
|
export { int8 } from './predefined/int8.mjs';
|
|
33
34
|
export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
|
|
34
35
|
export { uint8 } from './predefined/uint8.mjs';
|
|
@@ -46,7 +47,6 @@ export { isOptionalProperty, optional } from './record/optional.mjs';
|
|
|
46
47
|
export { partial } from './record/partial.mjs';
|
|
47
48
|
export { pick } from './record/pick.mjs';
|
|
48
49
|
export { record, strictRecord } from './record/record.mjs';
|
|
49
|
-
export { unknown } from './unknown.mjs';
|
|
50
50
|
export { createAssertFn } from './utils/create-assert-fn.mjs';
|
|
51
51
|
export { createCastFn } from './utils/create-cast-fn.mjs';
|
|
52
52
|
export { createIsFn } from './utils/create-is-fn.mjs';
|
package/dist/index.d.mts
CHANGED
|
@@ -2,10 +2,10 @@ export * from './array/index.mjs';
|
|
|
2
2
|
export * from './branded/index.mjs';
|
|
3
3
|
export * from './compose/index.mjs';
|
|
4
4
|
export * from './enum/index.mjs';
|
|
5
|
+
export * from './other-types/index.mjs';
|
|
5
6
|
export * from './predefined/index.mjs';
|
|
6
7
|
export * from './primitives/index.mjs';
|
|
7
8
|
export * from './record/index.mjs';
|
|
8
9
|
export * from './type.mjs';
|
|
9
|
-
export * from './unknown.mjs';
|
|
10
10
|
export * from './utils/index.mjs';
|
|
11
11
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -28,6 +28,7 @@ export { union } from './compose/union.mjs';
|
|
|
28
28
|
export { enumType } from './enum/enum.mjs';
|
|
29
29
|
export { intRange } from './enum/int-range.mjs';
|
|
30
30
|
export { uintRange } from './enum/uint-range.mjs';
|
|
31
|
+
export { unknown } from './other-types/unknown.mjs';
|
|
31
32
|
export { int8 } from './predefined/int8.mjs';
|
|
32
33
|
export { JsonObject, JsonPrimitive, JsonValue } from './predefined/json.mjs';
|
|
33
34
|
export { uint8 } from './predefined/uint8.mjs';
|
|
@@ -45,7 +46,6 @@ export { isOptionalProperty, optional } from './record/optional.mjs';
|
|
|
45
46
|
export { partial } from './record/partial.mjs';
|
|
46
47
|
export { pick } from './record/pick.mjs';
|
|
47
48
|
export { record, strictRecord } from './record/record.mjs';
|
|
48
|
-
export { unknown } from './unknown.mjs';
|
|
49
49
|
export { createAssertFn } from './utils/create-assert-fn.mjs';
|
|
50
50
|
export { createCastFn } from './utils/create-cast-fn.mjs';
|
|
51
51
|
export { createIsFn } from './utils/create-is-fn.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/other-types/index.mts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unknown.d.mts","sourceRoot":"","sources":["../../src/other-types/unknown.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,OAAO,GAAI,eAAe,OAAO,KAAG,IAAI,CAAC,OAAO,CAQ3D,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Result } from 'ts-data-forge';
|
|
2
|
-
import { createAssertFn } from '
|
|
3
|
-
import { createCastFn } from '
|
|
2
|
+
import { createAssertFn } from '../utils/create-assert-fn.mjs';
|
|
3
|
+
import { createCastFn } from '../utils/create-cast-fn.mjs';
|
|
4
4
|
|
|
5
5
|
const unknown = (defaultValue) => ({
|
|
6
6
|
typeName: 'unknown',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unknown.mjs","sources":["../../src/other-types/unknown.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;MAIa,OAAO,GAAG,CAAC,YAAsB,MAAqB;AACjE,IAAA,QAAQ,EAAE,SAAS;IACnB,YAAY;IACZ,EAAE;AACF,IAAA,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC;IACd,QAAQ;AACR,IAAA,QAAQ,EAAE,cAAc,CAAC,QAAQ,CAAC;AAClC,IAAA,IAAI,EAAE,YAAY,CAAC,QAAQ,CAAC;AAC7B,CAAA;AAED,MAAM,QAAQ,GAA8B,MAAM,CAAC,EAAE;AAErD,MAAM,EAAE,GAAG,CAAC,EAAW,KAAoB,IAAI;;;;"}
|
|
@@ -3,7 +3,7 @@ import { createPrimitiveType } from '../utils/create-primitive-type.mjs';
|
|
|
3
3
|
|
|
4
4
|
const string = (defaultValue = '') => createPrimitiveType({ typeName: 'string', defaultValue, is: isString });
|
|
5
5
|
const stringLiteral = (literal) => createPrimitiveType({
|
|
6
|
-
typeName: `stringLiteral(${literal})`,
|
|
6
|
+
typeName: `stringLiteral("${literal}")`,
|
|
7
7
|
defaultValue: literal,
|
|
8
8
|
is: (value) => value === literal,
|
|
9
9
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.mjs","sources":["../../src/primitives/string.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,EAAE,KAC9C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAEjE,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,
|
|
1
|
+
{"version":3,"file":"string.mjs","sources":["../../src/primitives/string.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,MAAM,GAAG,CAAC,eAAuB,EAAE,KAC9C,mBAAmB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,EAAE,QAAQ,EAAE;AAEjE,MAAM,aAAa,GAAG,CAAmB,OAAU,KACxD,mBAAmB,CAAC;IAClB,QAAQ,EAAE,CAAA,eAAA,EAAkB,OAAO,CAAA,EAAA,CAAI;AACvC,IAAA,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,CAAC,KAAc,KAAiB,KAAK,KAAK,OAAO;AACtD,CAAA;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-cast-fn.d.mts","sourceRoot":"","sources":["../../src/utils/create-cast-fn.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"create-cast-fn.d.mts","sourceRoot":"","sources":["../../src/utils/create-cast-fn.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,YAAY,GACtB,CAAC,EAAG,UAAU,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,MACjC,GAAG,OAAO,KAAG,CAMb,CAAC"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Result } from 'ts-data-forge';
|
|
2
2
|
import { validationErrorsToMessages } from './validation-error.mjs';
|
|
3
3
|
|
|
4
|
-
const validationResultToString = (result) => `\n${validationErrorsToMessages(result).join(',\n')}`;
|
|
5
4
|
const createCastFn = (validate) => (a) => {
|
|
6
5
|
const res = validate(a);
|
|
7
6
|
if (Result.isErr(res)) {
|
|
8
|
-
throw new Error(
|
|
7
|
+
throw new Error(validationErrorsToMessages(res.value).join('\n'));
|
|
9
8
|
}
|
|
10
9
|
return res.value;
|
|
11
10
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-cast-fn.mjs","sources":["../../src/utils/create-cast-fn.mts"],"sourcesContent":[null],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"create-cast-fn.mjs","sources":["../../src/utils/create-cast-fn.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAIO,MAAM,YAAY,GACvB,CAAK,QAA6B,KAClC,CAAC,CAAU,KAAO;AAChB,IAAA,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC;AACvB,IAAA,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;AACrB,QAAA,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnE;IACA,OAAO,GAAG,CAAC,KAAK;AAClB;;;;"}
|
|
@@ -19,12 +19,12 @@ export type ValidationErrorWithMessage = MergeIntersection<StrictOmit<Validation
|
|
|
19
19
|
/**
|
|
20
20
|
* Converts a validation error to a human-readable string message
|
|
21
21
|
*/
|
|
22
|
-
export declare const validationErrorToMessage: (error: ValidationError) => string;
|
|
22
|
+
export declare const validationErrorToMessage: (error: ValidationError, maxLengthToPrintActualValue?: number) => string;
|
|
23
23
|
/**
|
|
24
24
|
* Converts an array of validation errors to an array of string messages
|
|
25
25
|
* (for backward compatibility)
|
|
26
26
|
*/
|
|
27
|
-
export declare const validationErrorsToMessages: (errors: readonly ValidationError[]) => readonly string[];
|
|
27
|
+
export declare const validationErrorsToMessages: (errors: readonly ValidationError[], maxLengthToPrintActualValue?: number) => readonly string[];
|
|
28
28
|
/**
|
|
29
29
|
* Prepends a path segment to all validation errors
|
|
30
30
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-error.d.mts","sourceRoot":"","sources":["../../src/utils/validation-error.mts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation-error.d.mts","sourceRoot":"","sources":["../../src/utils/validation-error.mts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC;IACrC,iFAAiF;IACjF,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACxB,8CAA8C;IAC9C,WAAW,EAAE,OAAO,CAAC;IACrB,sCAAsC;IACtC,YAAY,EAAE,MAAM,CAAC;IACrB,sDAAsD;IACtD,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,oDAAoD;IACpD,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,CAAC;AAEH,MAAM,MAAM,0BAA0B,GAAG,iBAAiB,CACxD,UAAU,CAAC,eAAe,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CACvE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,GACnC,OAAO,eAAe,EACtB,8BAA6B,MAAW,KACvC,MAiBF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,0BAA0B,GACrC,QAAQ,SAAS,eAAe,EAAE,EAClC,8BAA6B,MAAW,KACvC,SAAS,MAAM,EAC2D,CAAC;AAE9E;;GAEG;AACH,eAAO,MAAM,6BAA6B,GACxC,QAAQ,SAAS,eAAe,EAAE,EAClC,aAAa,MAAM,KAClB,SAAS,eAAe,EAItB,CAAC;AAEN;;GAEG;AACH,eAAO,MAAM,8BAA8B,GACzC,QAAQ,SAAS,eAAe,EAAE,EAClC,OAAO,MAAM,KACZ,SAAS,eAAe,EAC8B,CAAC;AAE1D;;GAEG;AACH,eAAO,MAAM,8BAA8B,GAAI,0CAI5C,QAAQ,CAAC;IACV,WAAW,EAAE,OAAO,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,KAAG,eAMH,CAAC"}
|
|
@@ -1,19 +1,26 @@
|
|
|
1
|
+
import { isString, pipe, unknownToString } from 'ts-data-forge';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Converts a validation error to a human-readable string message
|
|
3
5
|
*/
|
|
4
|
-
const validationErrorToMessage = (error) => {
|
|
6
|
+
const validationErrorToMessage = (error, maxLengthToPrintActualValue = 20) => {
|
|
5
7
|
const pathStr = error.path.length > 0 ? ` at ${error.path.join('.')}` : '';
|
|
6
8
|
const actualTypeStr = typeof error.actualValue;
|
|
7
9
|
if (error.message !== undefined) {
|
|
8
10
|
return `${error.message}${pathStr}`;
|
|
9
11
|
}
|
|
10
|
-
|
|
12
|
+
const actualValueStr = isString(error.actualValue)
|
|
13
|
+
? error.actualValue.length <= maxLengthToPrintActualValue
|
|
14
|
+
? ` "${error.actualValue}"`
|
|
15
|
+
: ''
|
|
16
|
+
: pipe(unknownToString(error.actualValue)).map((s) => s.length <= maxLengthToPrintActualValue ? ` \`${s}\`` : '').value;
|
|
17
|
+
return `Expected <${error.expectedType}>${pathStr}, got <${actualTypeStr}> type value${actualValueStr}.`;
|
|
11
18
|
};
|
|
12
19
|
/**
|
|
13
20
|
* Converts an array of validation errors to an array of string messages
|
|
14
21
|
* (for backward compatibility)
|
|
15
22
|
*/
|
|
16
|
-
const validationErrorsToMessages = (errors) => errors.map(validationErrorToMessage);
|
|
23
|
+
const validationErrorsToMessages = (errors, maxLengthToPrintActualValue = 20) => errors.map((e) => validationErrorToMessage(e, maxLengthToPrintActualValue));
|
|
17
24
|
/**
|
|
18
25
|
* Prepends a path segment to all validation errors
|
|
19
26
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-error.mjs","sources":["../../src/utils/validation-error.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation-error.mjs","sources":["../../src/utils/validation-error.mts"],"sourcesContent":[null],"names":[],"mappings":";;AAsBA;;AAEG;AACI,MAAM,wBAAwB,GAAG,CACtC,KAAsB,EACtB,2BAAA,GAAsC,EAAE,KAC9B;IACV,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,GAAG,CAAA,IAAA,EAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA,CAAE,GAAG,EAAE;AAC1E,IAAA,MAAM,aAAa,GAAG,OAAO,KAAK,CAAC,WAAW;AAE9C,IAAA,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,EAAE;AAC/B,QAAA,OAAO,GAAG,KAAK,CAAC,OAAO,CAAA,EAAG,OAAO,EAAE;IACrC;AAEA,IAAA,MAAM,cAAc,GAAW,QAAQ,CAAC,KAAK,CAAC,WAAW;AACvD,UAAE,KAAK,CAAC,WAAW,CAAC,MAAM,IAAI;AAC5B,cAAE,CAAA,EAAA,EAAK,KAAK,CAAC,WAAW,CAAA,CAAA;AACxB,cAAE;AACJ,UAAE,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAC7C,CAAC,CAAC,MAAM,IAAI,2BAA2B,GAAG,CAAA,GAAA,EAAM,CAAC,CAAA,EAAA,CAAI,GAAG,EAAE,CAC3D,CAAC,KAAK;IAEX,OAAO,CAAA,UAAA,EAAa,KAAK,CAAC,YAAY,CAAA,CAAA,EAAI,OAAO,CAAA,OAAA,EAAU,aAAa,CAAA,YAAA,EAAe,cAAc,CAAA,CAAA,CAAG;AAC1G;AAEA;;;AAGG;AACI,MAAM,0BAA0B,GAAG,CACxC,MAAkC,EAClC,2BAAA,GAAsC,EAAE,KAExC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,wBAAwB,CAAC,CAAC,EAAE,2BAA2B,CAAC;AAE5E;;AAEG;MACU,6BAA6B,GAAG,CAC3C,MAAkC,EAClC,WAAmB,KAEnB,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,MAAM;AACrB,IAAA,GAAG,KAAK;IACR,IAAI,EAAE,CAAC,WAAW,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC;AACnC,CAAA,CAAC;AAEJ;;AAEG;MACU,8BAA8B,GAAG,CAC5C,MAAkC,EAClC,KAAa,KAEb,6BAA6B,CAAC,MAAM,EAAE,KAAK,CAAC,QAAQ,EAAE;AAExD;;AAEG;AACI,MAAM,8BAA8B,GAAG,CAAC,EAC7C,WAAW,EACX,YAAY,EACZ,QAAQ,GAKR,MAAuB;AACvB,IAAA,IAAI,EAAE,EAAE;IACR,WAAW;IACX,YAAY;IACZ,QAAQ;AACR,IAAA,OAAO,EAAE,SAAS;AACnB,CAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-fortress",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript"
|
|
@@ -89,16 +89,16 @@
|
|
|
89
89
|
"prettier": "^3.6.2",
|
|
90
90
|
"prettier-plugin-organize-imports": "^4.2.0",
|
|
91
91
|
"prettier-plugin-packagejson": "^2.5.19",
|
|
92
|
-
"rollup": "^4.
|
|
92
|
+
"rollup": "^4.49.0",
|
|
93
93
|
"semantic-release": "^24.2.7",
|
|
94
|
-
"ts-repo-utils": "^
|
|
94
|
+
"ts-repo-utils": "^7.0.4",
|
|
95
95
|
"tslib": "^2.8.1",
|
|
96
96
|
"tsx": "^4.20.4",
|
|
97
|
-
"typedoc": "^0.28.
|
|
97
|
+
"typedoc": "^0.28.11",
|
|
98
98
|
"typedoc-plugin-markdown": "^4.8.1",
|
|
99
99
|
"typescript": "^5.9.2",
|
|
100
|
-
"typescript-eslint": "^8.
|
|
100
|
+
"typescript-eslint": "^8.41.0",
|
|
101
101
|
"vitest": "^3.2.4",
|
|
102
|
-
"zod": "^4.1.
|
|
102
|
+
"zod": "^4.1.5"
|
|
103
103
|
}
|
|
104
104
|
}
|
|
@@ -146,8 +146,8 @@ describe('arrayOfLength', () => {
|
|
|
146
146
|
]);
|
|
147
147
|
|
|
148
148
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
149
|
-
'Expected number at 1, got string',
|
|
150
|
-
'Expected number at 2, got string',
|
|
149
|
+
'Expected <number> at 1, got <string> type value "1".',
|
|
150
|
+
'Expected <number> at 2, got <string> type value "".',
|
|
151
151
|
]);
|
|
152
152
|
}
|
|
153
153
|
});
|
package/src/array/array.test.mts
CHANGED
|
@@ -101,11 +101,9 @@ describe('array', () => {
|
|
|
101
101
|
|
|
102
102
|
// Test that we can convert to legacy string format for backward compatibility
|
|
103
103
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
104
|
-
'Expected number at 0, got string',
|
|
105
|
-
'Expected number at 1, got string',
|
|
104
|
+
'Expected <number> at 0, got <string> type value "1".',
|
|
105
|
+
'Expected <number> at 1, got <string> type value "".',
|
|
106
106
|
]);
|
|
107
|
-
} else {
|
|
108
|
-
throw new Error('Expected validation to fail');
|
|
109
107
|
}
|
|
110
108
|
});
|
|
111
109
|
|
|
@@ -146,8 +146,8 @@ describe('nonEmptyArray', () => {
|
|
|
146
146
|
]);
|
|
147
147
|
|
|
148
148
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
149
|
-
'Expected number at 0, got string',
|
|
150
|
-
'Expected number at 1, got string',
|
|
149
|
+
'Expected <number> at 0, got <string> type value "1".',
|
|
150
|
+
'Expected <number> at 1, got <string> type value "".',
|
|
151
151
|
]);
|
|
152
152
|
}
|
|
153
153
|
});
|
package/src/array/tuple.test.mts
CHANGED
|
@@ -153,8 +153,8 @@ describe('tuple', () => {
|
|
|
153
153
|
]);
|
|
154
154
|
|
|
155
155
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
156
|
-
'Expected number at 0.x, got string',
|
|
157
|
-
'Expected number at 0.y, got string',
|
|
156
|
+
'Expected <number> at 0.x, got <string> type value "str".',
|
|
157
|
+
'Expected <number> at 0.y, got <string> type value "str".',
|
|
158
158
|
]);
|
|
159
159
|
}
|
|
160
160
|
});
|
|
@@ -69,7 +69,7 @@ describe('simpleBrandedNumber', () => {
|
|
|
69
69
|
},
|
|
70
70
|
]);
|
|
71
71
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
72
|
-
'Expected number
|
|
72
|
+
'Expected <number>, got <string> type value "not a number".',
|
|
73
73
|
]);
|
|
74
74
|
}
|
|
75
75
|
});
|
|
@@ -142,7 +142,7 @@ describe('simpleBrandedNumber', () => {
|
|
|
142
142
|
},
|
|
143
143
|
]);
|
|
144
144
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
145
|
-
'Expected number
|
|
145
|
+
'Expected <number>, got <string> type value "invalid".',
|
|
146
146
|
]);
|
|
147
147
|
}
|
|
148
148
|
});
|
|
@@ -70,7 +70,7 @@ describe('simpleBrandedString', () => {
|
|
|
70
70
|
},
|
|
71
71
|
]);
|
|
72
72
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
73
|
-
'Expected string
|
|
73
|
+
'Expected <string>, got <number> type value `42`.',
|
|
74
74
|
]);
|
|
75
75
|
}
|
|
76
76
|
});
|
|
@@ -143,7 +143,7 @@ describe('simpleBrandedString', () => {
|
|
|
143
143
|
},
|
|
144
144
|
]);
|
|
145
145
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
146
|
-
'Expected string
|
|
146
|
+
'Expected <string>, got <object> type value `null`.',
|
|
147
147
|
]);
|
|
148
148
|
}
|
|
149
149
|
});
|
|
@@ -251,9 +251,9 @@ describe('intersection', () => {
|
|
|
251
251
|
]);
|
|
252
252
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
253
253
|
'The type of value is expected to match all types of { number, number }',
|
|
254
|
-
'Expected number
|
|
254
|
+
'Expected <number>, got <string> type value "aaa".',
|
|
255
255
|
'The type of value is expected to match all types of { number, number }',
|
|
256
|
-
'Expected number
|
|
256
|
+
'Expected <number>, got <string> type value "aaa".',
|
|
257
257
|
]);
|
|
258
258
|
}
|
|
259
259
|
});
|
|
@@ -94,14 +94,14 @@ describe('union', () => {
|
|
|
94
94
|
path: [],
|
|
95
95
|
actualValue: 5,
|
|
96
96
|
expectedType:
|
|
97
|
-
'Union<{ x: number, y: number }, numberLiteral(3), stringLiteral(2)>',
|
|
97
|
+
'Union<{ x: number, y: number }, numberLiteral(3), stringLiteral("2")>',
|
|
98
98
|
typeName:
|
|
99
|
-
'Union<{ x: number, y: number }, numberLiteral(3), stringLiteral(2)>',
|
|
99
|
+
'Union<{ x: number, y: number }, numberLiteral(3), stringLiteral("2")>',
|
|
100
100
|
message:
|
|
101
|
-
'The type of value is expected to be one of the elements contained in { { x: number, y: number }, numberLiteral(3), stringLiteral(2) }',
|
|
101
|
+
'The type of value is expected to be one of the elements contained in { { x: number, y: number }, numberLiteral(3), stringLiteral("2") }',
|
|
102
102
|
});
|
|
103
103
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
104
|
-
'The type of value is expected to be one of the elements contained in { { x: number, y: number }, numberLiteral(3), stringLiteral(2) }',
|
|
104
|
+
'The type of value is expected to be one of the elements contained in { { x: number, y: number }, numberLiteral(3), stringLiteral("2") }',
|
|
105
105
|
]);
|
|
106
106
|
}
|
|
107
107
|
});
|
package/src/enum/enum.test.mts
CHANGED
|
@@ -72,8 +72,6 @@ describe('enumType', () => {
|
|
|
72
72
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
73
73
|
'The value is expected to be one of the elements contained in { 3, 2, a }',
|
|
74
74
|
]);
|
|
75
|
-
} else {
|
|
76
|
-
throw new Error('Expected validation to fail');
|
|
77
75
|
}
|
|
78
76
|
});
|
|
79
77
|
|
package/src/index.mts
CHANGED
|
@@ -2,9 +2,9 @@ export * from './array/index.mjs';
|
|
|
2
2
|
export * from './branded/index.mjs';
|
|
3
3
|
export * from './compose/index.mjs';
|
|
4
4
|
export * from './enum/index.mjs';
|
|
5
|
+
export * from './other-types/index.mjs';
|
|
5
6
|
export * from './predefined/index.mjs';
|
|
6
7
|
export * from './primitives/index.mjs';
|
|
7
8
|
export * from './record/index.mjs';
|
|
8
9
|
export * from './type.mjs';
|
|
9
|
-
export * from './unknown.mjs';
|
|
10
10
|
export * from './utils/index.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './unknown.mjs';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Result } from 'ts-data-forge';
|
|
2
|
-
import { type Type } from '
|
|
3
|
-
import { createAssertFn, createCastFn } from '
|
|
2
|
+
import { type Type } from '../type.mjs';
|
|
3
|
+
import { createAssertFn, createCastFn } from '../utils/index.mjs';
|
|
4
4
|
|
|
5
5
|
export const unknown = (defaultValue?: unknown): Type<unknown> => ({
|
|
6
6
|
typeName: 'unknown',
|
|
@@ -64,7 +64,7 @@ describe('bigint', () => {
|
|
|
64
64
|
]);
|
|
65
65
|
|
|
66
66
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
67
|
-
'Expected bigint
|
|
67
|
+
'Expected <bigint>, got <number> type value `123`.',
|
|
68
68
|
]);
|
|
69
69
|
}
|
|
70
70
|
});
|
|
@@ -199,7 +199,7 @@ describe('bigintLiteral', () => {
|
|
|
199
199
|
]);
|
|
200
200
|
|
|
201
201
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
202
|
-
'Expected bigintLiteral(42)
|
|
202
|
+
'Expected <bigintLiteral(42)>, got <bigint> type value `99`.',
|
|
203
203
|
]);
|
|
204
204
|
}
|
|
205
205
|
});
|
|
@@ -220,7 +220,7 @@ describe('bigintLiteral', () => {
|
|
|
220
220
|
]);
|
|
221
221
|
|
|
222
222
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
223
|
-
'Expected bigintLiteral(42)
|
|
223
|
+
'Expected <bigintLiteral(42)>, got <string> type value "not a bigint".',
|
|
224
224
|
]);
|
|
225
225
|
}
|
|
226
226
|
});
|
|
@@ -64,7 +64,7 @@ describe('boolean', () => {
|
|
|
64
64
|
]);
|
|
65
65
|
|
|
66
66
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
67
|
-
'Expected boolean
|
|
67
|
+
'Expected <boolean>, got <string> type value "not a boolean".',
|
|
68
68
|
]);
|
|
69
69
|
}
|
|
70
70
|
});
|
|
@@ -202,7 +202,7 @@ describe('booleanLiteral', () => {
|
|
|
202
202
|
]);
|
|
203
203
|
|
|
204
204
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
205
|
-
'Expected booleanLiteral(true)
|
|
205
|
+
'Expected <booleanLiteral(true)>, got <boolean> type value `false`.',
|
|
206
206
|
]);
|
|
207
207
|
}
|
|
208
208
|
});
|
|
@@ -223,7 +223,7 @@ describe('booleanLiteral', () => {
|
|
|
223
223
|
]);
|
|
224
224
|
|
|
225
225
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
226
|
-
'Expected booleanLiteral(true)
|
|
226
|
+
'Expected <booleanLiteral(true)>, got <string> type value "not a boolean".',
|
|
227
227
|
]);
|
|
228
228
|
}
|
|
229
229
|
});
|
|
@@ -318,7 +318,7 @@ describe('booleanLiteral', () => {
|
|
|
318
318
|
]);
|
|
319
319
|
|
|
320
320
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
321
|
-
'Expected booleanLiteral(false)
|
|
321
|
+
'Expected <booleanLiteral(false)>, got <boolean> type value `true`.',
|
|
322
322
|
]);
|
|
323
323
|
}
|
|
324
324
|
});
|
|
@@ -68,7 +68,7 @@ describe('nullType', () => {
|
|
|
68
68
|
const value: unknown = undefined;
|
|
69
69
|
expect(() => {
|
|
70
70
|
nullType.assertIs(value);
|
|
71
|
-
}).toThrow('Expected null
|
|
71
|
+
}).toThrow('Expected <null>, got <undefined> type value `undefined`.');
|
|
72
72
|
});
|
|
73
73
|
});
|
|
74
74
|
|
|
@@ -82,7 +82,7 @@ describe('nullType', () => {
|
|
|
82
82
|
test('non-null throws error', () => {
|
|
83
83
|
const value: unknown = undefined;
|
|
84
84
|
expect(() => nullType.cast(value)).toThrow(
|
|
85
|
-
'Expected null
|
|
85
|
+
'Expected <null>, got <undefined> type value `undefined`.',
|
|
86
86
|
);
|
|
87
87
|
});
|
|
88
88
|
|
|
@@ -91,7 +91,7 @@ describe('nullType', () => {
|
|
|
91
91
|
|
|
92
92
|
for (const value of values) {
|
|
93
93
|
expect(() => nullType.cast(value)).toThrow(
|
|
94
|
-
/Expected null
|
|
94
|
+
/Expected <null>, got <(number|string|boolean|undefined)> type value/u,
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
97
|
});
|
|
@@ -88,7 +88,7 @@ describe('number', () => {
|
|
|
88
88
|
const value: unknown = '42';
|
|
89
89
|
expect(() => {
|
|
90
90
|
assertIs(value);
|
|
91
|
-
}).toThrow(/Expected number
|
|
91
|
+
}).toThrow(/Expected <number>/u);
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
94
|
|
|
@@ -101,14 +101,16 @@ describe('number', () => {
|
|
|
101
101
|
|
|
102
102
|
test('invalid value throws error', () => {
|
|
103
103
|
const value: unknown = 'not a number';
|
|
104
|
-
expect(() => num.cast(value)).toThrow(
|
|
104
|
+
expect(() => num.cast(value)).toThrow(
|
|
105
|
+
'Expected <number>, got <string> type value "not a number".',
|
|
106
|
+
);
|
|
105
107
|
});
|
|
106
108
|
|
|
107
109
|
test('throws error with type mismatch', () => {
|
|
108
110
|
const numWithDefault = number(100);
|
|
109
111
|
const value: unknown = 'not a number';
|
|
110
112
|
expect(() => numWithDefault.cast(value)).toThrow(
|
|
111
|
-
'Expected number
|
|
113
|
+
'Expected <number>, got <string> type value "not a number".',
|
|
112
114
|
);
|
|
113
115
|
});
|
|
114
116
|
});
|
|
@@ -7,7 +7,7 @@ export const string = (defaultValue: string = ''): Type<string> =>
|
|
|
7
7
|
|
|
8
8
|
export const stringLiteral = <L extends string>(literal: L): Type<L> =>
|
|
9
9
|
createPrimitiveType({
|
|
10
|
-
typeName: `stringLiteral(${literal})`,
|
|
10
|
+
typeName: `stringLiteral("${literal}")`,
|
|
11
11
|
defaultValue: literal,
|
|
12
12
|
is: (value: unknown): value is L => value === literal,
|
|
13
13
|
});
|
|
@@ -91,7 +91,7 @@ describe('string', () => {
|
|
|
91
91
|
const value: unknown = 42;
|
|
92
92
|
expect(() => {
|
|
93
93
|
assertIs(value);
|
|
94
|
-
}).toThrow(/Expected string
|
|
94
|
+
}).toThrow(/Expected <string>/u);
|
|
95
95
|
});
|
|
96
96
|
});
|
|
97
97
|
|
|
@@ -104,14 +104,16 @@ describe('string', () => {
|
|
|
104
104
|
|
|
105
105
|
test('invalid value throws error', () => {
|
|
106
106
|
const value: unknown = 42;
|
|
107
|
-
expect(() => str.cast(value)).toThrow(
|
|
107
|
+
expect(() => str.cast(value)).toThrow(
|
|
108
|
+
'Expected <string>, got <number> type value `42`.',
|
|
109
|
+
);
|
|
108
110
|
});
|
|
109
111
|
|
|
110
112
|
test('throws error with type mismatch', () => {
|
|
111
113
|
const strWithDefault = string('default');
|
|
112
114
|
const value: unknown = 42;
|
|
113
115
|
expect(() => strWithDefault.cast(value)).toThrow(
|
|
114
|
-
'Expected string
|
|
116
|
+
'Expected <string>, got <number> type value `42`.',
|
|
115
117
|
);
|
|
116
118
|
});
|
|
117
119
|
});
|
|
@@ -304,8 +306,8 @@ describe('stringLiteral', () => {
|
|
|
304
306
|
{
|
|
305
307
|
path: [],
|
|
306
308
|
actualValue: 'world',
|
|
307
|
-
expectedType: 'stringLiteral(hello)',
|
|
308
|
-
typeName: 'stringLiteral(hello)',
|
|
309
|
+
expectedType: 'stringLiteral("hello")',
|
|
310
|
+
typeName: 'stringLiteral("hello")',
|
|
309
311
|
message: undefined,
|
|
310
312
|
},
|
|
311
313
|
]);
|
|
@@ -332,7 +334,7 @@ describe('stringLiteral', () => {
|
|
|
332
334
|
test('invalid value throws error', () => {
|
|
333
335
|
const value: unknown = 'world';
|
|
334
336
|
expect(() => hello.cast(value)).toThrow(
|
|
335
|
-
'Expected stringLiteral(hello)
|
|
337
|
+
'Expected <stringLiteral("hello")>, got <string> type value "world".',
|
|
336
338
|
);
|
|
337
339
|
});
|
|
338
340
|
});
|
|
@@ -77,7 +77,7 @@ describe('undefinedType', () => {
|
|
|
77
77
|
const value: unknown = null;
|
|
78
78
|
expect(() => {
|
|
79
79
|
undefinedType.assertIs(value);
|
|
80
|
-
}).toThrow(
|
|
80
|
+
}).toThrow('Expected <undefined>, got <object> type value `null`.');
|
|
81
81
|
});
|
|
82
82
|
});
|
|
83
83
|
|
|
@@ -92,7 +92,7 @@ describe('undefinedType', () => {
|
|
|
92
92
|
const value: unknown = null;
|
|
93
93
|
expect(() => {
|
|
94
94
|
undefinedType.cast(value);
|
|
95
|
-
}).toThrow('Expected undefined
|
|
95
|
+
}).toThrow('Expected <undefined>, got <object> type value `null`.');
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
test('any non-undefined value throws error', () => {
|
|
@@ -101,7 +101,7 @@ describe('undefinedType', () => {
|
|
|
101
101
|
for (const value of values) {
|
|
102
102
|
expect(() => {
|
|
103
103
|
undefinedType.cast(value);
|
|
104
|
-
}).toThrow(
|
|
104
|
+
}).toThrow(/^Expected <undefined>, got <.*> type value .+\.$/u);
|
|
105
105
|
}
|
|
106
106
|
});
|
|
107
107
|
});
|
|
@@ -130,9 +130,9 @@ describe('keyValueRecord', () => {
|
|
|
130
130
|
]);
|
|
131
131
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
132
132
|
'The value of the record is expected to be <number>',
|
|
133
|
-
'Expected number at month, got string',
|
|
133
|
+
'Expected <number> at month, got <string> type value "ab".',
|
|
134
134
|
'The value of the record is expected to be <number>',
|
|
135
|
-
'Expected number at date, got string',
|
|
135
|
+
'Expected <number> at date, got <string> type value "cd".',
|
|
136
136
|
]);
|
|
137
137
|
}
|
|
138
138
|
});
|
package/src/record/omit.test.mts
CHANGED
|
@@ -186,8 +186,8 @@ describe('partial', () => {
|
|
|
186
186
|
},
|
|
187
187
|
]);
|
|
188
188
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
189
|
-
'Expected number at month, got string',
|
|
190
|
-
'Expected number at date, got string',
|
|
189
|
+
'Expected <number> at month, got <string> type value "ab".',
|
|
190
|
+
'Expected <number> at date, got <string> type value "cd".',
|
|
191
191
|
]);
|
|
192
192
|
}
|
|
193
193
|
});
|
|
@@ -212,7 +212,7 @@ describe('partial', () => {
|
|
|
212
212
|
},
|
|
213
213
|
]);
|
|
214
214
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
215
|
-
'Expected number at month, got string',
|
|
215
|
+
'Expected <number> at month, got <string> type value "ab".',
|
|
216
216
|
]);
|
|
217
217
|
}
|
|
218
218
|
});
|
|
@@ -385,8 +385,8 @@ describe('partial', () => {
|
|
|
385
385
|
},
|
|
386
386
|
]);
|
|
387
387
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
388
|
-
'Expected number at month, got string',
|
|
389
|
-
'Expected number at date, got string',
|
|
388
|
+
'Expected <number> at month, got <string> type value "ab".',
|
|
389
|
+
'Expected <number> at date, got <string> type value "cd".',
|
|
390
390
|
]);
|
|
391
391
|
}
|
|
392
392
|
});
|
package/src/record/pick.test.mts
CHANGED
|
@@ -106,8 +106,8 @@ describe('record', () => {
|
|
|
106
106
|
},
|
|
107
107
|
]);
|
|
108
108
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
109
|
-
'Expected number at month, got string',
|
|
110
|
-
'Expected number at date, got string',
|
|
109
|
+
'Expected <number> at month, got <string> type value "ab".',
|
|
110
|
+
'Expected <number> at date, got <string> type value "cd".',
|
|
111
111
|
]);
|
|
112
112
|
}
|
|
113
113
|
});
|
|
@@ -319,8 +319,8 @@ describe('partial record', () => {
|
|
|
319
319
|
},
|
|
320
320
|
]);
|
|
321
321
|
expect(validationErrorsToMessages(result.value)).toStrictEqual([
|
|
322
|
-
'Expected number at month, got string',
|
|
323
|
-
'Expected number at date, got string',
|
|
322
|
+
'Expected <number> at month, got <string> type value "ab".',
|
|
323
|
+
'Expected <number> at date, got <string> type value "cd".',
|
|
324
324
|
]);
|
|
325
325
|
}
|
|
326
326
|
});
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import { Result } from 'ts-data-forge';
|
|
2
2
|
import { type Type } from '../type.mjs';
|
|
3
|
-
import {
|
|
4
|
-
validationErrorsToMessages,
|
|
5
|
-
type ValidationError,
|
|
6
|
-
} from './validation-error.mjs';
|
|
7
|
-
|
|
8
|
-
const validationResultToString = (result: readonly ValidationError[]): string =>
|
|
9
|
-
`\n${validationErrorsToMessages(result).join(',\n')}`;
|
|
3
|
+
import { validationErrorsToMessages } from './validation-error.mjs';
|
|
10
4
|
|
|
11
5
|
export const createCastFn =
|
|
12
6
|
<T,>(validate: Type<T>['validate']) =>
|
|
13
7
|
(a: unknown): T => {
|
|
14
8
|
const res = validate(a);
|
|
15
9
|
if (Result.isErr(res)) {
|
|
16
|
-
throw new Error(
|
|
10
|
+
throw new Error(validationErrorsToMessages(res.value).join('\n'));
|
|
17
11
|
}
|
|
18
12
|
return res.value;
|
|
19
13
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isString, pipe, unknownToString } from 'ts-data-forge';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Represents a validation error with structured information
|
|
3
5
|
*/
|
|
@@ -21,7 +23,10 @@ export type ValidationErrorWithMessage = MergeIntersection<
|
|
|
21
23
|
/**
|
|
22
24
|
* Converts a validation error to a human-readable string message
|
|
23
25
|
*/
|
|
24
|
-
export const validationErrorToMessage = (
|
|
26
|
+
export const validationErrorToMessage = (
|
|
27
|
+
error: ValidationError,
|
|
28
|
+
maxLengthToPrintActualValue: number = 20,
|
|
29
|
+
): string => {
|
|
25
30
|
const pathStr = error.path.length > 0 ? ` at ${error.path.join('.')}` : '';
|
|
26
31
|
const actualTypeStr = typeof error.actualValue;
|
|
27
32
|
|
|
@@ -29,7 +34,15 @@ export const validationErrorToMessage = (error: ValidationError): string => {
|
|
|
29
34
|
return `${error.message}${pathStr}`;
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
const actualValueStr: string = isString(error.actualValue)
|
|
38
|
+
? error.actualValue.length <= maxLengthToPrintActualValue
|
|
39
|
+
? ` "${error.actualValue}"`
|
|
40
|
+
: ''
|
|
41
|
+
: pipe(unknownToString(error.actualValue)).map((s) =>
|
|
42
|
+
s.length <= maxLengthToPrintActualValue ? ` \`${s}\`` : '',
|
|
43
|
+
).value;
|
|
44
|
+
|
|
45
|
+
return `Expected <${error.expectedType}>${pathStr}, got <${actualTypeStr}> type value${actualValueStr}.`;
|
|
33
46
|
};
|
|
34
47
|
|
|
35
48
|
/**
|
|
@@ -38,7 +51,9 @@ export const validationErrorToMessage = (error: ValidationError): string => {
|
|
|
38
51
|
*/
|
|
39
52
|
export const validationErrorsToMessages = (
|
|
40
53
|
errors: readonly ValidationError[],
|
|
41
|
-
|
|
54
|
+
maxLengthToPrintActualValue: number = 20,
|
|
55
|
+
): readonly string[] =>
|
|
56
|
+
errors.map((e) => validationErrorToMessage(e, maxLengthToPrintActualValue));
|
|
42
57
|
|
|
43
58
|
/**
|
|
44
59
|
* Prepends a path segment to all validation errors
|
|
@@ -144,9 +144,9 @@ describe('validation-error', () => {
|
|
|
144
144
|
];
|
|
145
145
|
|
|
146
146
|
expect(validationErrorsToMessages(errors)).toStrictEqual([
|
|
147
|
-
'Expected string at user.name, got number',
|
|
148
|
-
'Expected number at items.0, got string',
|
|
149
|
-
'Expected boolean
|
|
147
|
+
'Expected <string> at user.name, got <number> type value `123`.',
|
|
148
|
+
'Expected <number> at items.0, got <string> type value "invalid".',
|
|
149
|
+
'Expected <boolean>, got <object> type value `null`.',
|
|
150
150
|
]);
|
|
151
151
|
});
|
|
152
152
|
|
package/dist/unknown.d.mts
DELETED
package/dist/unknown.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unknown.d.mts","sourceRoot":"","sources":["../src/unknown.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,YAAY,CAAC;AAGvC,eAAO,MAAM,OAAO,GAAI,eAAe,OAAO,KAAG,IAAI,CAAC,OAAO,CAQ3D,CAAC"}
|
package/dist/unknown.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unknown.mjs","sources":["../src/unknown.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;MAIa,OAAO,GAAG,CAAC,YAAsB,MAAqB;AACjE,IAAA,QAAQ,EAAE,SAAS;IACnB,YAAY;IACZ,EAAE;AACF,IAAA,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC;IACd,QAAQ;AACR,IAAA,QAAQ,EAAE,cAAc,CAAC,QAAQ,CAAC;AAClC,IAAA,IAAI,EAAE,YAAY,CAAC,QAAQ,CAAC;AAC7B,CAAA;AAED,MAAM,QAAQ,GAA8B,MAAM,CAAC,EAAE;AAErD,MAAM,EAAE,GAAG,CAAC,EAAW,KAAoB,IAAI;;;;"}
|