Skip to content

Commit 3622d4c

Browse files
committed
fix: File-support for node versions < 20
1 parent 6097122 commit 3622d4c

File tree

8 files changed

+34
-5
lines changed

8 files changed

+34
-5
lines changed

dist/jsonSchemaLibrary.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module/src/draft2019-09/methods/getData.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isObject } from "../../utils/isObject";
88
import { isSchemaNode } from "../../types";
99
import { mergeNode } from "../../mergeNode";
1010
import { reduceOneOfFuzzy } from "../../keywords/oneOf";
11+
import { isFile } from "../../utils/isFile";
1112
function safeResolveRef(node, options) {
1213
var _a, _b;
1314
if (node.$ref == null) {
@@ -65,7 +66,7 @@ export function getData(node, data, opts) {
6566
return data;
6667
}
6768
// @attention - very special case to support file instances
68-
if (data instanceof File) {
69+
if (isFile(data)) {
6970
return data;
7071
}
7172
if (((_a = node.schema) === null || _a === void 0 ? void 0 : _a.const) !== undefined) {

dist/module/src/methods/getData.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isObject } from "../utils/isObject";
88
import { isSchemaNode } from "../types";
99
import { mergeNode } from "../mergeNode";
1010
import { reduceOneOfFuzzy } from "../keywords/oneOf";
11+
import { isFile } from "../utils/isFile";
1112
function safeResolveRef(node, options) {
1213
var _a, _b;
1314
if (node.$ref == null) {
@@ -65,7 +66,7 @@ export function getData(node, data, opts) {
6566
return data;
6667
}
6768
// @attention - very special case to support file instances
68-
if (data instanceof File) {
69+
if (isFile(data)) {
6970
return data;
7071
}
7172
if (((_a = node.schema) === null || _a === void 0 ? void 0 : _a.const) !== undefined) {

dist/module/src/utils/isFile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let isFile;
2+
// @ts-expect-error forced types
3+
isFile = () => false;
4+
try {
5+
if (typeof File === "function") {
6+
isFile = (value) => value instanceof File;
7+
}
8+
}
9+
catch (e) { } // eslint-disable-line
10+
export { isFile };

dist/src/utils/isFile.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare let isFile: (value: unknown) => value is File;
2+
export { isFile };

src/draft2019-09/methods/getData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isObject } from "../../utils/isObject";
88
import { isSchemaNode, SchemaNode } from "../../types";
99
import { mergeNode } from "../../mergeNode";
1010
import { reduceOneOfFuzzy } from "../../keywords/oneOf";
11+
import { isFile } from "../../utils/isFile";
1112

1213
export type TemplateOptions = {
1314
/** Add all properties (required and optional) to the generated data */
@@ -92,7 +93,7 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
9293
return data;
9394
}
9495
// @attention - very special case to support file instances
95-
if (data instanceof File) {
96+
if (isFile(data)) {
9697
return data;
9798
}
9899

src/methods/getData.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { isObject } from "../utils/isObject";
88
import { isSchemaNode, SchemaNode } from "../types";
99
import { mergeNode } from "../mergeNode";
1010
import { reduceOneOfFuzzy } from "../keywords/oneOf";
11+
import { isFile } from "../utils/isFile";
12+
13+
1114

1215
export type TemplateOptions = {
1316
/** Add all properties (required and optional) to the generated data */
@@ -92,7 +95,7 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
9295
return data;
9396
}
9497
// @attention - very special case to support file instances
95-
if (data instanceof File) {
98+
if (isFile(data)) {
9699
return data;
97100
}
98101

src/utils/isFile.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let isFile: (value: unknown) => value is File;
2+
// @ts-expect-error forced types
3+
isFile = () => false;
4+
5+
try {
6+
if (typeof File === "function") {
7+
isFile = (value: unknown) => value instanceof File;
8+
}
9+
} catch (e) {} // eslint-disable-line
10+
11+
export { isFile };

0 commit comments

Comments
 (0)