massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / node_modules / ajv / lib / vocabularies / jtd / type.ts
index d284281991053a2aec919ea123c7db23ded2a50e..17274300b70b11b72977a965a4410f21a8f0f19c 100644 (file)
@@ -1,6 +1,6 @@
 import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
 import type {KeywordCxt} from "../../compile/validate"
-import {_, or, Code} from "../../compile/codegen"
+import {_, nil, or, Code} from "../../compile/codegen"
 import validTimestamp from "../../runtime/timestamp"
 import {useFunc} from "../../compile/util"
 import {checkMetadata} from "./metadata"
@@ -26,13 +26,23 @@ const error: KeywordErrorDefinition = {
   params: (cxt) => typeErrorParams(cxt, cxt.schema),
 }
 
+function timestampCode(cxt: KeywordCxt): Code {
+  const {gen, data, it} = cxt
+  const {timestamp, allowDate} = it.opts
+  if (timestamp === "date") return _`${data} instanceof Date `
+  const vts = useFunc(gen, validTimestamp)
+  const allowDateArg = allowDate ? _`, true` : nil
+  const validString = _`typeof ${data} == "string" && ${vts}(${data}${allowDateArg})`
+  return timestamp === "string" ? validString : or(_`${data} instanceof Date`, validString)
+}
+
 const def: CodeKeywordDefinition = {
   keyword: "type",
   schemaType: "string",
   error,
   code(cxt: KeywordCxt) {
     checkMetadata(cxt)
-    const {gen, data, schema, parentSchema} = cxt
+    const {data, schema, parentSchema, it} = cxt
     let cond: Code
     switch (schema) {
       case "boolean":
@@ -40,8 +50,7 @@ const def: CodeKeywordDefinition = {
         cond = _`typeof ${data} == ${schema}`
         break
       case "timestamp": {
-        const vts = useFunc(gen, validTimestamp)
-        cond = _`${data} instanceof Date || (typeof ${data} == "string" && ${vts}(${data}))`
+        cond = timestampCode(cxt)
         break
       }
       case "float32":
@@ -49,8 +58,14 @@ const def: CodeKeywordDefinition = {
         cond = _`typeof ${data} == "number"`
         break
       default: {
-        const [min, max] = intRange[schema as IntType]
-        cond = _`typeof ${data} == "number" && isFinite(${data}) && ${data} >= ${min} && ${data} <= ${max} && !(${data} % 1)`
+        const sch = schema as IntType
+        cond = _`typeof ${data} == "number" && isFinite(${data}) && !(${data} % 1)`
+        if (!it.opts.int32range && (sch === "int32" || sch === "uint32")) {
+          if (sch === "uint32") cond = _`${cond} && ${data} >= 0`
+        } else {
+          const [min, max] = intRange[sch]
+          cond = _`${cond} && ${data} >= ${min} && ${data} <= ${max}`
+        }
       }
     }
     cxt.pass(parentSchema.nullable ? or(_`${data} === null`, cond) : cond)