Files
permit-api/src/tokens/token.schema.ts
2024-12-20 13:17:53 +05:30

55 lines
1.0 KiB
TypeScript

import { buildJsonSchemas } from "fastify-zod";
import mongoose from "mongoose";
import { z } from "zod";
export const tokenModel = mongoose.model(
"token",
new mongoose.Schema({
tenantId: {
type: String,
required: true,
},
pid: {
type: String,
unique: true,
},
name: String,
claims: [],
userId: mongoose.Types.ObjectId,
hash: { type: String, required: true },
createdAt: Date,
})
);
const tokenCore = {
name: z.string().max(30),
claims: z.array(z.string()).nonempty(),
};
const createTokenInput = z.object({
...tokenCore,
});
const createTokenResponse = z.object({
pid: z.string(),
token: z.string(),
...tokenCore,
});
const getTokenResponse = z.object({
pid: z.string(),
name: z.string(),
createdAt: z.string(),
});
export type CreateTokenInput = z.infer<typeof createTokenInput>;
export const { schemas: tokenSchemas, $ref: $token } = buildJsonSchemas(
{
createTokenInput,
createTokenResponse,
getTokenResponse,
},
{ $id: "token" }
);