| 1 | import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types" |
| 2 | import type {KeywordCxt} from "../../compile/validate" |
| 3 | import {_} from "../../compile/codegen" |
| 4 | import {useFunc} from "../../compile/util" |
| 5 | import equal from "../../runtime/equal" |
| 6 | |
| 7 | export type ConstError = ErrorObject<"const", {allowedValue: any}> |
| 8 | |
| 9 | const error: KeywordErrorDefinition = { |
| 10 | message: "must be equal to constant", |
| 11 | params: ({schemaCode}) => _`{allowedValue: ${schemaCode}}`, |
| 12 | } |
| 13 | |
| 14 | const 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 | |
| 28 | export default def |