UNPKG

863 BPlain TextView Raw
1import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types"
2import type {KeywordCxt} from "../../compile/validate"
3import {_} from "../../compile/codegen"
4import {useFunc} from "../../compile/util"
5import equal from "../../runtime/equal"
6
7export type ConstError = ErrorObject<"const", {allowedValue: any}>
8
9const error: KeywordErrorDefinition = {
10 message: "must be equal to constant",
11 params: ({schemaCode}) => _`{allowedValue: ${schemaCode}}`,
12}
13
14const def: CodeKeywordDefinition = {
15 keyword: "const",
16 $data: true,
17 error,
18 code(cxt: KeywordCxt) {
19 const {gen, data, $data, schemaCode, schema} = cxt
20 if ($data || (schema && typeof schema == "object")) {
21 cxt.fail$data(_`!${useFunc(gen, equal)}(${data}, ${schemaCode})`)
22 } else {
23 cxt.fail(_`${schema} !== ${data}`)
24 }
25 },
26}
27
28export default def