schema2typebox
Version:
Creates typebox code from JSON schemas
436 lines • 19.3 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
const schema_to_typebox_1 = require("../src/schema-to-typebox");
const util_1 = require("./util");
(0, globals_1.describe)("parser unit tests", () => {
(0, globals_1.describe)("parseObject() - when parsing an object schema", () => {
(0, globals_1.it)("returns Type.Unknown() it the object has no properties", () => {
const dummySchema = {
type: "object",
properties: undefined,
};
const result = (0, schema_to_typebox_1.parseObject)(dummySchema);
(0, globals_1.expect)(result).toContain("Type.Unknown");
});
(0, globals_1.it)("creates code with attributes for each property", async () => {
const dummySchema = {
type: "object",
properties: {
a: {
type: "number",
},
b: {
type: "string",
},
},
required: ["b"],
};
const result = (0, schema_to_typebox_1.parseObject)(dummySchema);
const expectedResult = `Type.Object({a: Type.Optional(Type.Number()),\n b: Type.String()})`;
await (0, util_1.expectEqualIgnoreFormatting)(result, expectedResult);
});
(0, globals_1.it)("works for different attribute naming schemes", async () => {
const dummySchema = {
type: "object",
properties: {
"@prop": {
type: "string",
},
"6": {
type: "boolean",
},
unquoted: {
type: "number",
},
__underscores: {
type: "string",
},
" spaces are weirdly valid ": {
type: "number",
},
"with-hyphen": {
type: "string",
},
$: {
type: "string",
},
},
required: [
"@prop",
"6",
"unquoted",
"__underscores",
" spaces are weirdly valid ",
"with-hyphen",
"$",
],
};
const result = (0, schema_to_typebox_1.parseObject)(dummySchema);
const expectedResult = `Type.Object({"6": Type.Boolean(),\n "@prop": Type.String(),\n unquoted: Type.Number(),\n __underscores: Type.String(),\n " spaces are weirdly valid ": Type.Number(),\n "with-hyphen": Type.String(),\n $: Type.String()})`;
await (0, util_1.expectEqualIgnoreFormatting)(result, expectedResult);
});
(0, globals_1.it)("creates code with schemaOptions", async () => {
const dummySchema = {
$id: "AnyStringHere",
type: "object",
properties: {
a: {
type: "number",
},
b: {
type: "string",
},
},
required: ["b"],
};
const result = (0, schema_to_typebox_1.parseObject)(dummySchema);
const expectedResult = `Type.Object({a: Type.Optional(Type.Number()),\n b: Type.String()}, { $id: "AnyStringHere" })`;
await (0, util_1.expectEqualIgnoreFormatting)(expectedResult, result);
});
});
(0, globals_1.describe)("parseEnum() - when parsing an enum schema", () => {
(0, globals_1.it)("returns Type.Union()", () => {
const dummySchema = {
title: "Status",
enum: ["unknown", 1, null],
};
const result = (0, schema_to_typebox_1.parseEnum)(dummySchema);
(0, globals_1.expect)(result).toContain("Type.Union");
});
(0, globals_1.it)("creates code with schemaOptions", () => {
const dummySchema = {
$id: "AnyStringHere",
title: "Status",
enum: ["unknown", 1, null],
};
const result = (0, schema_to_typebox_1.parseEnum)(dummySchema);
(0, globals_1.expect)(result).toContain("Type.Union");
(0, globals_1.expect)(result).toContain('{"$id":"AnyStringHere"}');
});
});
(0, globals_1.describe)("parseUnknown() - when parsing an empty schema", () => {
(0, globals_1.it)("returns Type.Unknown()", async () => {
const dummySchema = {};
const result = (0, schema_to_typebox_1.parseUnknown)(dummySchema);
(0, globals_1.expect)(result).toEqual("Type.Unknown()");
});
});
(0, globals_1.describe)("parseAnyOf() - when parsing an anyOf schema", () => {
(0, globals_1.it)("returns Type.Union()", () => {
const dummySchema = {
anyOf: [
{
type: "string",
},
{
type: "number",
},
],
};
const result = (0, schema_to_typebox_1.parseAnyOf)(dummySchema);
(0, globals_1.expect)(result).toContain("Type.Union");
});
(0, globals_1.it)("creates one type per list of items inside anyOf", () => {
const dummySchema = {
anyOf: [
{
type: "string",
},
{
type: "number",
},
],
};
const result = (0, schema_to_typebox_1.parseAnyOf)(dummySchema);
(0, globals_1.expect)(result).toContain("Type.String()");
(0, globals_1.expect)(result).toContain("Type.Number()");
});
(0, globals_1.it)("creates code with schemaOptions", () => {
const dummySchema = {
$id: "AnyStringHere",
anyOf: [
{
type: "string",
},
{
type: "number",
},
],
};
const result = (0, schema_to_typebox_1.parseAnyOf)(dummySchema);
(0, globals_1.expect)(result).toContain("Type.Union");
(0, globals_1.expect)(result).toContain('{"$id":"AnyStringHere"}');
});
});
(0, globals_1.describe)("parseAllOf() - when parsing an allOf schema", () => {
(0, globals_1.it)("returns Type.Intersect()", () => {
const schema = {
allOf: [
{
type: "string",
},
],
};
const result = (0, schema_to_typebox_1.parseAllOf)(schema);
(0, globals_1.expect)(result).toContain("Type.Intersect");
});
(0, globals_1.it)("creates one type per list of items inside allOf", () => {
const schema = {
allOf: [
{
type: "string",
},
{
type: "number",
},
],
};
const result = (0, schema_to_typebox_1.parseAllOf)(schema);
(0, globals_1.expect)(result).toContain(`Type.String()`);
(0, globals_1.expect)(result).toContain(`Type.Number()`);
});
(0, globals_1.it)("creates code with schemaOptions", () => {
const schema = {
$id: "AnyStringHere",
allOf: [
{
type: "string",
},
],
};
const result = (0, schema_to_typebox_1.parseAllOf)(schema);
(0, globals_1.expect)(result).toContain('{"$id":"AnyStringHere"}');
});
});
(0, globals_1.describe)("parseOneOf() - when parsing a oneOf schema", () => {
(0, globals_1.it)("returns OneOf()", () => {
const schema = {
oneOf: [
{
type: "string",
},
],
};
const result = (0, schema_to_typebox_1.parseOneOf)(schema);
(0, globals_1.expect)(result).toContain(`OneOf`);
});
(0, globals_1.it)("creates one type per list of items inside oneOf", () => {
const schema = {
oneOf: [
{
type: "string",
},
{
type: "number",
},
],
};
const result = (0, schema_to_typebox_1.parseOneOf)(schema);
(0, globals_1.expect)(result).toContain(`Type.String()`);
(0, globals_1.expect)(result).toContain(`Type.Number()`);
});
(0, globals_1.it)("creates code with schemaOptions", () => {
const schema = {
$id: "AnyStringHere",
oneOf: [
{
type: "string",
},
],
};
const result = (0, schema_to_typebox_1.parseOneOf)(schema);
(0, globals_1.expect)(result).toContain('{"$id":"AnyStringHere"}');
});
});
(0, globals_1.describe)("parseNot() - when parsing a not schema", () => {
(0, globals_1.it)("returns Type.Not()", () => {
const schema = {
not: {
type: "number",
},
};
const result = (0, schema_to_typebox_1.parseNot)(schema);
(0, globals_1.expect)(result).toContain(`Type.Not`);
});
(0, globals_1.it)("creates code with schemaOptions", () => {
const schema = {
$id: "AnyStringHere",
not: {
type: "number",
},
};
const result = (0, schema_to_typebox_1.parseNot)(schema);
(0, globals_1.expect)(result).toContain('{"$id":"AnyStringHere"}');
});
});
(0, globals_1.describe)("parseArray() - when parsing an array schema", () => {
(0, globals_1.describe)('when "items" is not a list', () => {
(0, globals_1.it)("returns Type.Array", () => {
const schema = {
type: "array",
items: { type: "string" },
};
const result = (0, schema_to_typebox_1.parseArray)(schema);
(0, globals_1.expect)(result).toContain(`Type.Array`);
});
(0, globals_1.it)("creates schemaOptions", () => {
const schema = {
type: "array",
items: { type: "string", description: "test description" },
};
const result = (0, schema_to_typebox_1.parseArray)(schema);
(0, globals_1.expect)(result).toContain(JSON.stringify({ description: "test description" }));
});
});
(0, globals_1.describe)('when "items" is a list', () => {
(0, globals_1.it)("creates a Type.Union containing each item", () => {
const schema = {
type: "array",
items: [{ type: "string" }, { type: "null" }],
};
const result = (0, schema_to_typebox_1.parseArray)(schema);
(0, globals_1.expect)(result).toContain(`Type.Array(Type.Union`);
(0, globals_1.expect)(result).toContain(`Type.String`);
(0, globals_1.expect)(result).toContain(`Type.Null`);
});
(0, globals_1.it)("creates schemaOptions", () => {
const schema = {
type: "array",
items: [
{ type: "string", description: "test description" },
{ type: "number", minimum: 1 },
],
};
const result = (0, schema_to_typebox_1.parseArray)(schema);
(0, globals_1.expect)(result).toContain(JSON.stringify({ description: "test description" }));
(0, globals_1.expect)(result).toContain(JSON.stringify({ minimum: 1 }));
});
});
(0, globals_1.describe)('when "items" is undefined', () => {
(0, globals_1.it)("returns Type.Array and Type.Unknown", () => {
const schema = {
type: "array",
};
const result = (0, schema_to_typebox_1.parseArray)(schema);
(0, globals_1.expect)(result).toContain(`Type.Array`);
(0, globals_1.expect)(result).toContain(`Type.Unknown`);
});
(0, globals_1.it)("creates schemaOptions", () => {
const schema = {
type: "array",
description: "test description",
};
const result = (0, schema_to_typebox_1.parseArray)(schema);
(0, globals_1.expect)(result).toContain(JSON.stringify({ description: "test description" }));
});
});
});
(0, globals_1.describe)("parseWithMultipleTypes() - when parsing a schema where 'types' is a list", () => {
(0, globals_1.it)("returns Type.Union()", () => {
const schema = {
type: ["string"],
};
const result = (0, schema_to_typebox_1.parseWithMultipleTypes)(schema);
(0, globals_1.expect)(result).toContain(`Type.Union`);
});
(0, globals_1.it)("creates one type for each type in the list", () => {
const schema = {
type: ["string", "null"],
};
const result = (0, schema_to_typebox_1.parseWithMultipleTypes)(schema);
(0, globals_1.expect)(result).toContain(`Type.Union`);
(0, globals_1.expect)(result).toContain(`Type.String`);
(0, globals_1.expect)(result).toContain(`Type.Null`);
});
(0, globals_1.it)("creates union types for nullable objects", async () => {
const schema = {
type: ["object", "null"],
properties: {},
};
const result = (0, schema_to_typebox_1.parseWithMultipleTypes)(schema);
await (0, util_1.expectEqualIgnoreFormatting)(result, `Type.Union([Type.Object({}), Type.Null()])`);
});
(0, globals_1.it)("creates union types for nullable arrays", async () => {
const schema = {
type: ["array", "null"],
items: { type: "string" },
};
const result = (0, schema_to_typebox_1.parseWithMultipleTypes)(schema);
await (0, util_1.expectEqualIgnoreFormatting)(result, `Type.Union([Type.Array(Type.String()),Type.Null()])`);
});
});
(0, globals_1.describe)("parseConst() - when parsing a const schema", () => {
(0, globals_1.it)("returns Type.Literal()", () => {
const schema = {
const: "1",
};
const result = (0, schema_to_typebox_1.parseConst)(schema);
(0, globals_1.expect)(result).toContain(`Type.Literal`);
});
(0, globals_1.it)("quotes strings", () => {
const schema = {
const: "1",
};
const result = (0, schema_to_typebox_1.parseConst)(schema);
(0, globals_1.expect)(result).toContain(`"1"`);
});
(0, globals_1.it)("does not quote numbers", () => {
const schema = {
const: 1,
};
const result = (0, schema_to_typebox_1.parseConst)(schema);
(0, globals_1.expect)(result).toContain(`1`);
(0, globals_1.expect)(result).not.toContain(`"1"`);
});
(0, globals_1.it)("creates Type.Union() of Type.Literal()s for each item if const is a list", () => {
const schema = {
const: [1, null],
};
const result = (0, schema_to_typebox_1.parseConst)(schema);
(0, globals_1.expect)(result).toContain(`Type.Union`);
(0, globals_1.expect)(result).toContain(`Type.Literal`);
(0, globals_1.expect)(result).toContain(`1`);
(0, globals_1.expect)(result).toContain(`Type.Null`);
});
});
(0, globals_1.describe)('parseTypeName() - when parsing a type name (e.g. "number", "string", "null" ..)', () => {
(0, globals_1.it)('creates Type.Number for "number"', () => {
const result = (0, schema_to_typebox_1.parseTypeName)("number");
(0, globals_1.expect)(result).toEqual(`Type.Number()`);
});
(0, globals_1.it)('applies schemaOptions for "number"', () => {
const schemaOptions = { description: "test description" };
const result = (0, schema_to_typebox_1.parseTypeName)("number", schemaOptions);
(0, globals_1.expect)(result).toContain(JSON.stringify(schemaOptions));
});
(0, globals_1.it)('creates Type.String for "string"', () => {
const result = (0, schema_to_typebox_1.parseTypeName)("string");
(0, globals_1.expect)(result).toEqual(`Type.String()`);
});
(0, globals_1.it)('applies schemaOptions for "string"', () => {
const schemaOptions = { description: "test description" };
const result = (0, schema_to_typebox_1.parseTypeName)("string", schemaOptions);
(0, globals_1.expect)(result).toContain(JSON.stringify(schemaOptions));
});
(0, globals_1.it)('creates Type.Boolean for "boolean"', () => {
const result = (0, schema_to_typebox_1.parseTypeName)("boolean");
(0, globals_1.expect)(result).toEqual(`Type.Boolean()`);
});
(0, globals_1.it)('applies schemaOptions for "boolean"', () => {
const schemaOptions = { description: "test description" };
const result = (0, schema_to_typebox_1.parseTypeName)("boolean", schemaOptions);
(0, globals_1.expect)(result).toContain(JSON.stringify(schemaOptions));
});
(0, globals_1.it)('creates Type.Null for "null"', () => {
const result = (0, schema_to_typebox_1.parseTypeName)("null");
(0, globals_1.expect)(result).toEqual(`Type.Null()`);
});
(0, globals_1.it)('applies schemaOptions for "null"', () => {
const schemaOptions = { description: "test description" };
const result = (0, schema_to_typebox_1.parseTypeName)("null", schemaOptions);
(0, globals_1.expect)(result).toContain(JSON.stringify(schemaOptions));
});
});
});
//# sourceMappingURL=parser.spec.js.map
;