Skip to content

Commit 896bdf4

Browse files
committed
migrate to ZOD from AVJ
1 parent 533f6fe commit 896bdf4

16 files changed

+445
-484
lines changed

functions/package-lock.json

Lines changed: 164 additions & 181 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@
1919
},
2020
"main": "lib/index.js",
2121
"dependencies": {
22-
"@google-cloud/translate": "^8.0.2",
23-
"ajv": "^8.12.0",
22+
"@google-cloud/translate": "^8.0.3",
2423
"compressing": "^1.10.0",
2524
"cors": "^2.8.5",
2625
"express": "^4.18.2",
2726
"firebase-admin": "^11.11.1",
2827
"firebase-functions": "^4.6.0",
29-
"sharp": "^0.33.1",
30-
"stripe": "^14.10.0",
31-
"uuid": "^9.0.1"
28+
"sharp": "^0.33.2",
29+
"stripe": "^14.13.0",
30+
"uuid": "^9.0.1",
31+
"zod": "^3.22.4"
3232
},
3333
"devDependencies": {
3434
"@types/express": "^4.17.21",
3535
"@types/uuid": "^9.0.7",
36-
"@typescript-eslint/eslint-plugin": "^6.17.0",
37-
"@typescript-eslint/parser": "^6.17.0",
36+
"@typescript-eslint/eslint-plugin": "^6.19.1",
37+
"@typescript-eslint/parser": "^6.19.1",
3838
"eslint": "^8.56.0",
3939
"eslint-config-google": "^0.14.0",
4040
"eslint-plugin-import": "^2.29.1",
41-
"firebase-functions-test": "^3.1.0",
41+
"firebase-functions-test": "^3.1.1",
4242
"typescript": "^5.2.2"
4343
},
4444
"private": true

functions/src/models/asset.model.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Timestamp } from 'firebase-admin/firestore';
2-
import { JSONSchemaType } from 'ajv';
32

43
export type Asset = AssetFile | AssetFolder;
54

@@ -47,47 +46,3 @@ export interface AssetFileExport extends Omit<AssetFile, 'createdAt' | 'updatedA
4746
}
4847

4948
export type AssetExport = AssetFileExport | AssetFolderExport;
50-
51-
export const assetExportArraySchema: JSONSchemaType<AssetExport[]> = {
52-
type: 'array',
53-
items: {
54-
type: 'object',
55-
discriminator: { propertyName: 'kind' },
56-
required: ['kind', 'id', 'name', 'parentPath'],
57-
oneOf: [
58-
{
59-
properties: {
60-
id: { type: 'string', nullable: false },
61-
kind: { const: 'FOLDER' },
62-
name: { type: 'string', nullable: false },
63-
parentPath: { type: 'string', nullable: false },
64-
},
65-
// required: ['id', 'name', 'parentPath'],
66-
additionalProperties: false,
67-
},
68-
{
69-
properties: {
70-
id: { type: 'string', nullable: false },
71-
kind: { const: 'FILE' },
72-
name: { type: 'string', nullable: false },
73-
parentPath: { type: 'string', nullable: false },
74-
extension: { type: 'string', nullable: false },
75-
type: { type: 'string', nullable: false },
76-
size: { type: 'integer', nullable: false },
77-
alt: { type: 'string' },
78-
metadata: {
79-
type: 'object',
80-
properties: {
81-
format: { type: 'string' },
82-
width: { type: 'integer' },
83-
height: { type: 'integer' },
84-
},
85-
additionalProperties: false,
86-
},
87-
// required: ['id', 'name', 'parentPath'],
88-
},
89-
additionalProperties: false,
90-
},
91-
],
92-
},
93-
};

functions/src/models/asset.zod.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { z } from 'zod';
2+
import { AssetKind } from './asset.model';
3+
4+
export const assetBaseSchema = z.object({
5+
id: z.string(),
6+
kind: z.nativeEnum(AssetKind),
7+
name: z.string(),
8+
parentPath: z.string(),
9+
});
10+
11+
export const assetFolderSchema = assetBaseSchema.extend({
12+
kind: z.literal(AssetKind.FOLDER),
13+
});
14+
15+
export const assetMetadataSchema = z.object({
16+
format: z.string().optional(),
17+
width: z.number().optional(),
18+
height: z.number().optional(),
19+
});
20+
21+
export const assetFileSchema = assetBaseSchema.extend({
22+
kind: z.literal(AssetKind.FILE),
23+
extension: z.string(),
24+
type: z.string(),
25+
size: z.number(),
26+
alt: z.string().optional(),
27+
metadata: assetMetadataSchema.optional(),
28+
});
29+
30+
export const assetSchema = z.union([assetFileSchema, assetFolderSchema]);
31+
32+
export const zAssetExportArraySchema = z.array(assetSchema);

functions/src/models/content.model.ts

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Timestamp } from 'firebase-admin/firestore';
2-
import { JSONSchemaType } from 'ajv';
32

43
export type Content = ContentDocument | ContentFolder;
54

@@ -95,50 +94,8 @@ export interface ContentFolderExport extends Omit<ContentFolder, 'createdAt' | '
9594
id: string;
9695
}
9796

98-
export interface ContentDocumentExport extends Omit<ContentDocument, 'createdAt' | 'updatedAt'> {
97+
export interface ContentDocumentExport extends Omit<ContentDocument, 'createdAt' | 'updatedAt' | 'publishedAt' | 'editorEnabled'> {
9998
id: string;
10099
}
101100

102101
export type ContentExport = ContentDocumentExport | ContentFolderExport;
103-
104-
export const contentExportArraySchema: JSONSchemaType<ContentExport[]> = {
105-
type: 'array',
106-
items: {
107-
type: 'object',
108-
discriminator: { propertyName: 'kind' },
109-
required: ['kind', 'id', 'name', 'slug', 'parentSlug', 'fullSlug'],
110-
oneOf: [
111-
{
112-
properties: {
113-
id: { type: 'string', nullable: false },
114-
kind: { const: 'FOLDER' },
115-
name: { type: 'string', nullable: false },
116-
slug: { type: 'string', nullable: false },
117-
parentSlug: { type: 'string', nullable: false },
118-
fullSlug: { type: 'string', nullable: false },
119-
},
120-
additionalProperties: false,
121-
},
122-
{
123-
properties: {
124-
id: { type: 'string', nullable: false },
125-
kind: { const: 'DOCUMENT' },
126-
name: { type: 'string', nullable: false },
127-
slug: { type: 'string', nullable: false },
128-
parentSlug: { type: 'string', nullable: false },
129-
fullSlug: { type: 'string', nullable: false },
130-
schema: { type: 'string', nullable: false },
131-
editorEnabled: { type: 'boolean', nullable: true },
132-
data: {
133-
type: 'object',
134-
properties: {
135-
_id: { type: 'string' },
136-
schema: { type: 'string' },
137-
},
138-
},
139-
},
140-
additionalProperties: false,
141-
},
142-
],
143-
},
144-
};

functions/src/models/content.zod.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { z } from 'zod';
2+
import { ContentKind } from './content.model';
3+
4+
export const contentDataSchema = z.object({
5+
_id: z.string(),
6+
schema: z.string(),
7+
});
8+
9+
export const contentBaseSchema = z.object({
10+
id: z.string(),
11+
kind: z.nativeEnum(ContentKind),
12+
name: z.string(),
13+
slug: z.string(),
14+
parentSlug: z.string(),
15+
fullSlug: z.string(),
16+
});
17+
18+
export const contentDocumentSchema = contentBaseSchema.extend({
19+
kind: z.literal(ContentKind.DOCUMENT),
20+
schema: z.string(),
21+
data: contentDataSchema.optional(),
22+
});
23+
24+
export const contentFolderSchema = contentBaseSchema.extend({
25+
kind: z.literal(ContentKind.FOLDER),
26+
});
27+
28+
export const contentSchema = z.union([contentDocumentSchema, contentFolderSchema]);
29+
30+
export const zContentExportArraySchema = z.array(contentSchema);

functions/src/models/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
export * from './asset.model';
2+
export * from './asset.zod';
23
export * from './content.model';
4+
export * from './content.zod';
35
export * from './content-history.model';
46
export * from './firebase.model';
57
export * from './locale.model';
68
export * from './plugin.model';
79
export * from './schema.model';
10+
export * from './schema.zod';
811
export * from './space.model';
912
export * from './task.model';
1013
export * from './token.model';
1114
export * from './translate.model';
1215
export * from './translation.model';
16+
export * from './translation.zod';
1317
export * from './translation-history.model';
1418
export * from './user.model';

functions/src/models/schema.model.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Timestamp } from 'firebase-admin/firestore';
2-
import { JSONSchemaType } from 'ajv';
32

43
export enum SchemaType {
54
ROOT = 'ROOT',
@@ -37,6 +36,7 @@ export type SchemaField =
3736
| SchemaFieldOptions
3837
| SchemaFieldLink
3938
| SchemaFieldReference
39+
| SchemaFieldReferences
4040
| SchemaFieldAsset
4141
| SchemaFieldAssets;
4242

@@ -53,6 +53,7 @@ export enum SchemaFieldKind {
5353
OPTIONS = 'OPTIONS',
5454
LINK = 'LINK',
5555
REFERENCE = 'REFERENCE',
56+
REFERENCES = 'REFERENCES',
5657
ASSET = 'ASSET',
5758
ASSETS = 'ASSETS',
5859
SCHEMA = 'SCHEMA',
@@ -145,6 +146,11 @@ export interface SchemaFieldReference extends SchemaFieldBase {
145146
path?: string;
146147
}
147148

149+
export interface SchemaFieldReferences extends SchemaFieldBase {
150+
kind: SchemaFieldKind.REFERENCES;
151+
path?: string;
152+
}
153+
148154
export interface SchemaFieldAsset extends SchemaFieldBase {
149155
kind: SchemaFieldKind.ASSET;
150156
}
@@ -157,40 +163,3 @@ export interface SchemaFieldAssets extends SchemaFieldBase {
157163
export interface SchemaExport extends Omit<Schema, 'createdAt' | 'updatedAt'> {
158164
id: string;
159165
}
160-
161-
export const schemaExportArraySchema: JSONSchemaType<SchemaExport[]> = {
162-
type: 'array',
163-
items: {
164-
type: 'object',
165-
properties: {
166-
id: { type: 'string', nullable: false },
167-
name: { type: 'string', nullable: false },
168-
type: { type: 'string', enum: Object.values(SchemaType), nullable: false },
169-
displayName: { type: 'string', nullable: true },
170-
previewField: { type: 'string', nullable: true },
171-
previewImage: { type: 'string', nullable: true },
172-
locked: { type: 'boolean', nullable: true },
173-
lockedBy: { type: 'string', nullable: true },
174-
fields: {
175-
type: 'array',
176-
nullable: true,
177-
items: {
178-
type: 'object',
179-
properties: {
180-
name: { type: 'string', nullable: false },
181-
kind: { type: 'string', /*enum: Object.values(SchemaFieldKind),*/ nullable: false },
182-
displayName: { type: 'string', nullable: true },
183-
required: { type: 'boolean', nullable: true },
184-
description: { type: 'string', nullable: true },
185-
defaultValue: { type: 'string', nullable: true },
186-
translatable: { type: 'boolean', nullable: true },
187-
},
188-
required: ['name', 'kind'],
189-
additionalProperties: true,
190-
},
191-
},
192-
},
193-
required: ['id', 'name', 'type'],
194-
additionalProperties: false,
195-
},
196-
};

0 commit comments

Comments
 (0)