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