ts-fortress 2.2.1 → 2.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/README.md +135 -0
- package/dist/array/array-of-length.d.mts.map +1 -1
- package/dist/array/array-of-length.mjs +8 -6
- package/dist/array/array-of-length.mjs.map +1 -1
- package/dist/array/array.d.mts.map +1 -1
- package/dist/array/array.mjs +8 -6
- package/dist/array/array.mjs.map +1 -1
- package/dist/array/non-empty-array.d.mts.map +1 -1
- package/dist/array/non-empty-array.mjs +8 -6
- package/dist/array/non-empty-array.mjs.map +1 -1
- package/dist/array/tuple.d.mts.map +1 -1
- package/dist/array/tuple.mjs +8 -6
- package/dist/array/tuple.mjs.map +1 -1
- package/dist/compose/index.d.mts +1 -0
- package/dist/compose/index.d.mts.map +1 -1
- package/dist/compose/index.mjs +1 -0
- package/dist/compose/index.mjs.map +1 -1
- package/dist/compose/intersection.d.mts.map +1 -1
- package/dist/compose/intersection.mjs +10 -10
- package/dist/compose/intersection.mjs.map +1 -1
- package/dist/compose/merge-records.d.mts.map +1 -1
- package/dist/compose/merge-records.mjs +10 -10
- package/dist/compose/merge-records.mjs.map +1 -1
- package/dist/compose/recursion.d.mts +5 -0
- package/dist/compose/recursion.d.mts.map +1 -0
- package/dist/compose/recursion.mjs +38 -0
- package/dist/compose/recursion.mjs.map +1 -0
- package/dist/compose/union.d.mts.map +1 -1
- package/dist/compose/union.mjs.map +1 -1
- package/dist/enum/enum.d.mts.map +1 -1
- package/dist/enum/enum.mjs.map +1 -1
- package/dist/enum/uint-range.d.mts.map +1 -1
- package/dist/enum/uint-range.mjs.map +1 -1
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/record/key-value-record.d.mts.map +1 -1
- package/dist/record/key-value-record.mjs +21 -24
- package/dist/record/key-value-record.mjs.map +1 -1
- package/dist/record/omit.d.mts.map +1 -1
- package/dist/record/omit.mjs +3 -2
- package/dist/record/omit.mjs.map +1 -1
- package/dist/record/partial.d.mts.map +1 -1
- package/dist/record/partial.mjs +2 -2
- package/dist/record/partial.mjs.map +1 -1
- package/dist/record/pick.d.mts.map +1 -1
- package/dist/record/pick.mjs +2 -2
- package/dist/record/pick.mjs.map +1 -1
- package/dist/record/record.d.mts.map +1 -1
- package/dist/record/record.mjs +23 -21
- package/dist/record/record.mjs.map +1 -1
- package/dist/type.d.mts +1 -1
- package/dist/type.d.mts.map +1 -1
- package/dist/utils/create-assert-fn.mjs +1 -1
- package/dist/utils/create-cast-fn.mjs +1 -1
- package/dist/utils/create-primitive-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/validation-error.d.mts.map +1 -0
- package/dist/utils/validation-error.mjs.map +1 -0
- package/package.json +4 -2
- package/src/array/array-of-length.mts +12 -7
- package/src/array/array-of-length.test.mts +10 -1
- package/src/array/array.mts +12 -7
- package/src/array/array.test.mts +10 -1
- package/src/array/non-empty-array.mts +11 -7
- package/src/array/non-empty-array.test.mts +10 -1
- package/src/array/tuple.mts +11 -9
- package/src/array/tuple.test.mts +14 -1
- package/src/branded/brand.mts +2 -2
- package/src/branded/number/finite-number.test.mts +10 -1
- package/src/branded/number/int.test.mts +10 -1
- package/src/branded/number/int16.test.mts +10 -1
- package/src/branded/number/int32.test.mts +10 -1
- package/src/branded/number/non-negative-finite-number.test.mts +10 -1
- package/src/branded/number/non-zero-finite-number.test.mts +10 -1
- package/src/branded/number/non-zero-int.test.mts +10 -1
- package/src/branded/number/non-zero-safe-int.test.mts +10 -1
- package/src/branded/number/positive-finite-number.test.mts +10 -1
- package/src/branded/number/positive-int.test.mts +10 -1
- package/src/branded/number/positive-safe-int.test.mts +10 -1
- package/src/branded/number/safe-int.test.mts +10 -1
- package/src/branded/number/safe-uint.test.mts +10 -1
- package/src/branded/number/uint.test.mts +10 -1
- package/src/branded/number/uint16.test.mts +10 -1
- package/src/branded/number/uint32.test.mts +10 -1
- package/src/branded/simple-branded-number.test.mts +19 -1
- package/src/branded/simple-branded-string.test.mts +19 -1
- package/src/compose/index.mts +1 -0
- package/src/compose/intersection.mts +23 -19
- package/src/compose/intersection.test.mts +28 -1
- package/src/compose/merge-records.mts +22 -19
- package/src/compose/merge-records.test.mts +10 -1
- package/src/compose/recursion.mts +49 -0
- package/src/compose/recursion.test.mts +234 -0
- package/src/compose/union.mts +6 -2
- package/src/compose/union.test.mts +10 -1
- package/src/enum/enum.mts +6 -2
- package/src/enum/enum.test.mts +10 -1
- package/src/enum/uint-range.mts +6 -2
- package/src/enum/uint-range.test.mts +10 -1
- package/src/index.mts +0 -1
- package/src/primitives/bigint.test.mts +19 -1
- package/src/primitives/boolean.test.mts +28 -1
- package/src/primitives/null.test.mts +9 -0
- package/src/primitives/number.test.mts +18 -0
- package/src/primitives/string.test.mts +18 -0
- package/src/primitives/symbol.test.mts +10 -1
- package/src/primitives/undefined.test.mts +9 -0
- package/src/record/key-value-record.mts +34 -36
- package/src/record/key-value-record.test.mts +14 -1
- package/src/record/keyof.test.mts +10 -1
- package/src/record/omit.mts +8 -5
- package/src/record/omit.test.mts +46 -1
- package/src/record/partial.mts +7 -5
- package/src/record/partial.test.mts +71 -1
- package/src/record/pick.mts +7 -5
- package/src/record/pick.test.mts +46 -1
- package/src/record/record-allow-excess-properties.test.mts +28 -1
- package/src/record/record-strict-composition-simple.test.mts +38 -0
- package/src/record/record-strict-composition.test.mts +71 -0
- package/src/record/record.mts +30 -29
- package/src/record/record.test.mts +26 -1
- package/src/record/strict-record.test.mts +10 -1
- package/src/type.mts +1 -1
- package/src/utils/create-assert-fn.mts +1 -1
- package/src/utils/create-cast-fn.mts +1 -1
- package/src/utils/create-primitive-type.mts +1 -1
- package/src/utils/index.mts +1 -0
- package/dist/validation-error.d.mts.map +0 -1
- package/dist/validation-error.mjs.map +0 -1
- package/src/combination.test.mts +0 -544
- /package/dist/{validation-error.d.mts → utils/validation-error.d.mts} +0 -0
- /package/dist/{validation-error.mjs → utils/validation-error.mjs} +0 -0
- /package/src/{validation-error.mts → utils/validation-error.mts} +0 -0
- /package/src/{validation-error.test.mts → utils/validation-error.test.mts} +0 -0
package/src/array/tuple.test.mts
CHANGED
|
@@ -5,7 +5,7 @@ import { type TypeOf } from '../type.mjs';
|
|
|
5
5
|
import {
|
|
6
6
|
type ValidationError,
|
|
7
7
|
validationErrorsToMessages,
|
|
8
|
-
} from '../
|
|
8
|
+
} from '../utils/index.mjs';
|
|
9
9
|
import { tuple } from './tuple.mjs';
|
|
10
10
|
|
|
11
11
|
describe('tuple', () => {
|
|
@@ -158,6 +158,19 @@ describe('tuple', () => {
|
|
|
158
158
|
]);
|
|
159
159
|
}
|
|
160
160
|
});
|
|
161
|
+
|
|
162
|
+
test('validate returns input as-is for OK cases', () => {
|
|
163
|
+
const input = [{ x: 5, y: 10 }, 3, '2'];
|
|
164
|
+
const result = targetType.validate(input);
|
|
165
|
+
expect(Result.isOk(result)).toBe(true);
|
|
166
|
+
if (Result.isOk(result)) {
|
|
167
|
+
// Note: tuple validation may create new arrays for type safety
|
|
168
|
+
// so we check for structural equality rather than reference equality
|
|
169
|
+
expect(result.value).toStrictEqual(input);
|
|
170
|
+
// For tuples, the inner objects should maintain reference equality
|
|
171
|
+
expect(result.value[0]).toBe(input[0]); // ✅ same reference for nested objects
|
|
172
|
+
}
|
|
173
|
+
});
|
|
161
174
|
});
|
|
162
175
|
|
|
163
176
|
describe('fill', () => {
|
package/src/branded/brand.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { pipe, Result } from 'ts-data-forge';
|
|
2
2
|
import { type Type } from '../type.mjs';
|
|
3
|
-
import { createType } from '../utils/index.mjs';
|
|
4
3
|
import {
|
|
4
|
+
createType,
|
|
5
5
|
type ValidationError,
|
|
6
6
|
type ValidationErrorWithMessage,
|
|
7
|
-
} from '../
|
|
7
|
+
} from '../utils/index.mjs';
|
|
8
8
|
|
|
9
9
|
type ArrayToUnion<A extends readonly unknown[]> = A extends readonly []
|
|
10
10
|
? never
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asFiniteNumber, expectType, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { finiteNumber } from './finite-number.mjs';
|
|
5
5
|
|
|
6
6
|
describe('finiteNumber', () => {
|
|
@@ -72,6 +72,15 @@ describe('finiteNumber', () => {
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
+
test('validate returns input as-is for OK cases', () => {
|
|
76
|
+
const input = 123.456;
|
|
77
|
+
const result = targetType.validate(input);
|
|
78
|
+
expect(Result.isOk(result)).toBe(true);
|
|
79
|
+
if (Result.isOk(result)) {
|
|
80
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
75
84
|
test('falsy case - infinity', () => {
|
|
76
85
|
const result = targetType.validate(Number.POSITIVE_INFINITY);
|
|
77
86
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asInt, expectType, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { int } from './int.mjs';
|
|
5
5
|
|
|
6
6
|
describe('int', () => {
|
|
@@ -102,6 +102,15 @@ describe('int', () => {
|
|
|
102
102
|
]);
|
|
103
103
|
}
|
|
104
104
|
});
|
|
105
|
+
|
|
106
|
+
test('validate returns input as-is for OK cases', () => {
|
|
107
|
+
const input = 123;
|
|
108
|
+
const result = targetType.validate(input);
|
|
109
|
+
expect(Result.isOk(result)).toBe(true);
|
|
110
|
+
if (Result.isOk(result)) {
|
|
111
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
112
|
+
}
|
|
113
|
+
});
|
|
105
114
|
});
|
|
106
115
|
|
|
107
116
|
describe('assertIs', () => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asInt16, expectType, isInt16, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { int16 } from './int16.mjs';
|
|
5
5
|
|
|
6
6
|
describe('int16', () => {
|
|
@@ -61,6 +61,15 @@ describe('int16', () => {
|
|
|
61
61
|
}
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
+
test('validate returns input as-is for OK cases', () => {
|
|
65
|
+
const input = 15000;
|
|
66
|
+
const result = targetType.validate(input);
|
|
67
|
+
expect(Result.isOk(result)).toBe(true);
|
|
68
|
+
if (Result.isOk(result)) {
|
|
69
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
64
73
|
test('falsy case - out of range', () => {
|
|
65
74
|
const result = targetType.validate(50000);
|
|
66
75
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asInt32, expectType, isInt32, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { int32 } from './int32.mjs';
|
|
5
5
|
|
|
6
6
|
describe('int32', () => {
|
|
@@ -55,6 +55,15 @@ describe('int32', () => {
|
|
|
55
55
|
}
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
+
test('validate returns input as-is for OK cases', () => {
|
|
59
|
+
const input = 1000000;
|
|
60
|
+
const result = targetType.validate(input);
|
|
61
|
+
expect(Result.isOk(result)).toBe(true);
|
|
62
|
+
if (Result.isOk(result)) {
|
|
63
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
58
67
|
test('falsy case - out of range', () => {
|
|
59
68
|
const result = targetType.validate(3000000000);
|
|
60
69
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
Result,
|
|
6
6
|
} from 'ts-data-forge';
|
|
7
7
|
import { type TypeOf } from '../../type.mjs';
|
|
8
|
-
import { validationErrorsToMessages } from '../../
|
|
8
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
9
9
|
import { nonNegativeFiniteNumber } from './non-negative-finite-number.mjs';
|
|
10
10
|
|
|
11
11
|
describe('nonNegativeFiniteNumber', () => {
|
|
@@ -72,6 +72,15 @@ describe('nonNegativeFiniteNumber', () => {
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
+
test('validate returns input as-is for OK cases', () => {
|
|
76
|
+
const input = 123.456;
|
|
77
|
+
const result = targetType.validate(input);
|
|
78
|
+
expect(Result.isOk(result)).toBe(true);
|
|
79
|
+
if (Result.isOk(result)) {
|
|
80
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
75
84
|
test('falsy case - negative', () => {
|
|
76
85
|
const result = targetType.validate(-5.5);
|
|
77
86
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, isNonZeroFiniteNumber, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { nonZeroFiniteNumber } from './non-zero-finite-number.mjs';
|
|
5
5
|
|
|
6
6
|
describe('nonZeroFiniteNumber', () => {
|
|
@@ -68,6 +68,15 @@ describe('nonZeroFiniteNumber', () => {
|
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
+
test('validate returns input as-is for OK cases', () => {
|
|
72
|
+
const input = 123.456;
|
|
73
|
+
const result = targetType.validate(input);
|
|
74
|
+
expect(Result.isOk(result)).toBe(true);
|
|
75
|
+
if (Result.isOk(result)) {
|
|
76
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
71
80
|
test('falsy case - zero', () => {
|
|
72
81
|
const result = targetType.validate(0);
|
|
73
82
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asNonZeroInt, expectType, isNonZeroInt, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { nonZeroInt } from './non-zero-int.mjs';
|
|
5
5
|
|
|
6
6
|
describe('nonZeroInt', () => {
|
|
@@ -61,6 +61,15 @@ describe('nonZeroInt', () => {
|
|
|
61
61
|
}
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
+
test('validate returns input as-is for OK cases', () => {
|
|
65
|
+
const input = 123;
|
|
66
|
+
const result = targetType.validate(input);
|
|
67
|
+
expect(Result.isOk(result)).toBe(true);
|
|
68
|
+
if (Result.isOk(result)) {
|
|
69
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
64
73
|
test('falsy case - zero', () => {
|
|
65
74
|
const result = targetType.validate(0);
|
|
66
75
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, isNonZeroSafeInt, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { nonZeroSafeInt } from './non-zero-safe-int.mjs';
|
|
5
5
|
|
|
6
6
|
describe('nonZeroSafeInt', () => {
|
|
@@ -68,6 +68,15 @@ describe('nonZeroSafeInt', () => {
|
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
+
test('validate returns input as-is for OK cases', () => {
|
|
72
|
+
const input = 123;
|
|
73
|
+
const result = targetType.validate(input);
|
|
74
|
+
expect(Result.isOk(result)).toBe(true);
|
|
75
|
+
if (Result.isOk(result)) {
|
|
76
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
71
80
|
test('falsy case - zero', () => {
|
|
72
81
|
const result = targetType.validate(0);
|
|
73
82
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, isPositiveFiniteNumber, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { positiveFiniteNumber } from './positive-finite-number.mjs';
|
|
5
5
|
|
|
6
6
|
describe('positiveFiniteNumber', () => {
|
|
@@ -68,6 +68,15 @@ describe('positiveFiniteNumber', () => {
|
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
+
test('validate returns input as-is for OK cases', () => {
|
|
72
|
+
const input = 123.456;
|
|
73
|
+
const result = targetType.validate(input);
|
|
74
|
+
expect(Result.isOk(result)).toBe(true);
|
|
75
|
+
if (Result.isOk(result)) {
|
|
76
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
71
80
|
test('falsy case - zero', () => {
|
|
72
81
|
const result = targetType.validate(0);
|
|
73
82
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
Result,
|
|
6
6
|
} from 'ts-data-forge';
|
|
7
7
|
import { type TypeOf } from '../../type.mjs';
|
|
8
|
-
import { validationErrorsToMessages } from '../../
|
|
8
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
9
9
|
import { positiveInt } from './positive-int.mjs';
|
|
10
10
|
|
|
11
11
|
describe('positiveInt', () => {
|
|
@@ -78,6 +78,15 @@ describe('positiveInt', () => {
|
|
|
78
78
|
}
|
|
79
79
|
});
|
|
80
80
|
|
|
81
|
+
test('validate returns input as-is for OK cases', () => {
|
|
82
|
+
const input = 123;
|
|
83
|
+
const result = targetType.validate(input);
|
|
84
|
+
expect(Result.isOk(result)).toBe(true);
|
|
85
|
+
if (Result.isOk(result)) {
|
|
86
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
81
90
|
test('falsy case - negative', () => {
|
|
82
91
|
const result = targetType.validate(-5);
|
|
83
92
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
Result,
|
|
6
6
|
} from 'ts-data-forge';
|
|
7
7
|
import { type TypeOf } from '../../type.mjs';
|
|
8
|
-
import { validationErrorsToMessages } from '../../
|
|
8
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
9
9
|
import { positiveSafeInt } from './positive-safe-int.mjs';
|
|
10
10
|
|
|
11
11
|
describe('positiveSafeInt', () => {
|
|
@@ -72,6 +72,15 @@ describe('positiveSafeInt', () => {
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
+
test('validate returns input as-is for OK cases', () => {
|
|
76
|
+
const input = 123456;
|
|
77
|
+
const result = targetType.validate(input);
|
|
78
|
+
expect(Result.isOk(result)).toBe(true);
|
|
79
|
+
if (Result.isOk(result)) {
|
|
80
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
75
84
|
test('falsy case - zero', () => {
|
|
76
85
|
const result = targetType.validate(0);
|
|
77
86
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asSafeInt, expectType, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { safeInt } from './safe-int.mjs';
|
|
5
5
|
|
|
6
6
|
describe('safeInt', () => {
|
|
@@ -60,6 +60,15 @@ describe('safeInt', () => {
|
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
62
|
|
|
63
|
+
test('validate returns input as-is for OK cases', () => {
|
|
64
|
+
const input = 123;
|
|
65
|
+
const result = targetType.validate(input);
|
|
66
|
+
expect(Result.isOk(result)).toBe(true);
|
|
67
|
+
if (Result.isOk(result)) {
|
|
68
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
63
72
|
test('falsy case - unsafe integer', () => {
|
|
64
73
|
const result = targetType.validate(Number.MAX_SAFE_INTEGER + 1);
|
|
65
74
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asSafeUint, expectType, isSafeUint, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { safeUint } from './safe-uint.mjs';
|
|
5
5
|
|
|
6
6
|
describe('safeUint', () => {
|
|
@@ -67,6 +67,15 @@ describe('safeUint', () => {
|
|
|
67
67
|
}
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
+
test('validate returns input as-is for OK cases', () => {
|
|
71
|
+
const input = 123456;
|
|
72
|
+
const result = targetType.validate(input);
|
|
73
|
+
expect(Result.isOk(result)).toBe(true);
|
|
74
|
+
if (Result.isOk(result)) {
|
|
75
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
70
79
|
test('falsy case - negative', () => {
|
|
71
80
|
const result = targetType.validate(-5);
|
|
72
81
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asUint, expectType, isUint, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { uint } from './uint.mjs';
|
|
5
5
|
|
|
6
6
|
describe('uint', () => {
|
|
@@ -73,6 +73,15 @@ describe('uint', () => {
|
|
|
73
73
|
}
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
+
test('validate returns input as-is for OK cases', () => {
|
|
77
|
+
const input = 123;
|
|
78
|
+
const result = targetType.validate(input);
|
|
79
|
+
expect(Result.isOk(result)).toBe(true);
|
|
80
|
+
if (Result.isOk(result)) {
|
|
81
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
76
85
|
test('falsy case - negative', () => {
|
|
77
86
|
const result = targetType.validate(-5);
|
|
78
87
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asUint16, expectType, isUint16, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { uint16 } from './uint16.mjs';
|
|
5
5
|
|
|
6
6
|
describe('uint16', () => {
|
|
@@ -67,6 +67,15 @@ describe('uint16', () => {
|
|
|
67
67
|
}
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
+
test('validate returns input as-is for OK cases', () => {
|
|
71
|
+
const input = 30000;
|
|
72
|
+
const result = targetType.validate(input);
|
|
73
|
+
expect(Result.isOk(result)).toBe(true);
|
|
74
|
+
if (Result.isOk(result)) {
|
|
75
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
70
79
|
test('falsy case - negative', () => {
|
|
71
80
|
const result = targetType.validate(-5);
|
|
72
81
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { asUint32, expectType, isUint32, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../../
|
|
3
|
+
import { validationErrorsToMessages } from '../../utils/index.mjs';
|
|
4
4
|
import { uint32 } from './uint32.mjs';
|
|
5
5
|
|
|
6
6
|
describe('uint32', () => {
|
|
@@ -67,6 +67,15 @@ describe('uint32', () => {
|
|
|
67
67
|
}
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
+
test('validate returns input as-is for OK cases', () => {
|
|
71
|
+
const input = 2000000000;
|
|
72
|
+
const result = targetType.validate(input);
|
|
73
|
+
expect(Result.isOk(result)).toBe(true);
|
|
74
|
+
if (Result.isOk(result)) {
|
|
75
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
70
79
|
test('falsy case - negative', () => {
|
|
71
80
|
const result = targetType.validate(-5);
|
|
72
81
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../
|
|
3
|
+
import { validationErrorsToMessages } from '../utils/index.mjs';
|
|
4
4
|
import { simpleBrandedNumber } from './simple-branded-number.mjs';
|
|
5
5
|
|
|
6
6
|
describe('simpleBrandedNumber', () => {
|
|
@@ -36,6 +36,15 @@ describe('simpleBrandedNumber', () => {
|
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
describe('validate', () => {
|
|
39
|
+
test('validate returns input as-is for OK cases', () => {
|
|
40
|
+
const input = 42;
|
|
41
|
+
const result = userIdType.validate(input);
|
|
42
|
+
expect(Result.isOk(result)).toBe(true);
|
|
43
|
+
if (Result.isOk(result)) {
|
|
44
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
39
48
|
test('succeeds for valid numbers', () => {
|
|
40
49
|
const result = userIdType.validate(42);
|
|
41
50
|
expect(Result.isOk(result)).toBe(true);
|
|
@@ -100,6 +109,15 @@ describe('simpleBrandedNumber', () => {
|
|
|
100
109
|
});
|
|
101
110
|
|
|
102
111
|
describe('validate', () => {
|
|
112
|
+
test('validate returns input as-is for OK cases', () => {
|
|
113
|
+
const input = 85;
|
|
114
|
+
const result = scoreType.validate(input);
|
|
115
|
+
expect(Result.isOk(result)).toBe(true);
|
|
116
|
+
if (Result.isOk(result)) {
|
|
117
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
103
121
|
test('succeeds for valid numbers', () => {
|
|
104
122
|
const result = scoreType.validate(85);
|
|
105
123
|
expect(Result.isOk(result)).toBe(true);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expectType, Result } from 'ts-data-forge';
|
|
2
2
|
import { type TypeOf } from '../type.mjs';
|
|
3
|
-
import { validationErrorsToMessages } from '../
|
|
3
|
+
import { validationErrorsToMessages } from '../utils/index.mjs';
|
|
4
4
|
import { simpleBrandedString } from './simple-branded-string.mjs';
|
|
5
5
|
|
|
6
6
|
describe('simpleBrandedString', () => {
|
|
@@ -37,6 +37,15 @@ describe('simpleBrandedString', () => {
|
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
describe('validate', () => {
|
|
40
|
+
test('validate returns input as-is for OK cases', () => {
|
|
41
|
+
const input = 'john_doe';
|
|
42
|
+
const result = userNameType.validate(input);
|
|
43
|
+
expect(Result.isOk(result)).toBe(true);
|
|
44
|
+
if (Result.isOk(result)) {
|
|
45
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
40
49
|
test('succeeds for valid strings', () => {
|
|
41
50
|
const result = userNameType.validate('john_doe');
|
|
42
51
|
expect(Result.isOk(result)).toBe(true);
|
|
@@ -101,6 +110,15 @@ describe('simpleBrandedString', () => {
|
|
|
101
110
|
});
|
|
102
111
|
|
|
103
112
|
describe('validate', () => {
|
|
113
|
+
test('validate returns input as-is for OK cases', () => {
|
|
114
|
+
const input = 'technology';
|
|
115
|
+
const result = categoryType.validate(input);
|
|
116
|
+
expect(Result.isOk(result)).toBe(true);
|
|
117
|
+
if (Result.isOk(result)) {
|
|
118
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
104
122
|
test('succeeds for valid strings', () => {
|
|
105
123
|
const result = categoryType.validate('technology');
|
|
106
124
|
expect(Result.isOk(result)).toBe(true);
|
package/src/compose/index.mts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { expectType, Result } from 'ts-data-forge';
|
|
1
|
+
import { Arr, expectType, Result } from 'ts-data-forge';
|
|
2
2
|
import { type Type, type TypeOf } from '../type.mjs';
|
|
3
|
-
import { createAssertFn, createCastFn, createIsFn } from '../utils/index.mjs';
|
|
4
3
|
import {
|
|
4
|
+
createAssertFn,
|
|
5
|
+
createCastFn,
|
|
6
|
+
createIsFn,
|
|
5
7
|
type ValidationError,
|
|
6
8
|
type ValidationErrorWithMessage,
|
|
7
|
-
} from '../
|
|
9
|
+
} from '../utils/index.mjs';
|
|
8
10
|
|
|
9
11
|
export const intersection = <const Types extends NonEmptyArray<Type<unknown>>>(
|
|
10
12
|
types: Types,
|
|
@@ -22,22 +24,24 @@ export const intersection = <const Types extends NonEmptyArray<Type<unknown>>>(
|
|
|
22
24
|
`Intersection<${types.map((a) => a.typeName).join(', ')}>`;
|
|
23
25
|
|
|
24
26
|
const validate: Type<T>['validate'] = (a) => {
|
|
25
|
-
const errors: readonly ValidationError[] =
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
const errors: readonly ValidationError[] = Arr.generate(function* () {
|
|
28
|
+
for (const type of types) {
|
|
29
|
+
const res = type.validate(a);
|
|
30
|
+
|
|
31
|
+
if (Result.isErr(res)) {
|
|
32
|
+
yield {
|
|
33
|
+
path: [],
|
|
34
|
+
actualValue: a,
|
|
35
|
+
expectedType: typeNameFilled,
|
|
36
|
+
message: `The type of value is expected to match all types of { ${types
|
|
37
|
+
.map((t) => t.typeName)
|
|
38
|
+
.join(', ')} }`,
|
|
39
|
+
typeName: typeNameFilled,
|
|
40
|
+
} satisfies ValidationErrorWithMessage;
|
|
41
|
+
|
|
42
|
+
yield* res.value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
41
45
|
});
|
|
42
46
|
|
|
43
47
|
if (errors.length > 0) {
|
|
@@ -6,7 +6,7 @@ import { type TypeOf } from '../type.mjs';
|
|
|
6
6
|
import {
|
|
7
7
|
type ValidationError,
|
|
8
8
|
validationErrorsToMessages,
|
|
9
|
-
} from '../
|
|
9
|
+
} from '../utils/index.mjs';
|
|
10
10
|
import { intersection } from './intersection.mjs';
|
|
11
11
|
|
|
12
12
|
describe('intersection', () => {
|
|
@@ -90,6 +90,15 @@ describe('intersection', () => {
|
|
|
90
90
|
}
|
|
91
91
|
});
|
|
92
92
|
|
|
93
|
+
test('validate returns input as-is for OK cases', () => {
|
|
94
|
+
const input = { x: 0, y: 1, z: 2, w: 3 };
|
|
95
|
+
const result = targetType.validate(input);
|
|
96
|
+
expect(Result.isOk(result)).toBe(true);
|
|
97
|
+
if (Result.isOk(result)) {
|
|
98
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
93
102
|
test('falsy case', () => {
|
|
94
103
|
const result = targetType.validate({ x: 0, y: 1 });
|
|
95
104
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -194,6 +203,15 @@ describe('intersection', () => {
|
|
|
194
203
|
}
|
|
195
204
|
});
|
|
196
205
|
|
|
206
|
+
test('validate returns input as-is for OK cases', () => {
|
|
207
|
+
const input = 0;
|
|
208
|
+
const result = targetType.validate(input);
|
|
209
|
+
expect(Result.isOk(result)).toBe(true);
|
|
210
|
+
if (Result.isOk(result)) {
|
|
211
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
197
215
|
test('falsy case', () => {
|
|
198
216
|
const result = targetType.validate('aaa');
|
|
199
217
|
expect(Result.isErr(result)).toBe(true);
|
|
@@ -320,6 +338,15 @@ describe('intersection', () => {
|
|
|
320
338
|
}
|
|
321
339
|
});
|
|
322
340
|
|
|
341
|
+
test('validate returns input as-is for OK cases', () => {
|
|
342
|
+
const input = 3;
|
|
343
|
+
const result = targetType.validate(input);
|
|
344
|
+
expect(Result.isOk(result)).toBe(true);
|
|
345
|
+
if (Result.isOk(result)) {
|
|
346
|
+
expect(result.value).toBe(input); // ✅ same reference
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
|
|
323
350
|
test('falsy case', () => {
|
|
324
351
|
const result = targetType.validate(7);
|
|
325
352
|
expect(Result.isErr(result)).toBe(true);
|