1 | import type {CodeKeywordDefinition, KeywordCxt} from "ajv"
|
2 | import {_} from "ajv/dist/compile/codegen"
|
3 |
|
4 | const TYPES = ["undefined", "string", "number", "object", "function", "boolean", "symbol"]
|
5 |
|
6 | export default function getDef(): CodeKeywordDefinition {
|
7 | return {
|
8 | keyword: "typeof",
|
9 | schemaType: ["string", "array"],
|
10 | code(cxt: KeywordCxt) {
|
11 | const {data, schema, schemaValue} = cxt
|
12 | cxt.fail(
|
13 | typeof schema == "string"
|
14 | ? _`typeof ${data} != ${schema}`
|
15 | : _`${schemaValue}.indexOf(typeof ${data}) < 0`
|
16 | )
|
17 | },
|
18 | metaSchema: {
|
19 | anyOf: [
|
20 | {type: "string", enum: TYPES},
|
21 | {type: "array", items: {type: "string", enum: TYPES}},
|
22 | ],
|
23 | },
|
24 | }
|
25 | }
|
26 |
|
27 | module.exports = getDef
|