Skip to content

Commit c16711d

Browse files
authoredAug 2, 2023
fix(gettypes.ts): add null type if nullable is true (#21)
1 parent 7d1bc9d commit c16711d

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed
 

‎src/accessors/getTypes.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1-
import type { SchemaNodeKind } from '../nodes/types';
1+
import { SchemaNodeKind } from '../nodes/types';
22
import type { SchemaFragment } from '../types';
33
import { isValidType } from './guards/isValidType';
44
import { inferType } from './inferType';
55

66
export function getTypes(fragment: SchemaFragment): SchemaNodeKind[] | null {
7+
const types: SchemaNodeKind[] = [];
8+
let isNullable = false;
9+
10+
if ('nullable' in fragment) {
11+
if (fragment.nullable === true) {
12+
isNullable = true;
13+
}
14+
}
715
if ('type' in fragment) {
816
if (Array.isArray(fragment.type)) {
9-
return fragment.type.filter(isValidType);
17+
types.push(...fragment.type.filter(isValidType));
1018
} else if (isValidType(fragment.type)) {
11-
return [fragment.type];
19+
types.push(fragment.type);
1220
}
21+
if (isNullable && !types.includes(SchemaNodeKind.Null)) {
22+
types.push(SchemaNodeKind.Null);
23+
}
24+
return types;
1325
}
1426

1527
const inferredType = inferType(fragment);
1628
if (inferredType !== null) {
17-
return [inferredType];
29+
types.push(inferredType);
30+
if (isNullable && !types.includes(SchemaNodeKind.Null)) {
31+
types.push(SchemaNodeKind.Null);
32+
}
33+
return types;
1834
}
1935

2036
return null;

0 commit comments

Comments
 (0)