-
-
Notifications
You must be signed in to change notification settings - Fork 533
Fix oneOf
number const
#1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix oneOf
number const
#1056
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ export function defaultSchemaObjectTransform( | |
} | ||
|
||
// const (valid for any type) | ||
if (schemaObject.const) { | ||
if (schemaObject.const !== null && schemaObject.const !== undefined) { | ||
let schemaConst = schemaObject.const as any; | ||
if ("type" in schemaObject) { | ||
if (schemaObject.type === "string") { | ||
|
@@ -98,7 +98,7 @@ export function defaultSchemaObjectTransform( | |
// oneOf (no discriminator) | ||
if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) { | ||
const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx })); | ||
if (maybeTypes.some((t) => t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{") | ||
if (maybeTypes.some((t) => typeof t === "string" && t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return tsUnionOf(...maybeTypes); // otherwise, TS union works for primitives | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { tsUnionOf } from "../src/utils"; | ||
|
||
describe("utils", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests aren’t really necessary as they are all tested by proxy with other tests, and these are such simple functions with such simple inputs/outputs they have clear and obvious failures. However, the coverage is appreciated and it doesn’t hurt to have. Thank you. |
||
describe("tsUnionOf", () => { | ||
test("primitive", () => { | ||
return expect(tsUnionOf(...["string", "number", "boolean"])).toBe("string | number | boolean"); | ||
}); | ||
test("constant booleans", () => { | ||
return expect(tsUnionOf(...[true, false])).toBe("true | false"); | ||
}); | ||
test("constant strings", () => { | ||
return expect(tsUnionOf(...['"hello"', '"world"'])).toBe('"hello" | "world"'); | ||
}); | ||
test("constant numbers", () => { | ||
return expect(tsUnionOf(...[0, 1, 2, 3])).toBe("0 | 1 | 2 | 3"); | ||
}); | ||
test("mixed", () => { | ||
return expect(tsUnionOf(...[0, true, "string", '"hello"'])).toBe('0 | true | string | "hello"'); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍