discriminator.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
  2. import type {KeywordCxt} from "../../compile/validate"
  3. import {_, not, getProperty, Name} from "../../compile/codegen"
  4. import {checkMetadata} from "./metadata"
  5. import {checkNullableObject} from "./nullable"
  6. import {typeErrorMessage, typeErrorParams, _JTDTypeError} from "./error"
  7. import {DiscrError, DiscrErrorObj} from "../discriminator/types"
  8. export type JTDDiscriminatorError =
  9. | _JTDTypeError<"discriminator", "object", string>
  10. | DiscrErrorObj<DiscrError.Tag>
  11. | DiscrErrorObj<DiscrError.Mapping>
  12. const error: KeywordErrorDefinition = {
  13. message: (cxt) => {
  14. const {schema, params} = cxt
  15. return params.discrError
  16. ? params.discrError === DiscrError.Tag
  17. ? `tag "${schema}" must be string`
  18. : `value of tag "${schema}" must be in mapping`
  19. : typeErrorMessage(cxt, "object")
  20. },
  21. params: (cxt) => {
  22. const {schema, params} = cxt
  23. return params.discrError
  24. ? _`{error: ${params.discrError}, tag: ${schema}, tagValue: ${params.tag}}`
  25. : typeErrorParams(cxt, "object")
  26. },
  27. }
  28. const def: CodeKeywordDefinition = {
  29. keyword: "discriminator",
  30. schemaType: "string",
  31. implements: ["mapping"],
  32. error,
  33. code(cxt: KeywordCxt) {
  34. checkMetadata(cxt)
  35. const {gen, data, schema, parentSchema} = cxt
  36. const [valid, cond] = checkNullableObject(cxt, data)
  37. gen.if(cond)
  38. validateDiscriminator()
  39. gen.elseIf(not(valid))
  40. cxt.error()
  41. gen.endIf()
  42. cxt.ok(valid)
  43. function validateDiscriminator(): void {
  44. const tag = gen.const("tag", _`${data}${getProperty(schema)}`)
  45. gen.if(_`${tag} === undefined`)
  46. cxt.error(false, {discrError: DiscrError.Tag, tag})
  47. gen.elseIf(_`typeof ${tag} == "string"`)
  48. validateMapping(tag)
  49. gen.else()
  50. cxt.error(false, {discrError: DiscrError.Tag, tag}, {instancePath: schema})
  51. gen.endIf()
  52. }
  53. function validateMapping(tag: Name): void {
  54. gen.if(false)
  55. for (const tagValue in parentSchema.mapping) {
  56. gen.elseIf(_`${tag} === ${tagValue}`)
  57. gen.assign(valid, applyTagSchema(tagValue))
  58. }
  59. gen.else()
  60. cxt.error(
  61. false,
  62. {discrError: DiscrError.Mapping, tag},
  63. {instancePath: schema, schemaPath: "mapping", parentSchema: true}
  64. )
  65. gen.endIf()
  66. }
  67. function applyTagSchema(schemaProp: string): Name {
  68. const _valid = gen.name("valid")
  69. cxt.subschema(
  70. {
  71. keyword: "mapping",
  72. schemaProp,
  73. jtdDiscriminator: schema,
  74. },
  75. _valid
  76. )
  77. return _valid
  78. }
  79. },
  80. }
  81. export default def