Skip to content

Commit

Permalink
add: support file-instances as data-values in getTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Mar 25, 2024
1 parent c8417d7 commit 8e7486c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/getTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
12 changes: 12 additions & 0 deletions test/unit/getTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Expand Down

0 comments on commit 8e7486c

Please sign in to comment.