diff --git a/lib/getTemplate.ts b/lib/getTemplate.ts index d77dbd6..ec4805b 100644 --- a/lib/getTemplate.ts +++ b/lib/getTemplate.ts @@ -187,17 +187,24 @@ function getTemplate( // @todo Array.isArray(schema.type) // -> hasDefault? return // if not -> pick first types - if (!isJsonSchema(schema) || schema.type == null) { return undefined; } + // @attention - very special case to support file instances + if (data instanceof File) { + console.log("is file") + return data; + } + const type = Array.isArray(schema.type) ? selectType(schema.type, data, schema.default) : schema.type; // reset invalid type const javascriptTypeOfData = getTypeOf(data); + + if ( data != null && javascriptTypeOfData !== type && diff --git a/test/unit/getTemplate.test.ts b/test/unit/getTemplate.test.ts index 4f378db..59c1a08 100644 --- a/test/unit/getTemplate.test.ts +++ b/test/unit/getTemplate.test.ts @@ -1280,6 +1280,18 @@ describe("getTemplate", () => { expect({ list: [], author: "jane" }).to.deep.equal(template); }); + describe("file", () => { + it("should not modify file-instance", () => { + const file = new File([], "testfile.pdf"); + draft.setSchema({ + type: ["string", "object"], + format: "file" + }); + const res = getTemplate(draft, file, draft.getSchema()); + expect(res).to.deep.equal(file); + }); + }); + describe("extendDefaults", () => {