UNPKG

1.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const CONSTRUCTORS = {
4 Object,
5 Array,
6 Function,
7 Number,
8 String,
9 Date,
10 RegExp,
11};
12/* istanbul ignore else */
13if (typeof Buffer != "undefined")
14 CONSTRUCTORS.Buffer = Buffer;
15/* istanbul ignore else */
16if (typeof Promise != "undefined")
17 CONSTRUCTORS.Promise = Promise;
18const getDef = Object.assign(_getDef, { CONSTRUCTORS });
19function _getDef() {
20 return {
21 keyword: "instanceof",
22 schemaType: ["string", "array"],
23 compile(schema) {
24 if (typeof schema == "string") {
25 const C = getConstructor(schema);
26 return (data) => data instanceof C;
27 }
28 if (Array.isArray(schema)) {
29 const constructors = schema.map(getConstructor);
30 return (data) => {
31 for (const C of constructors) {
32 if (data instanceof C)
33 return true;
34 }
35 return false;
36 };
37 }
38 /* istanbul ignore next */
39 throw new Error("ajv implementation error");
40 },
41 metaSchema: {
42 anyOf: [{ type: "string" }, { type: "array", items: { type: "string" } }],
43 },
44 };
45}
46function getConstructor(c) {
47 const C = CONSTRUCTORS[c];
48 if (C)
49 return C;
50 throw new Error(`invalid "instanceof" keyword value ${c}`);
51}
52exports.default = getDef;
53module.exports = getDef;
54//# sourceMappingURL=instanceof.js.map
\No newline at end of file