Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import { type JSONSchemaV4 } from '../../utils/json-schema-types.js';
2import { type ItemTypeSchemaVariant } from '../moderationConfigService/index.js';
3import { type RawItemData } from './toNormalizedItemDataOrErrors.js';
4
5const rawItemSchemaVariants = ['original', 'partial'] as const;
6
7export type RawItemTypeSelector = {
8 id: string;
9 version?: string;
10 // We don't use ItemTypeSchemaVariant here because we want to keep the raw and
11 // normalized item schema-variant values decoupled.
12 schemaVariant?: (typeof rawItemSchemaVariants)[number];
13};
14
15export type RawItemSubmission =
16 | {
17 id: string;
18 data: RawItemData;
19 type: RawItemTypeSelector;
20 }
21 | {
22 id: string;
23 data: RawItemData;
24 typeId: string;
25 typeVersion?: string;
26 typeSchemaVariant?: ItemTypeSchemaVariant;
27 };
28
29export const rawItemTypeSelectorSchema = {
30 type: 'object',
31 properties: {
32 id: { type: ['string'] },
33 version: { type: ['string'] },
34 schemaVariant: {
35 type: ['string'],
36 enum: rawItemSchemaVariants,
37 },
38 },
39 required: ['id'],
40} as const satisfies JSONSchemaV4<RawItemTypeSelector>;
41
42export const rawItemSubmissionSchema = {
43 oneOf: [
44 {
45 type: 'object',
46 properties: {
47 id: { type: 'string' },
48 // NB: the typings break here if we don't have { required: [] },
49 // but actually putting an empty array for `required` in the runtime
50 // value breaks request handling, so we just use a cast.
51 data: { type: 'object' } as unknown as { type: 'object'; required: [] },
52 typeId: { type: 'string' },
53 typeVersion: { type: 'string' },
54 typeSchemaVariant: { type: 'string', enum: rawItemSchemaVariants },
55 },
56 required: ['id', 'data', 'typeId'],
57 },
58 {
59 type: 'object',
60 properties: {
61 id: { type: 'string' },
62 // NB: the typings break here if we don't have { required: [] },
63 // but actually putting an empty array for `required` in the runtime
64 // value breaks request handling, so we just use a cast.
65 data: { type: 'object' } as unknown as { type: 'object'; required: [] },
66 type: rawItemTypeSelectorSchema,
67 },
68 required: ['id', 'data', 'type'],
69 },
70 ],
71} as const satisfies JSONSchemaV4<RawItemSubmission>;