UNPKG

746 BPlain TextView Raw
1import type {CodeKeywordDefinition, KeywordCxt} from "ajv"
2import {_} from "ajv/dist/compile/codegen"
3
4const TYPES = ["undefined", "string", "number", "object", "function", "boolean", "symbol"]
5
6export 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
27module.exports = getDef