1 import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {_, nil, or, Code} from "../../compile/codegen"
4 import validTimestamp from "../../runtime/timestamp"
5 import {useFunc} from "../../compile/util"
6 import {checkMetadata} from "./metadata"
7 import {typeErrorMessage, typeErrorParams, _JTDTypeError} from "./error"
9 export type JTDTypeError = _JTDTypeError<"type", JTDType, JTDType>
11 export type IntType = "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32"
13 export const intRange: {[T in IntType]: [number, number, number]} = {
16 int16: [-32768, 32767, 5],
17 uint16: [0, 65535, 5],
18 int32: [-2147483648, 2147483647, 10],
19 uint32: [0, 4294967295, 10],
22 export type JTDType = "boolean" | "string" | "timestamp" | "float32" | "float64" | IntType
24 const error: KeywordErrorDefinition = {
25 message: (cxt) => typeErrorMessage(cxt, cxt.schema),
26 params: (cxt) => typeErrorParams(cxt, cxt.schema),
29 function timestampCode(cxt: KeywordCxt): Code {
30 const {gen, data, it} = cxt
31 const {timestamp, allowDate} = it.opts
32 if (timestamp === "date") return _`${data} instanceof Date `
33 const vts = useFunc(gen, validTimestamp)
34 const allowDateArg = allowDate ? _`, true` : nil
35 const validString = _`typeof ${data} == "string" && ${vts}(${data}${allowDateArg})`
36 return timestamp === "string" ? validString : or(_`${data} instanceof Date`, validString)
39 const def: CodeKeywordDefinition = {
43 code(cxt: KeywordCxt) {
45 const {data, schema, parentSchema, it} = cxt
50 cond = _`typeof ${data} == ${schema}`
53 cond = timestampCode(cxt)
58 cond = _`typeof ${data} == "number"`
61 const sch = schema as IntType
62 cond = _`typeof ${data} == "number" && isFinite(${data}) && !(${data} % 1)`
63 if (!it.opts.int32range && (sch === "int32" || sch === "uint32")) {
64 if (sch === "uint32") cond = _`${cond} && ${data} >= 0`
66 const [min, max] = intRange[sch]
67 cond = _`${cond} && ${data} >= ${min} && ${data} <= ${max}`
71 cxt.pass(parentSchema.nullable ? or(_`${data} === null`, cond) : cond)