Skip to content

Commit 3cd627f

Browse files
committed
create objectWithPatterns schema
1 parent b359f34 commit 3cd627f

File tree

4 files changed

+768
-0
lines changed

4 files changed

+768
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './objectWithPatterns.ts';
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { describe, expectTypeOf, test } from 'vitest';
2+
import { transform } from '../../actions/index.ts';
3+
import { pipe } from '../../methods/index.ts';
4+
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
5+
import type { BooleanIssue, BooleanSchema } from '../boolean/boolean.ts';
6+
import { boolean } from '../boolean/boolean.ts';
7+
import { custom } from '../custom/custom.ts';
8+
import type { CustomIssue } from '../custom/types.ts';
9+
import type { NumberIssue, NumberSchema } from '../number/number.ts';
10+
import { number } from '../number/number.ts';
11+
import type { StringIssue, StringSchema } from '../string/string.ts';
12+
import { string } from '../string/string.ts';
13+
import {
14+
objectWithPatterns,
15+
type ObjectWithPatternsIssue,
16+
type ObjectWithPatternsSchema,
17+
} from './objectWithPatterns.ts';
18+
19+
const FooKeySchema = custom<`foo(${string})`>(
20+
(input) =>
21+
typeof input === 'string' && input.startsWith('foo(') && input.endsWith(')')
22+
);
23+
24+
const BarKeysSchema = pipe(
25+
custom<`bar(${string})`>(
26+
(input) =>
27+
typeof input === 'string' &&
28+
input.startsWith('bar(') &&
29+
input.endsWith(')')
30+
),
31+
transform((input) => input.toUpperCase() as Uppercase<typeof input>)
32+
);
33+
34+
describe('objectWithPatterns', () => {
35+
describe('should return schema object', () => {
36+
test('without message', () => {
37+
expectTypeOf(
38+
objectWithPatterns(
39+
[
40+
[FooKeySchema, string()],
41+
[BarKeysSchema, number()],
42+
],
43+
boolean()
44+
)
45+
).toEqualTypeOf<
46+
ObjectWithPatternsSchema<
47+
readonly [
48+
readonly [typeof FooKeySchema, StringSchema<undefined>],
49+
readonly [typeof BarKeysSchema, NumberSchema<undefined>],
50+
],
51+
BooleanSchema<undefined>,
52+
undefined
53+
>
54+
>();
55+
});
56+
test('with message', () => {
57+
expectTypeOf(
58+
objectWithPatterns(
59+
[
60+
[FooKeySchema, string()],
61+
[BarKeysSchema, number()],
62+
],
63+
boolean(),
64+
'message'
65+
)
66+
).toEqualTypeOf<
67+
ObjectWithPatternsSchema<
68+
readonly [
69+
readonly [typeof FooKeySchema, StringSchema<undefined>],
70+
readonly [typeof BarKeysSchema, NumberSchema<undefined>],
71+
],
72+
BooleanSchema<undefined>,
73+
'message'
74+
>
75+
>();
76+
});
77+
});
78+
describe('should return inference types', () => {
79+
test('of input', () => {
80+
expectTypeOf<
81+
InferInput<
82+
ObjectWithPatternsSchema<
83+
readonly [
84+
readonly [typeof FooKeySchema, StringSchema<undefined>],
85+
readonly [typeof BarKeysSchema, NumberSchema<undefined>],
86+
],
87+
BooleanSchema<undefined>,
88+
undefined
89+
>
90+
>
91+
>().toEqualTypeOf<
92+
{
93+
[key: `foo(${string})`]: string | undefined;
94+
[key: `bar(${string})`]: number | undefined;
95+
} & { [key: string]: boolean }
96+
>();
97+
});
98+
test('of output', () => {
99+
expectTypeOf<
100+
InferOutput<
101+
ObjectWithPatternsSchema<
102+
readonly [
103+
readonly [typeof FooKeySchema, StringSchema<undefined>],
104+
readonly [typeof BarKeysSchema, NumberSchema<undefined>],
105+
],
106+
BooleanSchema<undefined>,
107+
undefined
108+
>
109+
>
110+
>().toEqualTypeOf<
111+
{
112+
[key: `foo(${string})`]: string | undefined;
113+
[key: `BAR(${Uppercase<string>})`]: number | undefined;
114+
} & { [key: string]: boolean }
115+
>();
116+
});
117+
test('of issue', () => {
118+
expectTypeOf<
119+
InferIssue<
120+
ObjectWithPatternsSchema<
121+
readonly [
122+
readonly [typeof FooKeySchema, StringSchema<undefined>],
123+
readonly [typeof BarKeysSchema, NumberSchema<undefined>],
124+
],
125+
BooleanSchema<undefined>,
126+
undefined
127+
>
128+
>
129+
>().toEqualTypeOf<
130+
| ObjectWithPatternsIssue
131+
| CustomIssue
132+
| StringIssue
133+
| NumberIssue
134+
| BooleanIssue
135+
>();
136+
});
137+
});
138+
});

0 commit comments

Comments
 (0)