Skip to content

Commit

Permalink
Add Schema Enum Import and Export
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcibotari committed Feb 2, 2024
1 parent 5f4a65f commit dac38b6
Show file tree
Hide file tree
Showing 28 changed files with 2,119 additions and 1,660 deletions.
2 changes: 1 addition & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { asset } from './assets';

export { content } from './contents';

export { plugin } from './plugins';
// export { plugin } from './plugins';

export { setup } from './setup';

Expand Down
49 changes: 40 additions & 9 deletions functions/src/models/schema.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@ import { Timestamp } from 'firebase-admin/firestore';
export enum SchemaType {
ROOT = 'ROOT',
NODE = 'NODE',
ENUM = 'ENUM',
}

export interface Schema {
export type Schema = SchemaComponent | SchemaEnum;

export interface SchemaBase {
name: string;
type: SchemaType;
displayName?: string;

createdAt: Timestamp;
updatedAt: Timestamp;
}

export interface SchemaComponent extends SchemaBase {
type: SchemaType.ROOT | SchemaType.NODE;
previewField?: string;
previewImage?: string;
fields?: SchemaField[];
}

// Lock
locked?: boolean;
lockedBy?: string;
export interface SchemaEnum extends SchemaBase {
type: SchemaType.ENUM;
values?: SchemaEnumValue[];
}

createdAt: Timestamp;
updatedAt: Timestamp;
export interface SchemaEnumValue {
name: string;
value: string;
}

export type SchemaField =
Expand Down Expand Up @@ -127,12 +140,14 @@ export interface SchemaFieldOptionSelectable {

export interface SchemaFieldOption extends SchemaFieldBase {
kind: SchemaFieldKind.OPTION;
options: SchemaFieldOptionSelectable[];
source: string | 'self';
options?: SchemaFieldOptionSelectable[];
}

export interface SchemaFieldOptions extends SchemaFieldBase {
kind: SchemaFieldKind.OPTIONS;
options: SchemaFieldOptionSelectable[];
source: string | 'self';
options?: SchemaFieldOptionSelectable[];
minValues?: number;
maxValues?: number;
}
Expand All @@ -153,13 +168,29 @@ export interface SchemaFieldReferences extends SchemaFieldBase {

export interface SchemaFieldAsset extends SchemaFieldBase {
kind: SchemaFieldKind.ASSET;
fileTypes?: AssetFileType[];
}

export interface SchemaFieldAssets extends SchemaFieldBase {
kind: SchemaFieldKind.ASSETS;
fileTypes?: AssetFileType[];
}

export enum AssetFileType {
ANY = 'ANY',
IMAGE = 'IMAGE',
VIDEO = 'VIDEO',
TEXT = 'TEXT',
AUDIO = 'AUDIO',
APPLICATION = 'APPLICATION',
}

// Export and Import
export interface SchemaExport extends Omit<Schema, 'createdAt' | 'updatedAt'> {
export type SchemaExport = SchemaComponentExport | SchemaEnumExport;
export interface SchemaComponentExport extends Omit<SchemaComponent, 'createdAt' | 'updatedAt'> {
id: string;
}

export interface SchemaEnumExport extends Omit<SchemaEnum, 'createdAt' | 'updatedAt'> {
id: string;
}
38 changes: 30 additions & 8 deletions functions/src/models/schema.zod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { z } from 'zod';
import { SchemaFieldKind, SchemaType } from './schema.model';
import { AssetFileType, SchemaFieldKind, SchemaType } from './schema.model';

export const schemaTypeSchema = z.nativeEnum(SchemaType);

export const schemaBaseSchema = z.object({
id: z.string(),
name: z.string(),
type: schemaTypeSchema,
displayName: z.string().optional(),
});

export const schemaEnumValueSchema = z.object({
name: z.string(),
value: z.string(),
});

export const schemaFieldKindSchema = z.nativeEnum(SchemaFieldKind);

export const schemaFieldBaseSchema = z.object({
Expand Down Expand Up @@ -72,12 +84,14 @@ export const schemaFieldOptionSelectableSchema = z.object({

export const schemaFieldOptionSchema = schemaFieldBaseSchema.extend({
kind: z.literal(SchemaFieldKind.OPTION),
options: z.array(schemaFieldOptionSelectableSchema),
source: z.union([z.string(), z.literal('self')]),
options: z.array(schemaFieldOptionSelectableSchema).optional(),
});

export const schemaFieldOptionsSchema = schemaFieldBaseSchema.extend({
kind: z.literal(SchemaFieldKind.OPTIONS),
options: z.array(schemaFieldOptionSelectableSchema),
source: z.union([z.string(), z.literal('self')]),
options: z.array(schemaFieldOptionSelectableSchema).optional(),
minValues: z.number().optional(),
maxValues: z.number().optional(),
});
Expand All @@ -96,12 +110,21 @@ export const schemaFieldReferencesSchema = schemaFieldBaseSchema.extend({
path: z.string().optional(),
});

export const assetFileTypeSchema = z.nativeEnum(AssetFileType);

export const schemaEnumSchema = schemaBaseSchema.extend({
type: z.literal(SchemaType.ENUM),
values: z.array(schemaEnumValueSchema).optional(),
});

export const schemaFieldAssetSchema = schemaFieldBaseSchema.extend({
kind: z.literal(SchemaFieldKind.ASSET),
fileTypes: z.array(assetFileTypeSchema).optional(),
});

export const schemaFieldAssetsSchema = schemaFieldBaseSchema.extend({
kind: z.literal(SchemaFieldKind.ASSETS),
fileTypes: z.array(assetFileTypeSchema).optional(),
});

export const schemaFieldSchema = z.union([
Expand All @@ -124,14 +147,13 @@ export const schemaFieldSchema = z.union([
schemaFieldAssetsSchema,
]);

export const schemaSchema = z.object({
id: z.string(),
name: z.string(),
type: schemaTypeSchema,
displayName: z.string().optional(),
export const schemaComponentSchema = schemaBaseSchema.extend({
type: z.union([z.literal(SchemaType.ROOT), z.literal(SchemaType.NODE)]),
previewField: z.string().optional(),
previewImage: z.string().optional(),
fields: z.array(schemaFieldSchema).optional(),
});

export const schemaSchema = z.union([schemaComponentSchema, schemaEnumSchema]);

export const zSchemaExportArraySchema = z.array(schemaSchema);
Loading

0 comments on commit dac38b6

Please sign in to comment.