Skip to content

Commit

Permalink
feat: add zod
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Feb 21, 2024
1 parent 44492a7 commit 705dec6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
9 changes: 9 additions & 0 deletions public/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions public/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"date-fns": "^3.3.1",
"rxjs": "~7.8.1",
"tslib": "^2.6.2",
"zod": "^3.22.4",
"zone.js": "~0.14.4"
},
"devDependencies": {
Expand Down
26 changes: 16 additions & 10 deletions public/src/app/model/company.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Timestamp } from '@angular/fire/firestore';
import { z } from 'zod';

export type SponsoringType = Record<string, number>;

export const ZodConfiguration = z.object({
next_value: z.string(),
enabled: z.boolean(),
openingDate: z.string(),
sponsorships: z.array(
z.object({
freeTickets: z.number(),
name: z.string(),
price: z.number(),
priceString: z.string(),
}),
),
});
export type Configuration = SponsoringType & {
next_value: string;
enabled: boolean;
openingDate: string;
sponsorships: {
freeTickets: number;
name: string;
price: number;
priceString: number;
}[];
sponsoringOptions?: SponsoringOption[];
};
} & z.infer<typeof ZodConfiguration>;

export type SponsoringOption = { key: string; label: string; price: number };
export interface Workflow {
Expand Down
9 changes: 8 additions & 1 deletion public/src/app/services/partner.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, inject } from '@angular/core';
import { Company, Configuration } from '../model/company';
import { Company, Configuration, ZodConfiguration } from '../model/company';
import {
Firestore,
QueryDocumentSnapshot,
Expand Down Expand Up @@ -47,6 +47,13 @@ export class PartnerService {
const data = await firstValueFrom(
docData(doc(this.firestore, 'configuration/invoice_2024')),
);

const check = ZodConfiguration.safeParse(data);

if (!check.success) {
console.log(check);
throw new Error(`Bad Configuration of your event`);
}
return data as Configuration;
}

Expand Down

0 comments on commit 705dec6

Please sign in to comment.