Skip to content
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

feature: parse {} as UnknownSchema #51

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.7.5

- parse {} as UnknownSchema [src](https://github.com/xddq/schema2typebox/pull/51)
FosterSamuel marked this conversation as resolved.
Show resolved Hide resolved

# 1.7.4

- support unions containing objects and/or arrays [src](https://github.com/xddq/schema2typebox/pull/48)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schema2typebox",
"version": "1.7.4",
"version": "1.7.5",
"main": "dist/src/index.js",
"description": "Creates typebox code from JSON schemas",
"source": "dist/src/index.js",
Expand Down
7 changes: 7 additions & 0 deletions src/schema-matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export const isConstSchema = (schema: JSONSchema7): schema is ConstSchema => {
return schema.const !== undefined;
};

export type UnknownSchema = JSONSchema7 & Record<string, never>;
export const isUnknownSchema = (
schema: JSONSchema7
): schema is UnknownSchema => {
return typeof schema === "object" && Object.keys(schema).length === 0;
};

export type MultipleTypesSchema = JSONSchema7 & { type: JSONSchema7TypeName[] };
export const isSchemaWithMultipleTypes = (
schema: JSONSchema7
Expand Down
8 changes: 8 additions & 0 deletions src/schema-to-typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
NotSchema,
ObjectSchema,
OneOfSchema,
UnknownSchema,
isAllOfSchema,
isAnyOfSchema,
isArraySchema,
Expand All @@ -28,6 +29,7 @@ import {
isObjectSchema,
isOneOfSchema,
isSchemaWithMultipleTypes,
isUnknownSchema,
} from "./schema-matchers";

type Code = string;
Expand Down Expand Up @@ -86,6 +88,8 @@ export const collect = (schema: JSONSchema7Definition): Code => {
return parseWithMultipleTypes(schema);
} else if (isConstSchema(schema)) {
return parseConst(schema);
} else if (isUnknownSchema(schema)) {
return parseUnknown(schema);
} else if (schema.type !== undefined && !Array.isArray(schema.type)) {
return parseTypeName(schema.type, schema);
}
Expand Down Expand Up @@ -213,6 +217,10 @@ export const parseConst = (schema: ConstSchema): Code => {
: `Type.Literal(${schema.const}, ${schemaOptions})`;
};

export const parseUnknown = (_: UnknownSchema): Code => {
return "Type.Unknown()";
};

export const parseType = (type: JSONSchema7Type): Code => {
if (isString(type)) {
return `Type.Literal("${type}")`;
Expand Down
11 changes: 11 additions & 0 deletions test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NotSchema,
ObjectSchema,
OneOfSchema,
UnknownSchema,
} from "../src/schema-matchers";
import {
parseAllOf,
Expand All @@ -21,6 +22,7 @@ import {
parseObject,
parseOneOf,
parseTypeName,
parseUnknown,
parseWithMultipleTypes,
} from "../src/schema-to-typebox";
import { expectEqualIgnoreFormatting } from "./util";
Expand Down Expand Up @@ -133,6 +135,15 @@ describe("parser unit tests", () => {
});
});

describe("parseUnknown() - when parsing an empty schema", () => {
it("returns Type.Unknown()", async () => {
const dummySchema: UnknownSchema = {};
const result = parseUnknown(dummySchema);

expect(result).toEqual("Type.Unknown()");
});
});

describe("parseAnyOf() - when parsing an anyOf schema", () => {
it("returns Type.Union()", () => {
const dummySchema: AnyOfSchema = {
Expand Down
Loading
Loading