jtd-schema.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {SchemaObject} from "../types"
  2. type MetaSchema = (root: boolean) => SchemaObject
  3. const shared: MetaSchema = (root) => {
  4. const sch: SchemaObject = {
  5. nullable: {type: "boolean"},
  6. metadata: {
  7. optionalProperties: {
  8. union: {elements: {ref: "schema"}},
  9. },
  10. additionalProperties: true,
  11. },
  12. }
  13. if (root) sch.definitions = {values: {ref: "schema"}}
  14. return sch
  15. }
  16. const emptyForm: MetaSchema = (root) => ({
  17. optionalProperties: shared(root),
  18. })
  19. const refForm: MetaSchema = (root) => ({
  20. properties: {
  21. ref: {type: "string"},
  22. },
  23. optionalProperties: shared(root),
  24. })
  25. const typeForm: MetaSchema = (root) => ({
  26. properties: {
  27. type: {
  28. enum: [
  29. "boolean",
  30. "timestamp",
  31. "string",
  32. "float32",
  33. "float64",
  34. "int8",
  35. "uint8",
  36. "int16",
  37. "uint16",
  38. "int32",
  39. "uint32",
  40. ],
  41. },
  42. },
  43. optionalProperties: shared(root),
  44. })
  45. const enumForm: MetaSchema = (root) => ({
  46. properties: {
  47. enum: {elements: {type: "string"}},
  48. },
  49. optionalProperties: shared(root),
  50. })
  51. const elementsForm: MetaSchema = (root) => ({
  52. properties: {
  53. elements: {ref: "schema"},
  54. },
  55. optionalProperties: shared(root),
  56. })
  57. const propertiesForm: MetaSchema = (root) => ({
  58. properties: {
  59. properties: {values: {ref: "schema"}},
  60. },
  61. optionalProperties: {
  62. optionalProperties: {values: {ref: "schema"}},
  63. additionalProperties: {type: "boolean"},
  64. ...shared(root),
  65. },
  66. })
  67. const optionalPropertiesForm: MetaSchema = (root) => ({
  68. properties: {
  69. optionalProperties: {values: {ref: "schema"}},
  70. },
  71. optionalProperties: {
  72. additionalProperties: {type: "boolean"},
  73. ...shared(root),
  74. },
  75. })
  76. const discriminatorForm: MetaSchema = (root) => ({
  77. properties: {
  78. discriminator: {type: "string"},
  79. mapping: {
  80. values: {
  81. metadata: {
  82. union: [propertiesForm(false), optionalPropertiesForm(false)],
  83. },
  84. },
  85. },
  86. },
  87. optionalProperties: shared(root),
  88. })
  89. const valuesForm: MetaSchema = (root) => ({
  90. properties: {
  91. values: {ref: "schema"},
  92. },
  93. optionalProperties: shared(root),
  94. })
  95. const schema: MetaSchema = (root) => ({
  96. metadata: {
  97. union: [
  98. emptyForm,
  99. refForm,
  100. typeForm,
  101. enumForm,
  102. elementsForm,
  103. propertiesForm,
  104. optionalPropertiesForm,
  105. discriminatorForm,
  106. valuesForm,
  107. ].map((s) => s(root)),
  108. },
  109. })
  110. const jtdMetaSchema: SchemaObject = {
  111. definitions: {
  112. schema: schema(false),
  113. },
  114. ...schema(true),
  115. }
  116. export default jtdMetaSchema