UNPKG

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