Skip to content

Commit

Permalink
Fix null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Apr 20, 2024
1 parent a79196e commit aa8fe02
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 67 deletions.
10 changes: 6 additions & 4 deletions src/TypeFormatter/LiteralUnionTypeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter {
let hasString = false;
let preserveLiterals = false;
let allStrings = true;
let hasNull = false;

const flattenedTypes = flattenTypes(type);

Expand All @@ -27,9 +28,10 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter {
hasString = true;
preserveLiterals = preserveLiterals || t.getPreserveLiterals();
return false;
}

if (t instanceof LiteralType && !t.isString()) {
} else if (t instanceof NullType) {
hasNull = true;
return true;
} else if (t instanceof LiteralType && !t.isString()) {
allStrings = false;
}

Expand All @@ -38,7 +40,7 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter {

if (allStrings && hasString && !preserveLiterals) {
return {
type: "string",
type: hasNull ? ["string", "null"] : "string",
};
}

Expand Down
1 change: 1 addition & 0 deletions test/valid-data/string-literals-hack/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type MyObject = {
stringWithNull: string | null;
literalWithNull: "foo" | "bar" | null;
literalWithString: "foo" | "bar" | string;
literalWithStringAndNull: "foo" | "bar" | string | null;
withRef: "foo" | Union;
withRefWithString: Union | string;
withHack: "foo" | "bar" | (string & {});
Expand Down
7 changes: 7 additions & 0 deletions test/valid-data/string-literals-hack/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"literalWithString": {
"type": "string"
},
"literalWithStringAndNull": {
"type": [
"string",
"null"
]
},
"literals": {
"enum": [
"foo",
Expand Down Expand Up @@ -95,6 +101,7 @@
"stringWithNull",
"literalWithNull",
"literalWithString",
"literalWithStringAndNull",
"withRef",
"withRefWithString",
"withHack",
Expand Down
97 changes: 34 additions & 63 deletions test/vega-lite/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4428,46 +4428,37 @@
]
},
"BinnedTimeUnit": {
"anyOf": [
{
"enum": [
"binnedyear",
"binnedyearquarter",
"binnedyearquartermonth",
"binnedyearmonth",
"binnedyearmonthdate",
"binnedyearmonthdatehours",
"binnedyearmonthdatehoursminutes",
"binnedyearmonthdatehoursminutesseconds",
"binnedyearweek",
"binnedyearweekday",
"binnedyearweekdayhours",
"binnedyearweekdayhoursminutes",
"binnedyearweekdayhoursminutesseconds",
"binnedyeardayofyear"
],
"type": "string"
},
{
"enum": [
"binnedutcyear",
"binnedutcyearquarter",
"binnedutcyearquartermonth",
"binnedutcyearmonth",
"binnedutcyearmonthdate",
"binnedutcyearmonthdatehours",
"binnedutcyearmonthdatehoursminutes",
"binnedutcyearmonthdatehoursminutesseconds",
"binnedutcyearweek",
"binnedutcyearweekday",
"binnedutcyearweekdayhours",
"binnedutcyearweekdayhoursminutes",
"binnedutcyearweekdayhoursminutesseconds",
"binnedutcyeardayofyear"
],
"type": "string"
}
]
"enum": [
"binnedyear",
"binnedyearquarter",
"binnedyearquartermonth",
"binnedyearmonth",
"binnedyearmonthdate",
"binnedyearmonthdatehours",
"binnedyearmonthdatehoursminutes",
"binnedyearmonthdatehoursminutesseconds",
"binnedyearweek",
"binnedyearweekday",
"binnedyearweekdayhours",
"binnedyearweekdayhoursminutes",
"binnedyearweekdayhoursminutesseconds",
"binnedyeardayofyear",
"binnedutcyear",
"binnedutcyearquarter",
"binnedutcyearquartermonth",
"binnedutcyearmonth",
"binnedutcyearmonthdate",
"binnedutcyearmonthdatehours",
"binnedutcyearmonthdatehoursminutes",
"binnedutcyearmonthdatehoursminutesseconds",
"binnedutcyearweek",
"binnedutcyearweekday",
"binnedutcyearweekdayhours",
"binnedutcyearweekdayhoursminutes",
"binnedutcyearweekdayhoursminutesseconds",
"binnedutcyeardayofyear"
],
"type": "string"
},
"Blend": {
"enum": [
Expand Down Expand Up @@ -19228,29 +19219,9 @@
"type": "object"
},
"ParseValue": {
"anyOf": [
{
"type": "null"
},
{
"type": "string"
},
{
"const": "string",
"type": "string"
},
{
"const": "boolean",
"type": "string"
},
{
"const": "date",
"type": "string"
},
{
"const": "number",
"type": "string"
}
"type": [
"string",
"null"
]
},
"PivotTransform": {
Expand Down

0 comments on commit aa8fe02

Please sign in to comment.