feat: spec to platform
This commit is contained in:
25
platform/models/account.ts
Normal file
25
platform/models/account.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { RoleSchema } from "@platform/spec/account/role.ts";
|
||||
import { StrategySchema } from "@platform/spec/account/strategies.ts";
|
||||
import { z } from "zod";
|
||||
|
||||
import { makeModelParser } from "./helpers/parser.ts";
|
||||
import { AvatarSchema } from "./value-objects/avatar.ts";
|
||||
import { ContactSchema } from "./value-objects/contact.ts";
|
||||
import { NameSchema } from "./value-objects/name.ts";
|
||||
|
||||
export const AccountSchema = z.object({
|
||||
id: z.uuid(),
|
||||
avatar: AvatarSchema.optional(),
|
||||
name: NameSchema.optional(),
|
||||
contact: ContactSchema.default({
|
||||
emails: [],
|
||||
}),
|
||||
strategies: z.array(StrategySchema).default([]),
|
||||
roles: z.array(RoleSchema).default([]),
|
||||
});
|
||||
|
||||
export const toAccountDocument = makeModelParser(AccountSchema);
|
||||
export const fromAccountDocument = makeModelParser(AccountSchema);
|
||||
|
||||
export type Account = z.infer<typeof AccountSchema>;
|
||||
export type AccountDocument = z.infer<typeof AccountSchema>;
|
||||
15
platform/models/helpers/parser.ts
Normal file
15
platform/models/helpers/parser.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import z, { ZodObject } from "zod";
|
||||
|
||||
export function makeModelParser<TSchema extends ZodObject>(schema: TSchema): ModelParserFn<TSchema> {
|
||||
return ((value: unknown | unknown[]) => {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((value: unknown) => schema.parse(value));
|
||||
}
|
||||
return schema.parse(value);
|
||||
}) as ModelParserFn<TSchema>;
|
||||
}
|
||||
|
||||
type ModelParserFn<TSchema extends ZodObject> = {
|
||||
(value: unknown): z.infer<TSchema>;
|
||||
(value: unknown[]): z.infer<TSchema>[];
|
||||
};
|
||||
10
platform/models/package.json
Normal file
10
platform/models/package.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "@platform/models",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@platform/spec": "workspace:*",
|
||||
"zod": "4"
|
||||
}
|
||||
}
|
||||
7
platform/models/value-objects/avatar.ts
Normal file
7
platform/models/value-objects/avatar.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import z from "zod";
|
||||
|
||||
export const AvatarSchema = z.object({
|
||||
url: z.string().describe("A valid URL pointing to the user's avatar image."),
|
||||
});
|
||||
|
||||
export type Avatar = z.infer<typeof AvatarSchema>;
|
||||
9
platform/models/value-objects/contact.ts
Normal file
9
platform/models/value-objects/contact.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import z from "zod";
|
||||
|
||||
import { EmailSchema } from "./email.ts";
|
||||
|
||||
export const ContactSchema = z.object({
|
||||
emails: z.array(EmailSchema).default([]).describe("A list of email addresses associated with the contact."),
|
||||
});
|
||||
|
||||
export type Contact = z.infer<typeof ContactSchema>;
|
||||
11
platform/models/value-objects/email.ts
Normal file
11
platform/models/value-objects/email.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import z from "zod";
|
||||
|
||||
export const EmailSchema = z.object({
|
||||
type: z.enum(["personal", "work"]).describe("The context of the email address, e.g., personal or work."),
|
||||
value: z.email().describe("A valid email address string."),
|
||||
primary: z.boolean().describe("Indicates if this is the primary email."),
|
||||
verified: z.boolean().describe("True if the email address has been verified."),
|
||||
label: z.string().optional().describe("Optional display label for the email address."),
|
||||
});
|
||||
|
||||
export type Email = z.infer<typeof EmailSchema>;
|
||||
8
platform/models/value-objects/name.ts
Normal file
8
platform/models/value-objects/name.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const NameSchema = z.object({
|
||||
family: z.string().nullable().describe("Family name, also known as last name or surname."),
|
||||
given: z.string().nullable().describe("Given name, also known as first name."),
|
||||
});
|
||||
|
||||
export type Name = z.infer<typeof NameSchema>;
|
||||
Reference in New Issue
Block a user