Urara-Blog/node_modules/.pnpm-store/v3/files/3f/a9ed09d2508784e34fdb3349238cc755f2f5abffd9d83ce2e564bc94971b0059c35c9c48f8e8bf047fec6d92d7fffdf2d58c12bdd82133a6da35c60a5bd364
2022-08-14 01:14:53 +08:00

23 lines
746 B
Text

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}}`
}