Skip to content

Commit

Permalink
Merge pull request #8827 from hicommonwealth/rotorsoft/zod-schemas-cl…
Browse files Browse the repository at this point in the history
…eanup

Schemas cleanup following model ADR #3
  • Loading branch information
Rotorsoft authored Aug 13, 2024
2 parents 6088bf5 + e0b35f1 commit 1bbcaee
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 48 deletions.
5 changes: 2 additions & 3 deletions libs/core/src/integration/events.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Reaction,
SubscriptionPreference,
Thread,
zDate,
} from '@hicommonwealth/schemas';
import { z } from 'zod';
import {
Expand Down Expand Up @@ -203,8 +202,8 @@ const ContestManagerEvent = EventMetadata.extend({
});

export const ContestStarted = ContestManagerEvent.extend({
start_time: zDate.describe('Contest start time'),
end_time: zDate.describe('Contest end time'),
start_time: z.coerce.date().describe('Contest start time'),
end_time: z.coerce.date().describe('Contest end time'),
contest_id: z.number().int().gte(1).describe('Recurring contest id'),
}).describe('When a contest instance gets started');

Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/integration/util.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';

// All events should carry this common metadata
export const EventMetadata = z.object({
created_at: z.coerce.date().nullish().describe('When the event was emitted'),
created_at: z.coerce.date().optional().describe('When the event was emitted'),
// TODO: TBD
// aggregateType: z.enum(Aggregates).describe("Event emitter aggregate type")
// aggregateId: z.string().describe("Event emitter aggregate id")
Expand Down
7 changes: 1 addition & 6 deletions libs/schemas/src/commands/community.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ export const CreateCommunity = {
// hidden optional params
user_address: z.string().optional(), // address for the user
alt_wallet_url: z.string().url().optional(),
eth_chain_id: z.coerce
.number()
.int()
.min(MIN_SCHEMA_INT)
.max(MAX_SCHEMA_INT)
.optional(),
eth_chain_id: PG_INT.optional(),
cosmos_chain_id: z.string().optional(),
address: z.string().optional(), // address for the contract of the chain
decimals: PG_INT.optional(),
Expand Down
12 changes: 6 additions & 6 deletions libs/schemas/src/entities/comment.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { z } from 'zod';
import { PG_INT, zDate } from '../utils';
import { PG_INT } from '../utils';
import { Thread } from './thread.schemas';
import { Address } from './user.schemas';

export const Comment = z.object({
id: PG_INT.optional(),
thread_id: PG_INT,
address_id: PG_INT,
text: z.string(),
plaintext: z.string(),
id: PG_INT.nullish(),
parent_id: z.string().nullish(),
version_history: z.array(z.string()).optional(),
version_history_updated: z.boolean().optional(),
Expand All @@ -17,10 +17,10 @@ export const Comment = z.object({
canvas_hash: z.string().nullish(),

created_by: z.string().nullish(),
created_at: zDate.nullish(),
updated_at: zDate.nullish(),
deleted_at: zDate.nullish(),
marked_as_spam_at: zDate.nullish(),
created_at: z.coerce.date().optional(),
updated_at: z.coerce.date().optional(),
deleted_at: z.coerce.date().nullish(),
marked_as_spam_at: z.coerce.date().nullish(),

discord_meta: z
.object({
Expand Down
2 changes: 1 addition & 1 deletion libs/schemas/src/entities/group-permission.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum PermissionEnum {
export type GroupPermissionAction = keyof typeof PermissionEnum;

export const GroupPermission = z.object({
group_id: PG_INT.nullish(),
group_id: PG_INT.optional(),
allowed_actions: z.array(z.nativeEnum(PermissionEnum)),

created_at: z.coerce.date().optional(),
Expand Down
10 changes: 5 additions & 5 deletions libs/schemas/src/entities/notification.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NotificationCategories } from '@hicommonwealth/shared';
import { z } from 'zod';
import { PG_INT, zDate } from '../utils';
import { PG_INT } from '../utils';
import { Comment } from './comment.schemas';
import { Community } from './community.schemas';
import { Thread } from './thread.schemas';
Expand Down Expand Up @@ -38,8 +38,8 @@ export const SubscriptionPreference = z.object({
mobile_push_notifications_enabled: z.boolean().default(false),
mobile_push_discussion_activity_enabled: z.boolean().default(false),
mobile_push_admin_alerts_enabled: z.boolean().default(false),
created_at: z.coerce.date().default(new Date()),
updated_at: z.coerce.date().default(new Date()),
created_at: z.coerce.date().optional(),
updated_at: z.coerce.date().optional(),
});

export const ThreadSubscription = z.object({
Expand Down Expand Up @@ -119,8 +119,8 @@ export const CommunityAlert = z
.object({
user_id: PG_INT,
community_id: z.string(),
created_at: zDate.optional(),
updated_at: zDate.optional(),
created_at: z.coerce.date().optional(),
updated_at: z.coerce.date().optional(),
})
.merge(
z.object({
Expand Down
8 changes: 4 additions & 4 deletions libs/schemas/src/entities/reaction.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from 'zod';
import { PG_INT, zDate } from '../utils';
import { PG_INT } from '../utils';

// TODO: use this as single source of truth for model?
export const Reaction = z.object({
id: PG_INT.nullish(),
id: PG_INT.optional(),
address_id: PG_INT,
reaction: z.enum(['like']),
thread_id: PG_INT.nullish(),
Expand All @@ -12,6 +12,6 @@ export const Reaction = z.object({
calculated_voting_weight: PG_INT.nullish(),
canvas_signed_data: z.any().nullish(),
canvas_hash: z.string().max(255).nullish(),
created_at: zDate.nullish(),
updated_at: zDate.nullish(),
created_at: z.coerce.date().optional(),
updated_at: z.coerce.date().optional(),
});
20 changes: 10 additions & 10 deletions libs/schemas/src/entities/thread.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { discordMetaSchema, linksSchema, PG_INT, zDate } from '../utils';
import { discordMetaSchema, linksSchema, PG_INT } from '../utils';
import { Address } from './user.schemas';

export const Thread = z.object({
Expand All @@ -26,22 +26,22 @@ export const Thread = z.object({
canvas_signed_data: z.string().nullish(),
canvas_hash: z.string().nullish(),

created_at: zDate.nullish(),
updated_at: zDate.nullish(),
last_edited: zDate.nullish(),
deleted_at: zDate.nullish(),
last_commented_on: zDate.nullish(),
marked_as_spam_at: zDate.nullish(),
archived_at: zDate.nullish(),
locked_at: zDate.nullish(),
created_at: z.coerce.date().optional(),
updated_at: z.coerce.date().optional(),
last_edited: z.coerce.date().nullish(),
deleted_at: z.coerce.date().nullish(),
last_commented_on: z.coerce.date().nullish(),
marked_as_spam_at: z.coerce.date().nullish(),
archived_at: z.coerce.date().nullish(),
locked_at: z.coerce.date().nullish(),
discord_meta: z.object(discordMetaSchema).nullish(),

//counts
reaction_count: PG_INT,
reaction_weights_sum: PG_INT,
comment_count: PG_INT,

activity_rank_date: zDate.nullish(),
activity_rank_date: z.coerce.date().nullish(),

//notifications
max_notif_id: PG_INT,
Expand Down
4 changes: 2 additions & 2 deletions libs/schemas/src/queries/contests.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { ContestManager } from '../entities';
import { Contest, ContestAction } from '../projections';
import { PG_INT, zDate } from '../utils';
import { PG_INT } from '../utils';

export const ContestResults = ContestManager.extend({
topics: z.array(z.object({ id: z.number(), name: z.string() })),
Expand Down Expand Up @@ -51,7 +51,7 @@ export const ContestLogEntry = z.object({
voting_power: PG_INT.nullish(),
thread_id: PG_INT.nullish(),
thread_title: z.string().nullish(),
created_at: zDate,
created_at: z.coerce.date(),
});

export const GetContestLog = {
Expand Down
5 changes: 2 additions & 3 deletions libs/schemas/src/queries/thread.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
linksSchema,
paginationSchema,
PG_INT,
zBoolean,
} from '../utils';

export const OrderByQueriesKeys = z.enum([
Expand Down Expand Up @@ -77,8 +76,8 @@ export const GetBulkThreads = {
community_id: z.string(),
fromDate: z.coerce.date().optional(),
toDate: z.coerce.date().optional(),
archived: zBoolean.default(false),
includePinnedThreads: zBoolean.default(false),
archived: z.coerce.boolean().default(false),
includePinnedThreads: z.coerce.boolean().default(false),
topicId: PG_INT.optional(),
stage: z.string().optional(),
orderBy: OrderByQueriesKeys.default('createdAt:desc'),
Expand Down
6 changes: 1 addition & 5 deletions libs/schemas/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ export const linksSchema = {
};

export const PG_INT = z.number().int().min(MIN_SCHEMA_INT).max(MAX_SCHEMA_INT);
export const zBoolean = z.preprocess((v) => v && v !== 'false', z.boolean());
export const zDate = z.preprocess(
(arg) => (typeof arg === 'string' ? new Date(arg) : arg),
z.date(),
);

export const ETHERS_BIG_NUMBER = z.object({
hex: z.string().regex(/^0x[0-9a-fA-F]+$/),
type: z.literal('BigNumber'),
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/src/types/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type NotificationCategory =
// TODO: @Timothee remove this type in favor of the one below once webhook and email functions are fixed + tested and
// their types are updated
export interface IForumNotificationData {
created_at: any;
created_at: Date;
thread_id: number | string;
root_title: string;
root_type: string;
Expand All @@ -51,7 +51,7 @@ export interface IForumNotificationData {
// | ICommentEditNotificationData;

export interface IBaseForumNotificationData {
created_at: any;
created_at: Date;
thread_id: number | string;
root_title: string;
root_type: string;
Expand Down

0 comments on commit 1bbcaee

Please sign in to comment.