Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 03fbecf

Browse files
committed
feat: support readonly types
1 parent 883ec4b commit 03fbecf

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Currently supports (and their nested closure):
5252
* `t.keyof`
5353
* `t.tuple`
5454
* `t.exact`
55+
* `t.readonly`
5556

5657
## Use Cases
5758

src/core/core.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export type BasicType =
1919
| t.KeyofType<{ [key: string]: unknown }>
2020
| t.TupleType<t.Mixed[]>
2121
| t.ExactType<t.Mixed>
22+
| t.ReadonlyType<t.Mixed>
2223
// not yet supported:
2324
| t.AnyArrayType
2425
| t.AnyDictionaryType
2526
| t.RefinementType<t.Mixed>
2627
| t.RecursiveType<t.Mixed>
2728
| t.DictionaryType<t.Mixed, t.Mixed>
28-
| t.ReadonlyType<t.Mixed>
2929
| t.ReadonlyArrayType<t.Mixed>;
3030

3131
export type basicFuzzGenerator<
@@ -171,6 +171,18 @@ export function fuzzExact(b: t.ExactC<t.HasProps>): ConcreteFuzzer<unknown> {
171171
};
172172
}
173173

174+
export function fuzzReadonly(
175+
b: t.ReadonlyType<t.Any>
176+
): ConcreteFuzzer<unknown> {
177+
return {
178+
children: [b.type],
179+
func: (n, h0) => {
180+
const r = h0.encode(n);
181+
return Object.freeze(r);
182+
},
183+
};
184+
}
185+
174186
export const defaultMaxArrayLength = 13;
175187

176188
const fuzzArrayWithMaxLength = (maxLength: number = defaultMaxArrayLength) => (
@@ -317,6 +329,7 @@ export const coreFuzzers = [
317329
partialFuzzer(),
318330
arrayFuzzer(),
319331
gen(fuzzExact, 'ExactType'),
332+
gen(fuzzReadonly, 'ReadonlyType'),
320333
gen(fuzzUnion, 'UnionType'),
321334
gen(fuzzIntersection, 'IntersectionType'),
322335
gen(fuzzLiteral, 'LiteralType'),

test/helpers.ts

+51-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ export const types = [
1515
t.exact(t.type({ s: t.string, m: t.number, ___0000_extra_: t.string })),
1616
t.exact(t.partial({ s: t.string, m: t.number })),
1717
t.exact(t.partial({ s: t.string, m: t.number, ___0000_extra_: t.boolean })),
18-
t.exact(
19-
t.intersection([
20-
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
21-
t.type({ s: t.string, j: t.boolean }),
22-
])
23-
),
24-
t.exact(
25-
t.intersection([
26-
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
27-
t.type({ s2: t.string, j: t.boolean }),
28-
])
29-
),
3018
t.null,
3119
t.undefined,
3220
t.void,
@@ -45,7 +33,42 @@ export const types = [
4533
0.3: null,
4634
}),
4735
t.tuple([t.number, t.string, t.boolean]),
36+
t.readonly(t.string),
37+
t.readonly(t.tuple([t.string, t.boolean])),
38+
t.readonly(t.type({ s: t.string, j: t.boolean })),
4839
// Complex types
40+
t.exact(
41+
t.intersection([
42+
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
43+
t.type({ s: t.string, j: t.boolean }),
44+
])
45+
),
46+
t.exact(
47+
t.intersection([
48+
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
49+
t.type({ s2: t.string, j: t.boolean }),
50+
])
51+
),
52+
t.readonly(
53+
t.intersection([
54+
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
55+
t.type({ s: t.string, j: t.boolean }),
56+
])
57+
),
58+
t.exact(
59+
t.intersection([
60+
t.readonly(
61+
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean })
62+
),
63+
t.type({ s2: t.string, j: t.boolean }),
64+
])
65+
),
66+
t.exact(
67+
t.intersection([
68+
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
69+
t.readonly(t.type({ s2: t.string, j: t.boolean })),
70+
])
71+
),
4972
t.intersection([
5073
t.type({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
5174
t.type({ s: t.string, j: t.boolean }),
@@ -78,6 +101,22 @@ export const types = [
78101
t.partial({ s: t.string, m: t.number, ___0000_extra_: t.boolean }),
79102
t.partial({ s2: t.string, j: t.boolean }),
80103
]),
104+
t.union([
105+
t.readonly(
106+
t.partial({ s: t.string, m: t.number, ___0000_extra_: t.boolean })
107+
),
108+
t.readonly(t.partial({ s: t.string, j: t.boolean })),
109+
]),
110+
t.union([
111+
t.readonly(
112+
t.partial({ s: t.string, m: t.number, ___0000_extra_: t.boolean })
113+
),
114+
t.readonly(t.partial({ s2: t.string, j: t.boolean })),
115+
]),
116+
t.union([
117+
t.readonly(t.partial({ s: t.string, m: t.number })),
118+
t.readonly(t.partial({ s2: t.string, j: t.boolean })),
119+
]),
81120
t.type({ s: t.string, m: t.type({ n: t.Int }) }),
82121
t.type({
83122
s: t.union([t.string, t.number, t.partial({ n: t.number, z: t.string })]),

0 commit comments

Comments
 (0)