-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.ts
83 lines (76 loc) · 2.19 KB
/
validation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { z } from "zod";
import { jobTypes, locationTypes } from "./job-types";
const companyLogoSchema = z
.custom<File | undefined>()
.refine(
(file) => !file || (file instanceof File && file.type.startsWith("image/")),
"Must be an image file",
)
.refine((file) => {
return !file || file.size < 1024 * 1024 * 2;
}, "File must be less than 2MB");
const numericRegex = /^\d+$/;
const locationSchema = z
.object({
locationType: z
.string()
.min(1, "Required")
.refine(
(value) => locationTypes.includes(value),
"Invalid location type",
),
location: z.string().max(100).optional(),
})
.refine(
(data) =>
!data.locationType || data.locationType === "Remote" || data.location,
{
message: "Location is required for on-site jobs",
path: ["location"],
},
);
const applicationSchema = z
.object({
applicationEmail: z
.string()
.email()
.max(100, "Email must be less than 100 characters")
.optional()
.or(z.literal("")),
applicationUrl: z
.string()
.url()
.max(200, "URL must be less than 200 characters")
.optional()
.or(z.literal("")),
})
.refine((val) => val.applicationEmail || val.applicationUrl, {
message: "Either email or URL is required",
path: ["applicationEmail"],
});
export const createJobSchema = z
.object({
title: z.string().min(1, "Required").max(100),
type: z
.string()
.min(1, "Required")
.refine((val) => jobTypes.includes(val), "Invalid job type"),
companyName: z.string().min(1, "Required").max(100),
companyLogo: companyLogoSchema,
description: z.string().max(5000).optional(),
salary: z
.string()
.min(1, "Required")
.max(9, "Number must be less than 9 digits")
.regex(numericRegex, "Must be a number"),
})
.and(applicationSchema)
.and(locationSchema);
export const JobFilterSchema = z.object({
q: z.string().optional(),
type: z.string().optional(),
location: z.string().optional(),
remote: z.coerce.boolean().optional(),
});
export type JobFilterValue = z.infer<typeof JobFilterSchema>;
export type CreateJobValues = z.infer<typeof createJobSchema>;