啊鑫
2025-07-17 eaa506e57403d1b8502f16ca5dd6e82c347724d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type {KeywordErrorDefinition, KeywordErrorCxt, ErrorObject} from "../../types"
import {_, Code} from "../../compile/codegen"
 
export type _JTDTypeError<K extends string, T extends string, S> = ErrorObject<
  K,
  {type: T; nullable: boolean},
  S
>
 
export function typeError(t: string): KeywordErrorDefinition {
  return {
    message: (cxt) => typeErrorMessage(cxt, t),
    params: (cxt) => typeErrorParams(cxt, t),
  }
}
 
export function typeErrorMessage({parentSchema}: KeywordErrorCxt, t: string): string {
  return parentSchema?.nullable ? `must be ${t} or null` : `must be ${t}`
}
 
export function typeErrorParams({parentSchema}: KeywordErrorCxt, t: string): Code {
  return _`{type: ${t}, nullable: ${!!parentSchema?.nullable}}`
}