diff options
Diffstat (limited to 'lib/items/validations.ts')
| -rw-r--r-- | lib/items/validations.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/items/validations.ts b/lib/items/validations.ts new file mode 100644 index 00000000..d299959c --- /dev/null +++ b/lib/items/validations.ts @@ -0,0 +1,47 @@ +import { + createSearchParamsCache, + parseAsArrayOf, + parseAsInteger, + parseAsString, + parseAsStringEnum, +} from "nuqs/server" +import * as z from "zod" + +import { getFiltersStateParser, getSortingStateParser } from "@/lib/parsers" +import { Item } from "@/db/schema/items"; + +export const searchParamsCache = createSearchParamsCache({ + flags: parseAsArrayOf(z.enum(["advancedTable", "floatingBar"])).withDefault( + [] + ), + page: parseAsInteger.withDefault(1), + perPage: parseAsInteger.withDefault(10), + sort: getSortingStateParser<Item>().withDefault([ + { id: "createdAt", desc: true }, + ]), + itemCode: parseAsString.withDefault(""), + itemName: parseAsString.withDefault(""), + description: parseAsString.withDefault(""), + + // advanced filter + filters: getFiltersStateParser().withDefault([]), + joinOperator: parseAsStringEnum(["and", "or"]).withDefault("and"), + search: parseAsString.withDefault(""), + +}) + +export const createItemSchema = z.object({ + itemCode: z.string(), + itemName: z.string(), + description: z.string(), +}) + +export const updateItemSchema = z.object({ + itemCode: z.string().optional(), + itemName: z.string().optional(), + description: z.string().optional(), +}) + +export type GetItemsSchema = Awaited<ReturnType<typeof searchParamsCache.parse>> +export type CreateItemSchema = z.infer<typeof createItemSchema> +export type UpdateItemSchema = z.infer<typeof updateItemSchema> |
