@chubbyts/chubbyts-api
Version:
[](https://github.com/chubbyts/chubbyts-api/actions?query=workflow%3ACI) [ • 1.66 kB
JavaScript
import { z } from 'zod';
export const stringSchema = z.string().min(1);
export const numberSchema = z.coerce.number();
export const dateSchema = z.coerce.date();
export const sortSchema = z.union([z.literal('asc'), z.literal('desc')]).optional();
const embeddedSchema = z.object({}).loose().optional();
const linkSchema = z.intersection(z.object({
href: z.string(),
name: z.string().optional(),
templated: z.boolean().optional(),
}), z.record(z.string(), z.unknown()));
const linksSchema = z.record(z.string(), z.union([linkSchema, z.array(linkSchema)])).optional();
export const createModelSchema = (inputModelSchema) => z
.object({ ...inputModelSchema.shape, id: stringSchema, createdAt: dateSchema, updatedAt: dateSchema.optional() })
.strict();
export const createModelListSchema = (inputModelSchema, inputModelListSchema) => z
.object({
...inputModelListSchema.shape,
count: numberSchema,
items: z.array(createModelSchema(inputModelSchema)),
})
.strict();
export const createEnrichedModelSchema = (inputModelSchema, embeddedModelSchema = embeddedSchema) => z
.object({
...createModelSchema(inputModelSchema).shape,
_embedded: embeddedModelSchema,
_links: linksSchema,
})
.strict();
export const createEnrichedModelListSchema = (inputModelSchema, inputModelListSchema, embeddedModelSchema = embeddedSchema, embeddedModelListSchema = embeddedSchema) => z
.object({
...inputModelListSchema.shape,
count: numberSchema,
items: z.array(createEnrichedModelSchema(inputModelSchema, embeddedModelSchema)),
_embedded: embeddedModelListSchema,
_links: linksSchema,
})
.strict();